You are here

class MenuLinksActiveTrail in Thunder 6.2.x

Return the menu links of a menu with active trail.

Plugin annotation


@DataProducer(
  id = "menu_links_active_trail",
  name = @Translation("Menu links"),
  description = @Translation("Returns the menu links of a menu with active trail."),
  produces = @ContextDefinition("any",
    label = @Translation("Menu link"),
    multiple = TRUE
  ),
  consumes = {
    "menu" = @ContextDefinition("entity:menu",
      label = @Translation("Menu")
    ),
    "entity" = @ContextDefinition("entity",
      label = @Translation("The entity")
    ),
  }
)

Hierarchy

Expanded class hierarchy of MenuLinksActiveTrail

File

modules/thunder_gqls/src/Plugin/GraphQL/DataProducer/MenuLinksActiveTrail.php, line 38

Namespace

Drupal\thunder_gqls\Plugin\GraphQL\DataProducer
View source
class MenuLinksActiveTrail extends DataProducerPluginBase implements ContainerFactoryPluginInterface {
  use DependencySerializationTrait;

  /**
   * The menu link tree.
   *
   * @var \Drupal\Core\Menu\MenuLinkTreeInterface
   */
  protected $menuLinkTree;

  /**
   * The menu link tree.
   *
   * @var \Drupal\Core\Menu\MenuLinkManagerInterface
   */
  protected $menuLinkManager;

  /**
   * {@inheritdoc}
   *
   * @codeCoverageIgnore
   */
  public static function create(ContainerInterface $container, array $configuration, $pluginId, $pluginDefinition) {
    return new static($configuration, $pluginId, $pluginDefinition, $container
      ->get('menu.link_tree'), $container
      ->get('plugin.manager.menu.link'));
  }

  /**
   * MenuItems constructor.
   *
   * @param array $configuration
   *   The plugin configuration array.
   * @param string $pluginId
   *   The plugin id.
   * @param mixed $pluginDefinition
   *   The plugin definition.
   * @param \Drupal\Core\Menu\MenuLinkTreeInterface $menuLinkTree
   *   The menu link tree service.
   * @param \Drupal\Core\Menu\MenuLinkManagerInterface $menuLinkManager
   *   The menu link manager service.
   *
   * @codeCoverageIgnore
   */
  public function __construct(array $configuration, $pluginId, $pluginDefinition, MenuLinkTreeInterface $menuLinkTree, MenuLinkManagerInterface $menuLinkManager) {
    parent::__construct($configuration, $pluginId, $pluginDefinition);
    $this->menuLinkTree = $menuLinkTree;
    $this->menuLinkManager = $menuLinkManager;
  }

  /**
   * Resolver.
   *
   * @param \Drupal\system\MenuInterface $menu
   *   The menu interface.
   * @param \Drupal\Core\Entity\ContentEntityInterface $entity
   *   The content entity interface.
   *
   * @return array
   *   The menu links.
   *
   * @throws \Drupal\Core\Entity\EntityMalformedException
   */
  public function resolve(MenuInterface $menu, ContentEntityInterface $entity) {
    $parameters = new MenuTreeParameters();
    $links = $this->menuLinkManager
      ->loadLinksByRoute($entity
      ->toUrl()
      ->getRouteName(), $entity
      ->toUrl()
      ->getRouteParameters(), $menu
      ->id());
    $activeLink = reset($links);
    if ($activeLink) {
      $activeTrail = [
        '' => '',
      ];
      if ($parents = $this->menuLinkManager
        ->getParentIds($activeLink
        ->getPluginId())) {
        $activeTrail = $parents + $activeTrail;
      }
      $parameters
        ->setActiveTrail($activeTrail);
    }
    $tree = $this->menuLinkTree
      ->load($menu
      ->id(), $parameters);
    $manipulators = [
      [
        'callable' => 'menu.default_tree_manipulators:checkAccess',
      ],
      [
        'callable' => 'menu.default_tree_manipulators:generateIndexAndSort',
      ],
    ];
    return array_filter($this->menuLinkTree
      ->transform($tree, $manipulators), function (MenuLinkTreeElement $item) {
      return $item->link instanceof MenuLinkInterface && $item->link
        ->isEnabled();
    });
  }

}

Members

Namesort descending Modifiers Type Description Overrides
DependencySerializationTrait::$_entityStorages protected property
DependencySerializationTrait::$_serviceIds protected property
DependencySerializationTrait::__sleep public function 2
DependencySerializationTrait::__wakeup public function 2
MenuLinksActiveTrail::$menuLinkManager protected property The menu link tree.
MenuLinksActiveTrail::$menuLinkTree protected property The menu link tree.
MenuLinksActiveTrail::create public static function @codeCoverageIgnore Overrides ContainerFactoryPluginInterface::create
MenuLinksActiveTrail::resolve public function Resolver.
MenuLinksActiveTrail::__construct public function MenuItems constructor.