ContextMenuActiveTrail.php 1.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051
  1. <?php
  2. namespace Drupal\context;
  3. use Drupal\Core\Cache\CacheBackendInterface;
  4. use Drupal\Core\Lock\LockBackendInterface;
  5. use Drupal\Core\Menu\MenuActiveTrail;
  6. use Drupal\Core\Menu\MenuLinkManagerInterface;
  7. use Drupal\Core\Routing\RouteMatchInterface;
  8. use Drupal\menu_link_content\Entity\MenuLinkContent;
  9. /**
  10. * Extend the MenuActiveTrail class.
  11. */
  12. class ContextMenuActiveTrail extends MenuActiveTrail {
  13. /**
  14. * @var \Drupal\context\ContextManager.
  15. */
  16. protected $contextManager;
  17. /**
  18. * {@inheritdoc}
  19. */
  20. public function __construct(MenuLinkManagerInterface $menu_link_manager, RouteMatchInterface $route_match, CacheBackendInterface $cache, LockBackendInterface $lock, ContextManager $context_manager) {
  21. parent::__construct($menu_link_manager, $route_match, $cache, $lock);
  22. $this->contextManager = $context_manager;
  23. }
  24. /**
  25. * {@inheritdoc}
  26. */
  27. public function getActiveLink($menu_name = NULL) {
  28. $found = parent::getActiveLink($menu_name);
  29. // Get active reaction of Menu type.
  30. foreach($this->contextManager->getActiveReactions('menu') as $reaction) {
  31. $menu_items = $reaction->execute();
  32. foreach ($menu_items as $menu_link_content) {
  33. $menu = strtok($menu_link_content, ':');
  34. if ($menu == $menu_name) {
  35. $plugin_id = substr($menu_link_content, strlen($menu) + 1);
  36. return $this->menuLinkManager->createInstance($plugin_id);
  37. }
  38. }
  39. }
  40. return $found;
  41. }
  42. }