function ActiveLinkResponseFilter::onResponse

Same name and namespace in other branches
  1. 9 core/lib/Drupal/Core/EventSubscriber/ActiveLinkResponseFilter.php \Drupal\Core\EventSubscriber\ActiveLinkResponseFilter::onResponse()
  2. 8.9.x core/lib/Drupal/Core/EventSubscriber/ActiveLinkResponseFilter.php \Drupal\Core\EventSubscriber\ActiveLinkResponseFilter::onResponse()
  3. 10 core/lib/Drupal/Core/EventSubscriber/ActiveLinkResponseFilter.php \Drupal\Core\EventSubscriber\ActiveLinkResponseFilter::onResponse()

Sets the 'is-active' class on links.

Parameters

\Symfony\Component\HttpKernel\Event\ResponseEvent $event: The response event.

File

core/lib/Drupal/Core/EventSubscriber/ActiveLinkResponseFilter.php, line 81

Class

ActiveLinkResponseFilter
Subscribes to filter HTML responses, to set attributes on active links.

Namespace

Drupal\Core\EventSubscriber

Code

public function onResponse(ResponseEvent $event) {
    $response = $event->getResponse();
    // Only care about HTML responses.
    if (stripos($response->headers
        ->get('Content-Type', ''), 'text/html') === FALSE) {
        return;
    }
    // For authenticated users, the 'is-active' class is set in JavaScript.
    // @see system_page_attachments()
    if ($this->currentUser
        ->isAuthenticated()) {
        return;
    }
    // If content is FALSE, assume the response does not support the
    // setContent() method and skip it, for example,
    // \Symfony\Component\HttpFoundation\BinaryFileResponse.
    $content = $response->getContent();
    if ($content !== FALSE) {
        $response->setContent(static::setLinkActiveClass($content, ltrim($this->currentPath
            ->getPath(), '/'), $this->pathMatcher
            ->isFrontPage(), $this->languageManager
            ->getCurrentLanguage(LanguageInterface::TYPE_URL)
            ->getId(), $event->getRequest()->query
            ->all()));
    }
}

Buggy or inaccurate documentation? Please file an issue. Need support? Need help programming? Connect with the Drupal community.