function MenuLinkContent::getEntity

Same name in other branches
  1. 9 core/modules/menu_link_content/src/Plugin/Menu/MenuLinkContent.php \Drupal\menu_link_content\Plugin\Menu\MenuLinkContent::getEntity()
  2. 8.9.x core/modules/menu_link_content/src/Plugin/Menu/MenuLinkContent.php \Drupal\menu_link_content\Plugin\Menu\MenuLinkContent::getEntity()
  3. 10 core/modules/menu_link_content/src/Plugin/Menu/MenuLinkContent.php \Drupal\menu_link_content\Plugin\Menu\MenuLinkContent::getEntity()

Loads the entity associated with this menu link.

Return value

\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.

2 calls to MenuLinkContent::getEntity()
MenuLinkContent::getListBuilderOperations in core/modules/menu_link_content/src/Plugin/Menu/MenuLinkContent.php
Load entity operations from the list builder.
MenuLinkContent::updateLink in core/modules/menu_link_content/src/Plugin/Menu/MenuLinkContent.php

File

core/modules/menu_link_content/src/Plugin/Menu/MenuLinkContent.php, line 134

Class

MenuLinkContent
Provides the menu link plugin for content menu links.

Namespace

Drupal\menu_link_content\Plugin\Menu

Code

public function getEntity() : MenuLinkContentInterface {
    if (empty($this->entity)) {
        $entity = NULL;
        $storage = $this->entityTypeManager
            ->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 = $entities[$entity_id] ?? NULL;
            static::$entityIdsToLoad = [];
        }
        if (!$entity) {
            // Fallback to the loading by the UUID.
            $uuid = $this->getUuid();
            $entity = $this->entityRepository
                ->loadEntityByUuid('menu_link_content', $uuid);
        }
        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->entityRepository
            ->getTranslationFromContext($this->entity);
        
        /** @var \Drupal\menu_link_content\MenuLinkContentInterface $the_entity */
        $this->entity = $the_entity;
        $this->entity
            ->setInsidePlugin();
    }
    return $this->entity;
}

Buggy or inaccurate documentation? Please file an issue. Need support? Need help programming? Connect with the Drupal community.