You are here

class MenuLinkTreeHandler in Menu Item Extras 8

Class MenuLinkTreeHandler.

Hierarchy

Expanded class hierarchy of MenuLinkTreeHandler

1 string reference to 'MenuLinkTreeHandler'
menu_item_extras.services.yml in ./menu_item_extras.services.yml
menu_item_extras.services.yml
1 service uses MenuLinkTreeHandler
menu_item_extras.menu_link_tree_handler in ./menu_item_extras.services.yml
Drupal\menu_item_extras\MenuLinkTreeHandler

File

src/MenuLinkTreeHandler.php, line 12

Namespace

Drupal\menu_item_extras
View source
class MenuLinkTreeHandler {

  /**
   * The entity type manager.
   *
   * @var \Drupal\Core\Entity\EntityTypeManagerInterface
   */
  protected $entityTypeManager;

  /**
   * The entity repository manager.
   *
   * @var \Drupal\Core\Entity\EntityRepositoryInterface
   */
  protected $entityRepository;

  /**
   * Constructs a new MenuLinkTreeHandler.
   *
   * @param \Drupal\Core\Entity\EntityTypeManagerInterface $entity_type_manager
   *   The entity type manager.
   * @param \Drupal\Core\Entity\EntityRepositoryInterface $entity_repository
   *   The entity repository manager.
   */
  public function __construct(EntityTypeManagerInterface $entity_type_manager, EntityRepositoryInterface $entity_repository) {
    $this->entityTypeManager = $entity_type_manager;
    $this->entityRepository = $entity_repository;
  }

  /**
   * Get menu_link_content entity.
   *
   * @param \Drupal\Core\Menu\MenuLinkInterface $link
   *   Link object.
   *
   * @return \Drupal\menu_link_content\Entity\MenuLinkContent|null
   *   Menu Link Content entity.
   */
  protected function getMenuLinkItemEntity(MenuLinkInterface $link) {
    $menu_item = NULL;
    $metadata = $link
      ->getMetaData();
    if (!empty($metadata['entity_id'])) {

      /** @var \Drupal\menu_link_content\Entity\MenuLinkContent $menu_item */
      $menu_item = $this->entityTypeManager
        ->getStorage('menu_link_content')
        ->load($metadata['entity_id']);
    }
    if ($menu_item) {
      $menu_item = $this->entityRepository
        ->getTranslationFromContext($menu_item);
    }
    return $menu_item;
  }

  /**
   * Get Menu Link Content entity content.
   *
   * @param \Drupal\Core\Menu\MenuLinkInterface $link
   *   Original link entity.
   *
   * @return array
   *   Renderable menu item content.
   */
  public function getMenuLinkItemContent(MenuLinkInterface $link) {
    $content = [];

    /** @var \Drupal\menu_link_content\Entity\MenuLinkContent $menu_item */
    $menu_item = $this
      ->getMenuLinkItemEntity($link);
    if ($menu_item && $menu_item
      ->hasField('body') && !$menu_item
      ->get('body')
      ->isEmpty()) {
      $field_body = $menu_item
        ->get('body')
        ->getValue();
      $content['body'] = [
        '#type' => 'processed_text',
        '#text' => $field_body[0]['value'],
        '#format' => $field_body[0]['format'],
      ];
    }
    return $content;
  }

  /**
   * Process menu tree items. Add menu item content.
   *
   * @param array $items
   *   Menu tree items.
   */
  public function processMenuLinkTree(array &$items) {
    foreach ($items as &$item) {
      if (isset($item['original_link'])) {
        $item['content'] = $this
          ->getMenuLinkItemContent($item['original_link']);
      }

      // Process subitems.
      if ($item['below']) {
        $this
          ->processMenuLinkTree($item['below']);
      }
    }
  }

}

Members

Namesort descending Modifiers Type Description Overrides
MenuLinkTreeHandler::$entityRepository protected property The entity repository manager.
MenuLinkTreeHandler::$entityTypeManager protected property The entity type manager.
MenuLinkTreeHandler::getMenuLinkItemContent public function Get Menu Link Content entity content.
MenuLinkTreeHandler::getMenuLinkItemEntity protected function Get menu_link_content entity.
MenuLinkTreeHandler::processMenuLinkTree public function Process menu tree items. Add menu item content.
MenuLinkTreeHandler::__construct public function Constructs a new MenuLinkTreeHandler.