Drupal → Отключить в рантайме определённую фронтэнд-библиотеку (library)

30.01.2018

Пример отключения библиотеки core/drupal.active-link на страницах нод:

modulename.services.yml:

services:
  modulename.subscriber:
    class: Drupal\modulename\EventSubscriber\ModulenameSubscriber
    tags:
      - { name: event_subscriber }

src/EventSubscriber/ModulenameSubscriber.php:

<?php

namespace Drupal\modulename\EventSubscriber;

use Symfony\Component\HttpKernel\KernelEvents;
use Symfony\Component\HttpKernel\Event\FilterResponseEvent;
use Symfony\Component\EventDispatcher\EventSubscriberInterface;

class ModulenameSubscriber implements EventSubscriberInterface {

  public static function getSubscribedEvents() {
    $events[KernelEvents::RESPONSE][] = ['onResponse', 10];
    return $events;
  }

  public function onResponse(FilterResponseEvent $event) {
    // @TODO Use DI without static class
    if (\Drupal::routeMatch()->getRouteName() == 'entity.node.canonical') {
      $response = $event->getResponse();
      if (method_exists($response, 'getAttachments')) {
        $attachments = $response->getAttachments();
        $attachments['library'] = array_diff($attachments['library'], ['core/drupal.active-link']);
        $response->setAttachments($attachments);
      }
    }
  }

}
Написанное актуально для
Drupal 8
Похожие записи

Комментарии

И еще добавить dependency injection в сервис:
modulename.services.yml:

services:
  modulename.subscriber:
    class: Drupal\modulename\EventSubscriber\ModulenameSubscriber
    arguments: ['@current_route_match']
    tags:
      - { name: event_subscriber }

src/EventSubscriber/ModulenameSubscriber.php:

<?php

namespace Drupal\modulename\EventSubscriber;

use Symfony\Component\EventDispatcher\EventSubscriberInterface;
use Symfony\Component\EventDispatcher\Event;
use Drupal\Core\Routing\CurrentRouteMatch;
use Symfony\Component\HttpKernel\KernelEvents;

/**
 * Class ModulenameSubscriber.
 */
class ModulenameSubscriber implements EventSubscriberInterface {

  /**
   * Drupal\Core\Routing\CurrentRouteMatch definition.
   *
   * @var \Drupal\Core\Routing\CurrentRouteMatch
   */
  protected $currentRouteMatch;

  /**
   * Constructs a ModulenameSubscriber object.
   */
  public function __construct(CurrentRouteMatch $current_route_match) {
    $this->currentRouteMatch = $current_route_match;
  }

  /**
   * {@inheritdoc}
   */
  public static function getSubscribedEvents() {
    $events[KernelEvents::RESPONSE] = ['onResponse'];

    return $events;
  }

  /**
   * This method is called whenever the kernel.response event is
   * dispatched.
   *
   * @param GetResponseEvent $event
   */
  public function onResponse(Event $event) {
    if ($this->currentRouteMatch->getRouteName() == 'entity.node.canonical') {
      $response = $event->getResponse();
      if (method_exists($response, 'getAttachments')) {
        $attachments = $response->getAttachments();
        $attachments['library'] = array_diff($attachments['library'], ['core/drupal.active-link']);
        $response->setAttachments($attachments);
      }
    }
  }
}

Добавить комментарий