You are here

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

Expanded class hierarchy of LinkItemContentActiveTrailsCacheContext

1 string reference to 'LinkItemContentActiveTrailsCacheContext'
menu_item_extras.services.yml in ./menu_item_extras.services.yml
menu_item_extras.services.yml
1 service uses LinkItemContentActiveTrailsCacheContext
cache_context.menu_item_extras_link_item_content_active_trails in ./menu_item_extras.services.yml
Drupal\menu_item_extras\Cache\LinkItemContentActiveTrailsCacheContext

File

src/Cache/LinkItemContentActiveTrailsCacheContext.php, line 16

Namespace

Drupal\menu_item_extras\Cache
View 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

Namesort descending Modifiers Type Description Overrides
LinkItemContentActiveTrailsCacheContext::getCacheableMetadata public function Gets the cacheability metadata for the context based on the parameter value. Overrides CalculatedCacheContextInterface::getCacheableMetadata
LinkItemContentActiveTrailsCacheContext::getContext public function Returns the string representation of the cache context. Overrides CalculatedCacheContextInterface::getContext
LinkItemContentActiveTrailsCacheContext::getLabel public static function Returns the label of the cache context. Overrides CalculatedCacheContextInterface::getLabel