You are here

public function LinkItemContentActiveTrailsCacheContext::getContext in Menu Item Extras 8.2

Returns the string representation of the cache context.

A cache context service's name is used as a token (placeholder) cache key, and is then replaced with the string returned by this method.

Parameters

string|null $parameter: The parameter, or NULL to indicate all possible parameter values.

Return value

string The string representation of the cache context. When $parameter is NULL, a value representing all possible parameters must be generated.

Throws

\LogicException Thrown if the passed in parameter is invalid.

Overrides CalculatedCacheContextInterface::getContext

File

src/Cache/LinkItemContentActiveTrailsCacheContext.php, line 30

Class

LinkItemContentActiveTrailsCacheContext
Defines the MenuActiveTrailsCacheContext service.

Namespace

Drupal\menu_item_extras\Cache

Code

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';
  }
}