class MenuTrailByPathActiveTrail in Menu Trail By Path 8
Overrides the class for the file entity normalizer from HAL.
Hierarchy
- class \Drupal\Core\Cache\CacheCollector implements CacheCollectorInterface, DestructableInterface
- class \Drupal\Core\Menu\MenuActiveTrail implements MenuActiveTrailInterface
- class \Drupal\menu_trail_by_path\MenuTrailByPathActiveTrail
- class \Drupal\Core\Menu\MenuActiveTrail implements MenuActiveTrailInterface
Expanded class hierarchy of MenuTrailByPathActiveTrail
2 files declare their use of MenuTrailByPathActiveTrail
- MenuTrailByPathActiveTrailHtmlClassTest.php in tests/
src/ Functional/ MenuTrailByPathActiveTrailHtmlClassTest.php - MenuTrailByPathSettingsForm.php in src/
Form/ MenuTrailByPathSettingsForm.php
File
- src/
MenuTrailByPathActiveTrail.php, line 20
Namespace
Drupal\menu_trail_by_pathView source
class MenuTrailByPathActiveTrail extends MenuActiveTrail {
/**
* Disabled menu trail.
*/
const MENU_TRAIL_DISABLED = 'disabled';
/**
* Menu trail is created using this module.
*/
const MENU_TRAIL_PATH = 'path';
/**
* Menu trail is created by Drupal Core.
*/
const MENU_TRAIL_CORE = 'core';
/**
* @var \Drupal\menu_trail_by_path\Path\PathHelperInterface
*/
protected $pathHelper;
/**
* @var \Drupal\menu_trail_by_path\Menu\MenuHelperInterface
*/
protected $menuHelper;
/**
* @var \Drupal\Core\Routing\RequestContext
*/
protected $context;
/**
* @var \Drupal\Core\Language\LanguageManagerInterface
*/
protected $languageManager;
/**
* The configuration object.
*
* @var \Drupal\Core\Config\ImmutableConfig
*/
protected $config;
/**
* MenuTrailByPathActiveTrail constructor.
* @param \Drupal\Core\Menu\MenuLinkManagerInterface $menu_link_manager
* @param \Drupal\Core\Routing\RouteMatchInterface $route_match
* @param \Drupal\Core\Cache\CacheBackendInterface $cache
* @param \Drupal\Core\Lock\LockBackendInterface $lock
* @param \Drupal\menu_trail_by_path\Path\PathHelperInterface $path_helper
* @param \Drupal\menu_trail_by_path\Menu\MenuHelperInterface $menu_helper
* @param \Drupal\Core\Routing\RequestContext $context
* @param \Drupal\Core\Language\LanguageManagerInterface $languageManager
* @param \Drupal\Core\Config\ConfigFactoryInterface $config_factory
*/
public function __construct(MenuLinkManagerInterface $menu_link_manager, RouteMatchInterface $route_match, CacheBackendInterface $cache, LockBackendInterface $lock, PathHelperInterface $path_helper, MenuHelperInterface $menu_helper, RequestContext $context, LanguageManagerInterface $languageManager, ConfigFactoryInterface $config_factory) {
parent::__construct($menu_link_manager, $route_match, $cache, $lock);
$this->pathHelper = $path_helper;
$this->menuHelper = $menu_helper;
$this->context = $context;
$this->languageManager = $languageManager;
$this->config = $config_factory
->get('menu_trail_by_path.settings');
}
/**
* {@inheritdoc}
*
* @see https://www.drupal.org/node/2824594
*/
protected function getCid() {
if (!isset($this->cid)) {
return parent::getCid() . ":langcode:{$this->languageManager->getCurrentLanguage()->getId()}:pathinfo:{$this->context->getPathInfo()}";
}
return $this->cid;
}
/**
* {@inheritdoc}
*/
protected function doGetActiveTrailIds($menu_name) {
// Parent ids; used both as key and value to ensure uniqueness.
// We always want all the top-level links with parent == ''.
$active_trail = array(
'' => '',
);
$entity = Menu::load($menu_name);
if (!$entity) {
return $active_trail;
}
// Build an active trail based on the trail source setting.
$trail_source = $entity
->getThirdPartySetting('menu_trail_by_path', 'trail_source') ?: $this->config
->get('trail_source');
if ($trail_source == static::MENU_TRAIL_CORE) {
return parent::doGetActiveTrailIds($menu_name);
}
elseif ($trail_source == static::MENU_TRAIL_DISABLED) {
return $active_trail;
}
// If a link in the given menu indeed matches the path, then use it to
// complete the active trail.
if ($active_link = $this
->getActiveTrailLink($menu_name)) {
if ($parents = $this->menuLinkManager
->getParentIds($active_link
->getPluginId())) {
$active_trail = $parents + $active_trail;
}
}
return $active_trail;
}
/**
* Fetches the deepest, heaviest menu link which matches the deepest trail path url.
*
* @param string $menu_name
* The menu within which to find the active trail link.
*
* @return \Drupal\Core\Menu\MenuLinkInterface|NULL
* The menu link for the given menu, or NULL if there is no matching menu link.
*/
public function getActiveTrailLink($menu_name) {
$menu_links = $this->menuHelper
->getMenuLinks($menu_name);
$trail_urls = $this->pathHelper
->getUrls();
foreach (array_reverse($trail_urls) as $trail_url) {
foreach (array_reverse($menu_links) as $menu_link) {
/* @var $menu_link \Drupal\Core\Menu\MenuLinkInterface */
/* @var $trail_url \Drupal\Core\Url */
if ($menu_link
->getUrlObject()
->toString() == $trail_url
->toString()) {
return $menu_link;
}
}
}
return NULL;
}
}
Members
Name | Modifiers | Type | Description | Overrides |
---|---|---|---|---|
CacheCollector:: |
protected | property | The cache backend that should be used. | 1 |
CacheCollector:: |
protected | property | Stores the cache creation time. | |
CacheCollector:: |
protected | property | Flag that indicates of the cache has been invalidated. | |
CacheCollector:: |
protected | property | Indicates if the collected cache was already loaded. | |
CacheCollector:: |
protected | property | The cache id that is used for the cache entry. | |
CacheCollector:: |
protected | property | An array of keys to add to the cache on service termination. | |
CacheCollector:: |
protected | property | An array of keys to remove from the cache on service termination. | |
CacheCollector:: |
protected | property | The lock backend that should be used. | 1 |
CacheCollector:: |
protected | property | Storage for the data itself. | |
CacheCollector:: |
protected | property | A list of tags that are used for the cache entry. | |
CacheCollector:: |
public | function |
Clears the collected cache entry. Overrides CacheCollectorInterface:: |
1 |
CacheCollector:: |
public | function |
Deletes the element. Overrides CacheCollectorInterface:: |
|
CacheCollector:: |
public | function |
Performs destruct operations. Overrides DestructableInterface:: |
|
CacheCollector:: |
public | function |
Gets value from the cache. Overrides CacheCollectorInterface:: |
2 |
CacheCollector:: |
public | function |
Returns whether data exists for this key. Overrides CacheCollectorInterface:: |
1 |
CacheCollector:: |
protected | function | Invalidate the cache. | |
CacheCollector:: |
protected | function | Loads the cache if not already done. | 1 |
CacheCollector:: |
protected | function | Normalizes a cache ID in order to comply with database limitations. | |
CacheCollector:: |
protected | function | Flags an offset value to be written to the persistent cache. | |
CacheCollector:: |
public | function |
Resets the local cache. Overrides CacheCollectorInterface:: |
1 |
CacheCollector:: |
public | function |
Implements \Drupal\Core\Cache\CacheCollectorInterface::set(). Overrides CacheCollectorInterface:: |
1 |
CacheCollector:: |
protected | function | Writes a value to the persistent cache immediately. | 1 |
MenuActiveTrail:: |
protected | property | The menu link plugin manager. | |
MenuActiveTrail:: |
protected | property | The route match object for the current page. | |
MenuActiveTrail:: |
public | function |
Fetches a menu link which matches the route name, parameters and menu name. Overrides MenuActiveTrailInterface:: |
|
MenuActiveTrail:: |
public | function |
This implementation caches all active trail IDs per route match for *all*
menus whose active trails are calculated on that page. This ensures 1 cache
get for all active trails per page load, rather than N. Overrides MenuActiveTrailInterface:: |
|
MenuActiveTrail:: |
protected | function |
Overrides CacheCollector:: |
|
MenuTrailByPathActiveTrail:: |
protected | property | The configuration object. | |
MenuTrailByPathActiveTrail:: |
protected | property | ||
MenuTrailByPathActiveTrail:: |
protected | property | ||
MenuTrailByPathActiveTrail:: |
protected | property | ||
MenuTrailByPathActiveTrail:: |
protected | property | ||
MenuTrailByPathActiveTrail:: |
protected | function |
Helper method for ::getActiveTrailIds(). Overrides MenuActiveTrail:: |
|
MenuTrailByPathActiveTrail:: |
public | function | Fetches the deepest, heaviest menu link which matches the deepest trail path url. | |
MenuTrailByPathActiveTrail:: |
protected | function |
Overrides MenuActiveTrail:: |
|
MenuTrailByPathActiveTrail:: |
constant | Menu trail is created by Drupal Core. | ||
MenuTrailByPathActiveTrail:: |
constant | Disabled menu trail. | ||
MenuTrailByPathActiveTrail:: |
constant | Menu trail is created using this module. | ||
MenuTrailByPathActiveTrail:: |
public | function |
MenuTrailByPathActiveTrail constructor. Overrides MenuActiveTrail:: |