You are here

ContextMenuActiveTrail.php in Context 8

Same filename and directory in other branches
  1. 8.4 src/ContextMenuActiveTrail.php

Namespace

Drupal\context

File

src/ContextMenuActiveTrail.php
View source
<?php

namespace Drupal\context;

use Drupal\Core\Cache\CacheBackendInterface;
use Drupal\Core\Lock\LockBackendInterface;
use Drupal\Core\Menu\MenuActiveTrail;
use Drupal\Core\Menu\MenuLinkManagerInterface;
use Drupal\Core\Routing\RouteMatchInterface;
use Drupal\menu_link_content\Entity\MenuLinkContent;

/**
 * Extend the MenuActiveTrail class.
 */
class ContextMenuActiveTrail extends MenuActiveTrail {

  /**
   * @var \Drupal\context\ContextManager.
   */
  protected $contextManager;

  /**
   * {@inheritdoc}
   */
  public function __construct(MenuLinkManagerInterface $menu_link_manager, RouteMatchInterface $route_match, CacheBackendInterface $cache, LockBackendInterface $lock, ContextManager $context_manager) {
    parent::__construct($menu_link_manager, $route_match, $cache, $lock);
    $this->contextManager = $context_manager;
  }

  /**
   * {@inheritdoc}
   */
  public function getActiveLink($menu_name = NULL) {
    $found = parent::getActiveLink($menu_name);

    // Get active reaction of Menu type.
    foreach ($this->contextManager
      ->getActiveReactions('menu') as $reaction) {
      $menu_items = $reaction
        ->execute();
      foreach ($menu_items as $menu_link_content) {
        $menu = strtok($menu_link_content, ':');
        if ($menu == $menu_name) {
          $plugin_id = substr($menu_link_content, strlen($menu) + 1);
          return $this->menuLinkManager
            ->createInstance($plugin_id);
        }
      }
    }
    return $found;
  }

}

Classes

Namesort descending Description
ContextMenuActiveTrail Extend the MenuActiveTrail class.