/** * {@inheritdoc} */ function eventTypeWithEntityType($entity_type) { $ids = $this->eventTypeStorage->getQuery()->condition('entity_type', $entity_type, '=')->execute(); if ($ids) { return $this->eventTypeStorage->loadMultiple($ids); } return []; }
/** * Loads the cart data for the given user. * * @param \Drupal\Core\Session\AccountInterface $account * The user. If empty, the current user is assumed. * * @return array * The cart data. */ protected function loadCartData(AccountInterface $account = NULL) { $account = $account ?: $this->currentUser; $uid = $account->id(); if (isset($this->cartData[$uid])) { return $this->cartData[$uid]; } if ($account->isAuthenticated()) { $query = $this->orderStorage->getQuery()->condition('cart', TRUE)->condition('uid', $account->id())->sort('order_id', 'DESC'); $cart_ids = $query->execute(); } else { $cart_ids = $this->cartSession->getCartIds(); } $this->cartData[$uid] = []; if (!$cart_ids) { return []; } // Getting the cart data and validating the cart ids received from the // session requires loading the entities. This is a performance hit, but // it's assumed that these entities would be loaded at one point anyway. $carts = $this->orderStorage->loadMultiple($cart_ids); foreach ($carts as $cart) { if ($cart->getOwnerId() != $uid || empty($cart->cart)) { // Skip orders that are no longer eligible. continue; } $this->cartData[$uid][$cart->id()] = ['type' => $cart->bundle(), 'store_id' => $cart->getStoreId()]; } return $this->cartData[$uid]; }
/** * Retrieves the revision translation affected flag value. * * @param \Drupal\entity_test\Entity\EntityTestMulRevChanged $entity * The entity object to be checked. * * @return bool * The flag value. */ protected function getRevisionTranslationAffectedFlag(EntityTestMulRevChanged $entity) { $query = $this->mulRevChangedStorage->getQuery(); $ids = $query->condition('revision_translation_affected', 1, '=', $entity->language()->getId())->execute(); $id = reset($ids); return (bool) ($id == $entity->id()); }
/** * Tests non-revisionable fields on revisionable entities. */ public function testNonRevisionableField() { $user1 = $this->createUser(); $user2 = $this->createUser(); // Create a test entity. $entity = EntityTestRev::create(array('name' => $this->randomString(), 'user_id' => $user1->id(), 'non_rev_field' => 'Superior')); $entity->save(); // Create a test entity. $entity2 = EntityTestRev::create(array('name' => $this->randomString(), 'user_id' => $user1->id(), 'non_rev_field' => 'Ontario')); $entity2->save(); $this->assertEquals('Superior', $entity->get('non_rev_field')->value, 'Superior found on entity 1'); $this->assertEquals('Ontario', $entity2->get('non_rev_field')->value, 'Ontario found on entity 2'); $entity->setNewRevision(); $entity->setOwner($user2); $entity->save(); $entity2->setNewRevision(); $entity2->setOwner($user2); $entity2->save(); $this->assertEquals($user2->id(), $entity->getOwner()->id(), 'User 2 found on entity 1'); $this->assertEquals($user2->id(), $entity2->getOwner()->id(), 'User 2 found on entity 2'); $expected_revision_ids = [4 => 2, 3 => 1, 2 => 2, 1 => 1]; $revision_ids = $this->rev->getQuery()->allRevisions()->sort('revision_id', 'DESC')->execute(); $this->assertEquals($expected_revision_ids, $revision_ids, 'Revision ids found'); $expected_non_rev_field_revision_ids = [3 => 1, 1 => 1]; $non_rev_field_revision_ids = $this->rev->getQuery()->allRevisions()->condition('non_rev_field', 'Superior')->sort('revision_id', 'DESC')->execute(); $this->assertEquals($expected_non_rev_field_revision_ids, $non_rev_field_revision_ids, 'Revision ids found'); }
/** * Provides the list of accounts that can be used for the user switch. * * Inactive users are omitted from all of the following db selects. Users * with 'switch users' permission and anonymous user if include_anon property * is set to TRUE, are prioritized. * * @return \Drupal\core\Session\AccountInterface[] * List of accounts to be used for the switch. */ protected function getUsers() { $list_size = $this->configuration['list_size']; $include_anonymous = $this->configuration['include_anon']; $list_size = $include_anonymous ? $list_size - 1 : $list_size; // Users with 'switch users' permission are prioritized so // we try to load first users with this permission. $query = $this->userStorage->getQuery()->condition('uid', 0, '>')->condition('status', 0, '>')->sort('access', 'DESC')->range(0, $list_size); $roles = user_roles(TRUE, 'switch users'); if (!isset($roles[Role::AUTHENTICATED_ID])) { $query->condition('roles', array_keys($roles), 'IN'); } $user_ids = $query->execute(); // If we don't have enough users with 'switch users' permission, add // uids until we hit $list_size. if (count($user_ids) < $list_size) { $users = $this->userStorage->getQuery()->condition('uid', 0, '>')->condition('status', 0, '>')->condition('uid', array_keys($user_ids), 'NOT IN')->sort('access', 'DESC')->range(0, $list_size - count($user_ids))->execute(); $user_ids += $users; } $accounts = $this->userStorage->loadMultiple($user_ids); if ($include_anonymous) { $anonymous = new AnonymousUserSession(); $accounts[$anonymous->id()] = $anonymous; } uasort($accounts, 'static::sortUserList'); return $accounts; }
/** * Return the same array passed as parameter * but with an array of uids for the key 'users'. * * @param $results array * The input values from the settings form. */ protected function preGenerate(&$results) { // Get user id. $users = $this->userStorage->getQuery()->range(0, 50)->execute(); $users = array_merge($users, ['0']); $results['users'] = $users; }
/** * {@inheritdoc} */ protected function generateElements(array $values) { $num = $values['num']; $kill = $values['kill']; $pass = $values['pass']; $age = $values['time_range']; $roles = array_filter($values['roles']); if ($kill) { $uids = $this->userStorage->getQuery()->condition('uid', 1, '>')->execute(); $users = $this->userStorage->loadMultiple($uids); $this->userStorage->delete($users); $this->setMessage($this->formatPlural(count($uids), '1 user deleted', '@count users deleted.')); } if ($num > 0) { $names = array(); while (count($names) < $num) { $name = $this->getRandom()->word(mt_rand(6, 12)); $names[$name] = ''; } if (empty($roles)) { $roles = array(DRUPAL_AUTHENTICATED_RID); } foreach ($names as $name => $value) { $account = $this->userStorage->create(array('uid' => NULL, 'name' => $name, 'pass' => $pass, 'mail' => $name . '@example.com', 'status' => 1, 'created' => REQUEST_TIME - mt_rand(0, $age), 'roles' => array_values($roles), 'devel_generate' => TRUE)); // Populate all fields with sample values. $this->populateFields($account); $account->save(); } } $this->setMessage($this->t('@num_users created.', array('@num_users' => $this->formatPlural($num, '1 user', '@count users')))); }
/** * Deletes all nodes of given node types. * * @param array $values * The input values from the settings form. */ protected function contentKill($values) { $nids = $this->nodeStorage->getQuery()->condition('type', $values['node_types'], 'IN')->execute(); if (!empty($nids)) { $nodes = $this->nodeStorage->loadMultiple($nids); $this->nodeStorage->delete($nodes); $this->setMessage($this->t('Deleted %count nodes.', array('%count' => count($nids)))); } }
/** * Deletes all products. */ protected function contentKill() { $pids = $this->productStorage->getQuery()->execute(); if (!empty($pids)) { $products = $this->productStorage->loadMultiple($pids); $this->productStorage->delete($products); $this->setMessage($this->t('Deleted %count products.', array('%count' => count($pids)))); } }
/** * Builds the overview of the licence entities. * * This is mimicking the rdf_entity's overview builder class. * * @see Drupal\rdf_entity\Entity\Controller\RdfListBuilder * * @return string * Return Hello string. */ public function overview() { $query = $this->entityStorage->getQuery()->condition('rid', 'licence'); $header = $this->buildHeader(); $query->tableSort($header); $rids = $query->execute(); $licences = Rdf::loadMultiple($rids); $rows = []; foreach ($licences as $licence) { $rows[] = $this->buildRow($licence); } $build['table'] = array('#type' => 'table', '#header' => $this->buildHeader(), '#rows' => array(), '#empty' => $this->t('There are no licences yet.')); foreach ($licences as $licence) { if ($row = $this->buildRow($licence)) { $build['table']['#rows'][$licence->id()] = $row; } } return $build; }
/** * Gets the entities that can be filtered by. * * @return \Drupal\Core\Entity\EntityInterface[] */ protected function getReferenceableEntities() { if ($this->referenceableEntities !== NULL) { return $this->referenceableEntities; } $target_ids = NULL; // Filter by bundle if if the plugin was configured to do so. $target_bundles = array_filter($this->options['verf_target_bundles']); if ($this->targetEntityType->hasKey('bundle') && $target_bundles) { $query = $this->targetEntityStorage->getQuery(); $query->condition($this->targetEntityType->getKey('bundle'), $target_bundles, 'IN'); $target_ids = $query->execute(); } $this->referenceableEntities = $this->targetEntityStorage->loadMultiple($target_ids); return $this->referenceableEntities; }
/** * Generates a unique machine name for a block. * * @param \Drupal\block\BlockInterface $block * The block entity. * * @return string * Returns the unique name. */ public function getUniqueMachineName(BlockInterface $block) { $suggestion = $block->getPlugin()->getMachineNameSuggestion(); // Get all the blocks which starts with the suggested machine name. $query = $this->storage->getQuery(); $query->condition('id', $suggestion, 'CONTAINS'); $block_ids = $query->execute(); $block_ids = array_map(function ($block_id) { $parts = explode('.', $block_id); return end($parts); }, $block_ids); // Iterate through potential IDs until we get a new one. E.g. // 'plugin', 'plugin_2', 'plugin_3', etc. $count = 1; $machine_default = $suggestion; while (in_array($machine_default, $block_ids)) { $machine_default = $suggestion . '_' . ++$count; } return $machine_default; }
/** * Deletes custom generated menus. */ protected function deleteMenus() { if ($this->moduleHandler->moduleExists('menu_ui')) { $menu_ids = array(); foreach (menu_ui_get_menus(FALSE) as $menu => $menu_title) { if (strpos($menu, 'devel-') === 0) { $menu_ids[] = $menu; } } if ($menu_ids) { $menus = $this->menuStorage->loadMultiple($menu_ids); $this->menuStorage->delete($menus); } } // Delete menu links generated by devel. $link_ids = $this->menuLinkContentStorage->getQuery()->condition('menu_name', 'devel', '<>')->condition('link__options', '%' . db_like('s:5:"devel";b:1') . '%', 'LIKE')->execute(); if ($link_ids) { $links = $this->menuLinkContentStorage->loadMultiple($link_ids); $this->menuLinkContentStorage->delete($links); } }
/** * Submission handler for the confirmation form. * * @param array $form * An associative array containing the structure of the form. * @param \Drupal\Core\Form\FormStateInterface $form_state * The current state of the form. */ public function submitConfirmForm(array &$form, FormStateInterface $form_state) { $storage = $form_state->getStorage(); if (isset($storage['upgrade_option']) && $storage['upgrade_option'] == static::MIGRATE_UPGRADE_ROLLBACK) { $query = $this->entityStorage->getQuery(); $names = $query->execute(); // Order the migrations according to their dependencies. /** @var \Drupal\migrate\Entity\MigrationInterface[] $migrations */ $migrations = $this->entityStorage->loadMultiple($names); // Assume we want all those tagged 'Drupal %'. foreach ($migrations as $migration_id => $migration) { $keep = FALSE; $tags = $migration->get('migration_tags'); foreach ($tags as $tag) { if (strpos($tag, 'Drupal ') === 0) { $keep = TRUE; break; } } if (!$keep) { unset($migrations[$migration_id]); } } // Roll back in reverse order. $migrations = array_reverse($migrations); $batch = ['title' => $this->t('Rolling back upgrade'), 'progress_message' => '', 'operations' => [[[MigrateUpgradeRunBatch::class, 'run'], [array_keys($migrations), 'rollback']]], 'finished' => [MigrateUpgradeRunBatch::class, 'finished']]; batch_set($batch); $form_state->setRedirect('migrate_upgrade.upgrade'); $this->state->delete('migrate_upgrade.performed'); } else { $migration_template = $storage['migration_template']; $migration_ids = $this->createMigrations($migration_template); $batch = ['title' => $this->t('Running upgrade'), 'progress_message' => '', 'operations' => [[[MigrateUpgradeRunBatch::class, 'run'], [$migration_ids, 'import']]], 'finished' => [MigrateUpgradeRunBatch::class, 'finished']]; batch_set($batch); $form_state->setRedirect('<front>'); $this->state->set('migrate_upgrade.performed', REQUEST_TIME); } }
/** * Returns an asset entity given an asset ID. * * @param string $asset_id * The asset ID property to check for. * @param EntityStorageInterface $storage * The storage to load from. * * @return EmbridgeAssetEntityInterface|NULL * Null if the asset didn't exist. */ private function loadFromAssetId($asset_id, EntityStorageInterface $storage) { $query = $storage->getQuery(); $query->condition('asset_id', $asset_id); $query_result = $query->execute(); if ($query_result) { $id = array_pop($query_result); return $storage->load($id); } return NULL; }
/** * Returns an entity query instance. * * @return \Drupal\Core\Entity\Query\QueryInterface * The query instance. */ protected function getQuery() { return $this->storage->getQuery(); }
/** * Counts the number of revisions in the default language. * * @param \Drupal\Core\Entity\ContentEntityInterface $entity * The entity. * @param \Drupal\Core\Entity\EntityStorageInterface $entity_storage * The entity storage. * * @return int * The number of revisions in the default language. */ protected function countDefaultLanguageRevisions(ContentEntityInterface $entity, EntityStorageInterface $entity_storage) { $entity_type = $entity->getEntityType(); $count = $entity_storage->getQuery()->allRevisions()->condition($entity_type->getKey('id'), $entity->id())->condition($entity_type->getKey('default_langcode'), 1)->count()->execute(); return $count; }