You are here

public function MenuLinkField::getEntity in Menu Link (Field) 8

Same name and namespace in other branches
  1. 2.0.x src/Plugin/Menu/MenuLinkField.php \Drupal\menu_link\Plugin\Menu\MenuLinkField::getEntity()

Loads the entity the field was attached to.

Return value

\Drupal\Core\Entity\EntityInterface Returns the entity, if exists.

2 calls to MenuLinkField::getEntity()
MenuLinkField::deleteLink in src/Plugin/Menu/MenuLinkField.php
Deletes a menu link.
MenuLinkField::getProperty in src/Plugin/Menu/MenuLinkField.php
Gets a specific property.

File

src/Plugin/Menu/MenuLinkField.php, line 177

Class

MenuLinkField
Provides a menu link plugin for field-based menu links.

Namespace

Drupal\menu_link\Plugin\Menu

Code

public function getEntity() {
  if (empty($this->entity)) {
    $entity_type_id = $this->pluginDefinition['metadata']['entity_type_id'];
    $entity_id = $this->pluginDefinition['metadata']['entity_id'];
    $entity = $this->entityTypeManager
      ->getStorage($entity_type_id)
      ->load($entity_id);
    if ($entity instanceof TranslatableInterface && $this->pluginDefinition['metadata']['langcode'] !== LanguageInterface::LANGCODE_NOT_SPECIFIED && $entity
      ->hasTranslation($this->pluginDefinition['metadata']['langcode'])) {
      $this->entity = $entity
        ->getTranslation($this->pluginDefinition['metadata']['langcode']);
    }
    else {
      $this->entity = $this->entityRepository
        ->getTranslationFromContext($entity);
    }
  }
  return $this->entity;
}