You are here

class MenuLinkTree in Colossal Menu 2.x

Same name and namespace in other branches
  1. 8 src/Menu/MenuLinkTree.php \Drupal\colossal_menu\Menu\MenuLinkTree

Implements the loading, transforming and rendering of menu link trees.

Hierarchy

Expanded class hierarchy of MenuLinkTree

1 string reference to 'MenuLinkTree'
colossal_menu.services.yml in ./colossal_menu.services.yml
colossal_menu.services.yml
1 service uses MenuLinkTree
colossal_menu.link_tree in ./colossal_menu.services.yml
Drupal\colossal_menu\Menu\MenuLinkTree

File

src/Menu/MenuLinkTree.php, line 17

Namespace

Drupal\colossal_menu\Menu
View source
class MenuLinkTree extends CoreMenuLinkTree {

  /**
   * Entity Type Manager.
   *
   * @var \Drupal\Core\Entity\EntityTypeManagerInterface
   */
  protected $entityTypeManager;

  /**
   * Constructs a \Drupal\Core\Menu\MenuLinkTree object.
   *
   * @param \Drupal\Core\Menu\MenuTreeStorageInterface $tree_storage
   *   The menu link tree storage.
   * @param \Drupal\Core\Routing\RouteProviderInterface $route_provider
   *   The route provider to load routes by name.
   * @param \Drupal\Core\Menu\MenuActiveTrailInterface $menu_active_trail
   *   The active menu trail service.
   * @param \Drupal\Core\Controller\ControllerResolverInterface $controller_resolver
   *   The controller resolver.
   * @param \Drupal\Core\Entity\EntityTypeManagerInterface $entity_type_manager
   *   The EntityTypeManager service.
   */
  public function __construct(MenuTreeStorageInterface $tree_storage, RouteProviderInterface $route_provider, MenuActiveTrailInterface $menu_active_trail, ControllerResolverInterface $controller_resolver, EntityTypeManagerInterface $entity_type_manager) {
    $this->treeStorage = $tree_storage;
    $this->routeProvider = $route_provider;
    $this->menuActiveTrail = $menu_active_trail;
    $this->controllerResolver = $controller_resolver;
    $this->entityTypeManager = $entity_type_manager;
  }

  /**
   * {@inheritdoc}
   */
  protected function createInstances(array $data_tree) {
    $tree = [];
    foreach ($data_tree as $key => $element) {
      $subtree = $this
        ->createInstances($element['subtree']);

      // Build a MenuLinkTreeElement out of the menu tree link definition:
      // transform the tree link definition into a link definition and store
      // tree metadata.
      $tree[$key] = new MenuLinkTreeElement($element['link'], (bool) $element['has_children'], (int) $element['depth'], (bool) $element['in_active_trail'], $subtree);
      if ($tree[$key]->link instanceof AccessibleInterface) {
        $tree[$key]->access = $tree[$key]->link
          ->access('view', NULL, TRUE);
      }
    }
    return $tree;
  }

  /**
   * {@inheritdoc}
   */
  public function build(array $tree) {
    $build = parent::build($tree);

    // Use a custom theme.
    if (isset($build['#theme'])) {
      $build['#theme'] = 'colossal_menu__' . strtr($build['#menu_name'], '-', '_');
    }
    if (!empty($build['#items'])) {
      $this
        ->addItemContent($build['#items']);
    }
    return $build;
  }

  /**
   * Add the Link Content and add a no link variable.
   *
   * @param array $tree
   *   Tree of links.
   */
  protected function addItemContent(array &$tree) {
    foreach ($tree as &$item) {

      /** @var \Drupal\colossal_menu\LinkInterface $link */
      $link = $item['original_link'];
      $item['show_title'] = $link
        ->showTitle();
      $item['identifier'] = $link
        ->id();
      $item['has_link'] = TRUE;
      if (!$link
        ->isExternal() && $link
        ->getRouteName() == '<none>') {
        $item['has_link'] = FALSE;
      }
      $item['content'] = $this->entityTypeManager
        ->getViewBuilder($link
        ->getEntityTypeId())
        ->view($link, 'default');
      if (!empty($item['below'])) {
        $this
          ->addItemContent($item['below']);
      }
    }
  }

}

Members

Namesort descending Modifiers Type Description Overrides
MenuLinkTree::$controllerResolver protected property The controller resolver.
MenuLinkTree::$entityTypeManager protected property Entity Type Manager.
MenuLinkTree::$menuActiveTrail protected property The active menu trail service.
MenuLinkTree::$menuLinkManager protected property The menu link plugin manager.
MenuLinkTree::$routeProvider protected property The route provider to load routes by name.
MenuLinkTree::$treeStorage protected property The menu link tree storage.
MenuLinkTree::addItemContent protected function Add the Link Content and add a no link variable.
MenuLinkTree::build public function Builds a renderable array from a menu tree. Overrides MenuLinkTree::build
MenuLinkTree::buildItems protected function Builds the #items property for a menu tree's renderable array.
MenuLinkTree::createInstances protected function Returns a tree containing of MenuLinkTreeElement based upon tree data. Overrides MenuLinkTree::createInstances
MenuLinkTree::getCurrentRouteMenuTreeParameters public function Gets the link tree parameters for rendering a specific menu. Overrides MenuLinkTreeInterface::getCurrentRouteMenuTreeParameters
MenuLinkTree::getExpanded public function Finds expanded links in a menu given a set of possible parents. Overrides MenuLinkTreeInterface::getExpanded
MenuLinkTree::getSubtreeHeight public function Finds the height of a subtree rooted by of the given ID. Overrides MenuLinkTreeInterface::getSubtreeHeight
MenuLinkTree::load public function Loads a menu tree with a menu link plugin instance at each element. Overrides MenuLinkTreeInterface::load
MenuLinkTree::maxDepth public function Returns the maximum depth of tree that is supported. Overrides MenuLinkTreeInterface::maxDepth
MenuLinkTree::transform public function Applies menu link tree manipulators to transform the given tree. Overrides MenuLinkTreeInterface::transform
MenuLinkTree::__construct public function Constructs a \Drupal\Core\Menu\MenuLinkTree object. Overrides MenuLinkTree::__construct