class ActiveLinkResponseFilter implements EventSubscriberInterface (View source)

Subscribes to filter HTML responses, to set the 'is-active' class on links.

Only for anonymous users; for authenticated users, the active-link asset library is loaded.

Properties

protected AccountInterface $currentUser

The current user.

protected CurrentPathStack $currentPath

The current path.

protected PathMatcherInterface $pathMatcher

The path matcher.

protected LanguageManagerInterface $languageManager

The language manager.

Methods

__construct(AccountInterface $current_user, CurrentPathStack $current_path, PathMatcherInterface $path_matcher, LanguageManagerInterface $language_manager)

Constructs a new ActiveLinkResponseFilter instance.

onResponse(ResponseEvent $event)

Sets the 'is-active' class on links.

static string
setLinkActiveClass(string $html_markup, string $current_path, bool $is_front, string $url_language, array $query)

Sets the "is-active" class on relevant links.

static 
getSubscribedEvents()

{@inheritdoc}

Details

__construct(AccountInterface $current_user, CurrentPathStack $current_path, PathMatcherInterface $path_matcher, LanguageManagerInterface $language_manager)

Constructs a new ActiveLinkResponseFilter instance.

Parameters

AccountInterface $current_user

The current user.

CurrentPathStack $current_path

The current path.

PathMatcherInterface $path_matcher

The path matcher.

LanguageManagerInterface $language_manager

The language manager.

onResponse(ResponseEvent $event)

Sets the 'is-active' class on links.

Parameters

ResponseEvent $event

The response event.

static string setLinkActiveClass(string $html_markup, string $current_path, bool $is_front, string $url_language, array $query)

Sets the "is-active" class on relevant links.

This is a PHP implementation of the drupal.active-link JavaScript library.

Once a future version of PHP supports parsing HTML5 properly (i.e. doesn't fail on https://www.drupal.org/comment/7938201#comment-7938201) then we can get rid of this manual parsing and use DOMDocument instead.

Parameters

string $html_markup

The HTML markup to update.

string $current_path

The system path of the currently active page.

bool $is_front

Whether the current page is the front page (which implies the current path might also be ).

string $url_language

The language code of the current URL.

array $query

The query string for the current URL.

Return Value

string

The updated HTML markup.

static getSubscribedEvents()

{@inheritdoc}