class MenuActiveTrail extends CacheCollector implements MenuActiveTrailInterface (View source)

Provides the default implementation of the active menu trail service.

It uses the current route name and route parameters to compare with the ones of the menu links.

Properties

protected string $cid

The cache id that is used for the cache entry.

from  CacheCollector
protected array $tags

A list of tags that are used for the cache entry.

from  CacheCollector
protected CacheBackendInterface $cache

The cache backend that should be used.

from  CacheCollector
protected LockBackendInterface $lock

The lock backend that should be used.

from  CacheCollector
protected array $keysToPersist

An array of keys to add to the cache on service termination.

from  CacheCollector
protected array $keysToRemove

An array of keys to remove from the cache on service termination.

from  CacheCollector
protected array $storage

Storage for the data itself.

from  CacheCollector
protected int $cacheCreated

Stores the cache creation time.

from  CacheCollector
protected bool $cacheInvalidated

Flag that indicates of the cache has been invalidated.

from  CacheCollector
protected bool $cacheLoaded

Indicates if the collected cache was already loaded.

from  CacheCollector
protected MenuLinkManagerInterface $menuLinkManager

The menu link plugin manager.

protected RouteMatchInterface $routeMatch

The route match object for the current page.

Methods

__construct(MenuLinkManagerInterface $menu_link_manager, RouteMatchInterface $route_match, CacheBackendInterface $cache, LockBackendInterface $lock)

Constructs a \Drupal\Core\Menu\MenuActiveTrail object.

string
getCid()

Gets the cache ID.

has(string $key)

Returns whether data exists for this key.

mixed
get(string $key)

Gets value from the cache.

set(string $key, mixed $value)

Implements \Drupal\Core\Cache\CacheCollectorInterface::set().

delete(string $key)

Deletes the element.

persist(string $key, bool $persist = TRUE)

Flags an offset value to be written to the persistent cache.

mixed
resolveCacheMiss($menu_name)

Resolves a cache miss.

updateCache(bool $lock = TRUE)

Writes a value to the persistent cache immediately.

string
normalizeLockName(string $cid)

Normalizes a cache ID in order to comply with database limitations.

reset()

Resets the local cache.

clear()

Clears the collected cache entry.

destruct()

Performs destruct operations.

lazyLoadCache()

Loads the cache if not already done.

invalidateCache()

Invalidate the cache.

array
getActiveTrailIds(string|null $menu_name)

Gets the active trail IDs of the specified menu tree.

doGetActiveTrailIds($menu_name)

Helper method for ::getActiveTrailIds().

getActiveLink(string|null $menu_name = NULL)

Fetches a menu link which matches the route name, parameters and menu name.

Details

__construct(MenuLinkManagerInterface $menu_link_manager, RouteMatchInterface $route_match, CacheBackendInterface $cache, LockBackendInterface $lock)

Constructs a \Drupal\Core\Menu\MenuActiveTrail object.

Parameters

MenuLinkManagerInterface $menu_link_manager

The menu link plugin manager.

RouteMatchInterface $route_match

A route match object for finding the active link.

CacheBackendInterface $cache

The cache backend.

LockBackendInterface $lock

The lock backend.

protected string getCid()

Gets the cache ID.

Return Value

string

See also

\Drupal\Core\Menu\::getActiveTrailIds()

has(string $key)

Returns whether data exists for this key.

Parameters

string $key

Key that identifies the data.

mixed get(string $key)

Gets value from the cache.

Parameters

string $key

Key that identifies the data.

Return Value

mixed

The corresponding cache data.

set(string $key, mixed $value)

Implements \Drupal\Core\Cache\CacheCollectorInterface::set().

This is not persisted by default. In practice this means that setting a value will only apply while the object is in scope and will not be written back to the persistent cache. This follows a similar pattern to static vs. persistent caching in procedural code. Extending classes may wish to alter this behavior, for example by adding a call to persist().

Parameters

string $key

Key that identifies the data.

mixed $value

The data to be set.

delete(string $key)

Deletes the element.

It depends on the specific case and implementation whether this has a permanent effect or if it just affects the current request.

Parameters

string $key

Key that identifies the data.

protected persist(string $key, bool $persist = TRUE)

Flags an offset value to be written to the persistent cache.

Parameters

string $key

The key that was requested.

bool $persist

(optional) Whether the offset should be persisted or not, defaults to TRUE. When called with $persist = FALSE the offset will be unflagged so that it will not be written at the end of the request.

protected mixed resolveCacheMiss($menu_name)

Resolves a cache miss.

When an offset is not found in the object, this is treated as a cache miss. This method allows classes using this implementation to look up the actual value and allow it to be cached.

Parameters

$menu_name

Return Value

mixed

The value of the offset, or NULL if no value was found.

See also

\Drupal\Core\Menu\::getActiveTrailIds()

protected updateCache(bool $lock = TRUE)

Writes a value to the persistent cache immediately.

Parameters

bool $lock

(optional) Whether to acquire a lock before writing to cache. Defaults to TRUE.

protected string normalizeLockName(string $cid)

Normalizes a cache ID in order to comply with database limitations.

Parameters

string $cid

The passed in cache ID.

Return Value

string

An ASCII-encoded cache ID that is at most 255 characters long.

reset()

Resets the local cache.

Does not clear the persistent cache.

clear()

Clears the collected cache entry.

destruct()

Performs destruct operations.

protected lazyLoadCache()

Loads the cache if not already done.

protected invalidateCache()

Invalidate the cache.

array getActiveTrailIds(string|null $menu_name)

Gets the active trail IDs of the specified menu tree.

Parameters

string|null $menu_name

(optional) The menu name of the requested tree. If omitted, all menu trees will be searched.

Return Value

array

An array containing the active trail: a list of plugin IDs.

See also

\Drupal\Core\Menu\::get()
CacheCollectorInterface
CacheCollector

protected doGetActiveTrailIds($menu_name)

Helper method for ::getActiveTrailIds().

Parameters

$menu_name

Fetches a menu link which matches the route name, parameters and menu name.

Parameters

string|null $menu_name

(optional) The menu within which to find the active link. If omitted, all menus will be searched.

Return Value

MenuLinkInterface|null

The menu link for the given route name, parameters and menu, or NULL if there is no matching menu link or the current user cannot access the current page (i.e. we have a 403 response).