protected function getConfigNames($config_type) { $this->configStorage = $this->getDrupalService('config.storage'); // For a given entity type, load all entities. if ($config_type && $config_type !== 'system.simple') { $entity_storage = $this->entityManager->getStorage($config_type); foreach ($entity_storage->loadMultiple() as $entity) { $entity_id = $entity->id(); $label = $entity->label() ?: $entity_id; $names[$entity_id] = $label; } } else { // Gather the config entity prefixes. $config_prefixes = array_map(function ($definition) { return $definition->getConfigPrefix() . '.'; }, $this->definitions); // Find all config, and then filter our anything matching a config prefix. $names = $this->configStorage->listAll(); $names = array_combine($names, $names); foreach ($names as $config_name) { foreach ($config_prefixes as $config_prefix) { if (strpos($config_name, $config_prefix) === 0) { unset($names[$config_name]); } } } } return $names; }
/** * Say hello to the world. * * @return string * Return "Hello world!" string. */ public function helloWorld(EntityViewModeInterface $viewmode) { $content = ['#type' => 'markup', '#markup' => $this->t('Hello world!')]; // Second version displaying the opening hours of the library. $opening_hours = $this->config('happy_alexandrie.library_config')->get('opening_hours'); if (!empty($opening_hours)) { $content = ['#markup' => $this->t('<p>Greetings dear adventurer!</p><p>Opening hours:<br />@opening_hours</p>', array('@opening_hours' => $opening_hours))]; } // Third version with the query // Query against our entities. $query = $this->query_factory->get('node')->condition('status', 1)->condition('type', 'alexandrie_book')->condition('changed', REQUEST_TIME, '<')->range(0, 5); $nids = $query->execute(); if ($nids) { // Load the storage manager of our entity. $storage = $this->entity_manager->getStorage('node'); // Now we can load the entities. $nodes = $storage->loadMultiple($nids); list($entity_type, $viewmode_name) = explode('.', $viewmode->getOriginalId()); // Get the EntityViewBuilder instance. $render_controller = $this->entity_manager->getViewBuilder('node'); $build = $render_controller->viewMultiple($nodes, $viewmode_name); $build['#markup'] = $this->t('Happy Query by view mode: @label', array('@label' => $viewmode->label())); $content[] = $build; } else { $content[] = array('#markup' => $this->t('No result')); } return $content; }
/** * {inheritdoc} */ protected function getTitles($idList) { $ret = []; foreach ($this->entityManager->getStorage('node')->load($idList) as $node) { $ret[$node->nid] = entity_label('node', $node); } return $ret; }
/** * {@inheritdoc} */ public function build() { $items = array(); // When on a node page, the parameter is already upcast as a node. $node = \Drupal::routeMatch()->getParameter('node'); if ($node) { // Read about entity query at http://www.sitepoint.com/drupal-8-version-entityfieldquery/. // See query API at https://api.drupal.org/api/drupal/core%21lib%21Drupal%21Core%21Entity%21Query%21QueryInterface.php/interface/QueryInterface/8. // The static method for using the query service would have been: // $query = \Drupal::entityQuery('node'); // Use injected queryFactory to look for items that are related // to the show that matches the input nid. $query = $this->entity_query->get('node')->condition('status', 1)->condition('type', 'tv_episode')->sort('field_related_season.entity.field_season_number.value', 'DESC')->sort('field_episode_number', 'DESC')->range(0, 5)->condition('field_related_season.entity.field_related_show.entity.nid', $node->id()); $nids = $query->execute(); // Note that entity_load() is deprecated. // The static method of loading the entity would have been: // \Drupal\node\Entity\Node::load(); // Use the injected entityManager to load the results into an array of node objects. $nodes = $this->entity_manager->getStorage('node')->loadMultiple($nids); foreach ($nodes as $node) { // Create a render array for each title field. // Note that field_view_field() is deprecated, use the view method on the field. $title = $node->title->view('full'); // Entities have a handy toLink() method. $items[] = $node->toLink($title); } } // Return a render array for a html list. return ['#theme' => 'item_list', '#items' => $items, '#cache' => ['contexts' => ['route']]]; }
/** * Update a field. This is used to process fields when the storage * configuration changes. * * @param $entity_type * @param $field_name * @param string $op (encrypt / decrypt) */ protected function update_stored_field($entity_type, $field_name, $op = 'encrypt') { /** * Before we load entities, we have to disable the encryption setting. * Otherwise, the act of loading the entity triggers an improper decryption * Which messes up the batch encryption. */ $this->updatingStoredField = $field_name; /** * @var $query \Drupal\Core\Entity\Query\QueryInterface */ $query = $this->queryFactory->get($entity_type); // The field is present. $query->exists($field_name); $query->allRevisions(); $entity_ids = $query->execute(); // Load entities. /** * @var $entity_storage \Drupal\Core\Entity\ContentEntityStorageBase */ $entity_storage = $this->entityManager->getStorage($entity_type); foreach ($entity_ids as $revision_id => $entity_id) { /** * @var $entity \Drupal\Core\Entity\Entity */ $entity = $entity_storage->loadRevision($revision_id); /** * @var $field \Drupal\Core\Field\FieldItemList */ $field = $entity->get($field_name); $this->process_field($field, $op, TRUE); // Save the entity. $entity->save(); } }
/** * Gets the currently wrapped account * * @return AccountInterface */ public function getAccount() { if (!$this->account) { if ($this->originalAccount && $this->originalAccount->uid) { // Prey for Drupal being correctly initialized at this point $this->account = $this->entityManager->getStorage('user')->load($this->originalAccount->uid); } else { if ($this->originalAccount) { // User is not logged in, but we do have already a User // instance for anonymous user set $this->account = $this->originalAccount; } } } if (!$this->account instanceof AccountInterface) { // @todo why do the hell this happens only during unit tests? // seriously god I hate Drupal, and whatever is arround it $values = (array) $this->account; $this->account = new User(); foreach ($values as $key => $value) { $this->account->{$key} = $value; } } return $this->account; }
/** * {@inheritdoc} */ public function build() { // If the page is not a solution page, return an empty form so that the // rendering of this block can be omitted. if (empty($this->solution) || $this->solution->bundle() != 'solution') { return []; } // Get news referencing to this solution. // @todo EntityManager is deprecated. Use EntityTypeManager instead. // @see https://webgate.ec.europa.eu/CITnet/jira/browse/ISAICP-2669 $entities = $this->entityManager->getStorage('node')->loadByProperties([OgGroupAudienceHelperInterface::DEFAULT_FIELD => $this->solution->id()]); $items = []; foreach ($entities as $entity) { $items[] = ['#markup' => $entity->link()]; } // Also retrieve related collections. $ids = $this->entityManager->getStorage('rdf_entity')->getQuery()->condition('field_ar_affiliates', $this->solution->id())->execute(); $entities = Rdf::loadMultiple($ids); foreach ($entities as $entity) { $items[] = ['#markup' => $entity->link()]; } // Build the array output. if ($items) { return ['list' => ['#theme' => 'item_list', '#items' => $items, '#cache' => ['tags' => ['entity:node:news', 'entity:rdf_entity:collection']]]]; } return []; }
/** * Creates the contact message entity without saving it. * * @return \Drupal\contact\Entity\Message|null * The contact message entity. NULL if the entity does not exist. */ protected function createContactMessage() { $contact_message = NULL; $contact_form = $this->getContactForm(); if ($contact_form) { $contact_message = $this->entityManager->getStorage('contact_message')->create(['contact_form' => $contact_form->id()]); } return $contact_message; }
/** * {@inheritdoc} */ public function build() { $node = $this->entity_manager->getStorage('node')->load($this->configuration['block_node_id']); if (!$node) { return ''; } $this->setViewMode($this->configuration['block_node_view_mode']); $view_builder = $this->entity_manager->getViewBuilder('node'); return $view_builder->view($node, $this->configuration['block_node_view_mode']); }
/** * Returns the schema for the given table. * * @param string $table * The table name. * * @return array|bool * The table field mapping for the given table or FALSE if not available. */ protected function getTableMapping($table, $entity_type_id) { $storage = $this->entityManager->getStorage($entity_type_id); if ($storage instanceof SqlEntityStorageInterface) { $mapping = $storage->getTableMapping()->getAllColumns($table); } else { return FALSE; } return array_flip($mapping); }
/** * Returns the schema for the given table. * * @param string $table * The table name. * * @return array|bool * The table field mapping for the given table or FALSE if not available. */ protected function getTableMapping($table, $entity_type_id) { $storage = $this->entityManager->getStorage($entity_type_id); if ($storage instanceof SqlEntityStorageInterface) { $mapping = $storage->getTableMapping()->getAllColumns($table); } else { // @todo Stop calling drupal_get_schema() once menu links are converted // to the Entity Field API. See https://drupal.org/node/1842858. $schema = drupal_get_schema($table); $mapping = array_keys($schema['fields']); } return array_flip($mapping); }
/** * Gets the human-readable labels for the given field storage names. * * Since not all field storages are required to have a field, we can only * provide the field labels on a best-effort basis (e.g. the label of a field * storage without any field attached to a bundle will be the field name). * * @param array $field_names * An array of field names. * * @return array * An array of field labels keyed by field name. */ protected function getExistingFieldLabels(array $field_names) { // Get all the fields corresponding to the given field storage names and // this entity type. $field_ids = $this->queryFactory->get('field_config')->condition('entity_type', $this->entityTypeId)->condition('field_name', $field_names)->execute(); $fields = $this->entityManager->getStorage('field_config')->loadMultiple($field_ids); // Go through all the fields and use the label of the first encounter. $labels = array(); foreach ($fields as $field) { if (!isset($labels[$field->getName()])) { $labels[$field->getName()] = $field->label(); } } // For field storages without any fields attached to a bundle, the default // label is the field name. $labels += array_combine($field_names, $field_names); return $labels; }
/** * Get entity internal path * * @param string $type * Entity type * @param int|string $id * Entity identifier * * @return string * The Drupal internal path */ public function getEntityPath($type, $id) { // In most cases, this will be used for nodes only, so just set the // node URL. // It will avoid nasty bugs, since the 'text' core module does sanitize // (and call check_markup()) during field load if there are any circular // links dependencies between two nodes, it triggers an finite loop. // This will also make the whole faster. // @todo If node does not exists, no error will be triggered. if ('node' === $type) { return 'node/' . $id; } $entity = $this->entityManager->getStorage($type)->load($id); if (!$entity) { throw new \InvalidArgumentException(sprintf("entity of type %s with identifier %s does not exist", $type, $id)); } if (!$entity instanceof EntityInterface) { return $this->getDrupalEntityPath($type, $entity); } else { return $entity->url(); } }
/** * Constructs a new EventManager object. * * @param EntityManager $entity_manager * The entity manager. */ function __construct(EntityManager $entity_manager) { $this->eventTypeStorage = $entity_manager->getStorage('event_type'); }
/** * {@inheritdoc} */ public function getDescription() { return $this->entityManager->getStorage('taxonomy_term')->load($this->pluginDefinition['metadata']['taxonomy_term_id'])->getDescription(); }
/** * {@inheritdoc} */ public function build() { $form = $this->entity_manager->getStorage('contact_message')->create(array('contact_form' => $this->configuration['contact_form'])); if (!$form) { return ''; } $form = $this->entity_form_builder->getForm($form); return $form; }