ContextActiveTrail.php in Context Active Trail 8.2
File
src/ContextActiveTrail.php
View source
<?php
namespace Drupal\context_active_trail;
use Drupal\context\ContextManager;
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\Component\Plugin\Exception\PluginNotFoundException;
class ContextActiveTrail extends MenuActiveTrail {
const CACHE_TAG_BREADCRUMBS = "cache_tag_breadcrumbs";
protected $contextManager;
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;
$this->tags[] = 'context_active_trail';
}
public function getActiveLink($menu_name = NULL) {
foreach ($this->contextManager
->getActiveReactions('active_trail') as $reaction) {
if ($link_id = $reaction
->getLinkId()) {
try {
$instance = $this->menuLinkManager
->getInstance([
'id' => $link_id,
]);
} catch (PluginNotFoundException $e) {
\Drupal::logger('context_active_trail')
->error('Could not find the configured menu link to set active: @error', [
'@error' => $e
->getMessage(),
]);
return FALSE;
}
if (!empty($menu_name) && $instance
->getMenuName() != $menu_name) {
break;
}
return $instance;
}
}
return parent::getActiveLink($menu_name);
}
}