class LinkItemContentActiveTrailsCacheContext in Menu Item Extras 8.2
Defines the MenuActiveTrailsCacheContext service.
This class is container-aware to avoid initializing the 'menu.active_trails' service (and its dependencies) when it is not necessary.
Hierarchy
- class \Drupal\menu_item_extras\Cache\LinkItemContentActiveTrailsCacheContext implements \Symfony\Component\DependencyInjection\ContainerAwareInterface, CalculatedCacheContextInterface uses \Symfony\Component\DependencyInjection\ContainerAwareTrait
Expanded class hierarchy of LinkItemContentActiveTrailsCacheContext
1 string reference to 'LinkItemContentActiveTrailsCacheContext'
1 service uses LinkItemContentActiveTrailsCacheContext
File
- src/
Cache/ LinkItemContentActiveTrailsCacheContext.php, line 16
Namespace
Drupal\menu_item_extras\CacheView source
class LinkItemContentActiveTrailsCacheContext implements CalculatedCacheContextInterface, ContainerAwareInterface {
use ContainerAwareTrait;
/**
* {@inheritdoc}
*/
public static function getLabel() {
return t("Active menu trail");
}
/**
* {@inheritdoc}
*/
public function getContext($parameter = NULL) {
list($menu_name, $menu_link_id) = explode(':', $parameter);
if (!$menu_name) {
throw new \LogicException('No menu name provided for menu.active_trails cache context.');
}
$active_trail_manager = $this->container
->get('menu.active_trail');
$active_trail_link = $active_trail_manager
->getActiveLink($menu_name);
$active_trail_ids = array_values($active_trail_manager
->getActiveTrailIds($menu_name));
if ($active_trail_link && $active_trail_link
->getDerivativeId() == $menu_link_id) {
return 'link_item_content.active.' . $menu_link_id;
}
elseif (in_array('menu_link_content:' . $menu_link_id, $active_trail_ids)) {
return 'link_item_content.active_trail';
}
else {
return 'link_item_content.inactive';
}
}
/**
* {@inheritdoc}
*/
public function getCacheableMetadata($parameter = NULL) {
list($menu_name, ) = explode(':', $parameter);
if (!$menu_name) {
throw new \LogicException('No menu name provided for menu.active_trails cache context.');
}
$cacheable_metadata = new CacheableMetadata();
return $cacheable_metadata
->setCacheTags([
"config:system.menu.{$menu_name}",
]);
}
}
Members
Name | Modifiers | Type | Description | Overrides |
---|---|---|---|---|
LinkItemContentActiveTrailsCacheContext:: |
public | function |
Gets the cacheability metadata for the context based on the parameter value. Overrides CalculatedCacheContextInterface:: |
|
LinkItemContentActiveTrailsCacheContext:: |
public | function |
Returns the string representation of the cache context. Overrides CalculatedCacheContextInterface:: |
|
LinkItemContentActiveTrailsCacheContext:: |
public static | function |
Returns the label of the cache context. Overrides CalculatedCacheContextInterface:: |