Exemple #1
0
 /**
  * Loads the entity associated with this menu link.
  *
  * @return \Drupal\menu_link_content\MenuLinkContentInterface
  *   The menu link content entity.
  *
  * @throws \Drupal\Component\Plugin\Exception\PluginException
  *   If the entity ID and UUID are both invalid or missing.
  */
 protected function getEntity()
 {
     if (empty($this->entity)) {
         $entity = NULL;
         $storage = $this->entityManager->getStorage('menu_link_content');
         if (!empty($this->pluginDefinition['metadata']['entity_id'])) {
             $entity_id = $this->pluginDefinition['metadata']['entity_id'];
             // Make sure the current ID is in the list, since each plugin empties
             // the list after calling loadMultiple(). Note that the list may include
             // multiple IDs added earlier in each plugin's constructor.
             static::$entityIdsToLoad[$entity_id] = $entity_id;
             $entities = $storage->loadMultiple(array_values(static::$entityIdsToLoad));
             $entity = isset($entities[$entity_id]) ? $entities[$entity_id] : NULL;
             static::$entityIdsToLoad = array();
         }
         if (!$entity) {
             // Fallback to the loading by the UUID.
             $uuid = $this->getUuid();
             $loaded_entities = $storage->loadByProperties(array('uuid' => $uuid));
             $entity = reset($loaded_entities);
         }
         if (!$entity) {
             throw new PluginException("Entity not found through the menu link plugin definition and could not fallback on UUID '{$uuid}'");
         }
         // Clone the entity object to avoid tampering with the static cache.
         $this->entity = clone $entity;
         $the_entity = $this->entityManager->getTranslationFromContext($this->entity);
         /** @var \Drupal\menu_link_content\MenuLinkContentInterface $the_entity */
         $this->entity = $the_entity;
         $this->entity->setInsidePlugin();
     }
     return $this->entity;
 }
 /**
  * Loads the entity associated with this menu link.
  *
  * @return \Drupal\menu_link_config\MenuLinkConfigInterface
  *   The menu link content entity.
  *
  * @throws \Drupal\Component\Plugin\Exception\PluginException
  *   If the entity ID and UUID are both invalid or missing.
  */
 public function getEntity()
 {
     if (empty($this->entity)) {
         $entity = NULL;
         $storage = $this->entityManager->getStorage('menu_link_config');
         if (!empty($this->pluginDefinition['metadata']['entity_id'])) {
             $entity_id = $this->pluginDefinition['metadata']['entity_id'];
             // Make sure the current ID is in the list, since each plugin empties
             // the list after calling loadMultple(). Note that the list may include
             // multiple IDs added earlier in each plugin's constructor.
             static::$entityIdsToLoad[$entity_id] = $entity_id;
             $entities = $storage->loadMultiple(array_values(static::$entityIdsToLoad));
             $entity = isset($entities[$entity_id]) ? $entities[$entity_id] : NULL;
             static::$entityIdsToLoad = array();
         }
         if (!$entity) {
             // Fallback to the loading by the ID.
             $entity = $storage->load($this->getDerivativeId());
         }
         if (!$entity) {
             throw new PluginException(String::format('Entity not found through the menu link plugin definition and could not fallback on ID @id', array('@uuid' => $this->getDerivativeId())));
         }
         // Clone the entity object to avoid tampering with the static cache.
         $this->entity = clone $entity;
         $this->entity = $this->entityManager->getTranslationFromContext($this->entity);
     }
     return $this->entity;
 }