You are here

class CacheexcludeSubscriber in CacheExclude 8

Same name and namespace in other branches
  1. 1.0.x src/EventSubscriber/CacheexcludeSubscriber.php \Drupal\cacheexclude\EventSubscriber\CacheexcludeSubscriber

Class CacheexcludeSubscriber.

@package Drupal\cacheexclude.

Hierarchy

  • class \Drupal\cacheexclude\EventSubscriber\CacheexcludeSubscriber implements \Symfony\Component\EventDispatcher\EventSubscriberInterface

Expanded class hierarchy of CacheexcludeSubscriber

1 string reference to 'CacheexcludeSubscriber'
cacheexclude.services.yml in ./cacheexclude.services.yml
cacheexclude.services.yml
1 service uses CacheexcludeSubscriber
cacheexclude_event_subscriber in ./cacheexclude.services.yml
Drupal\cacheexclude\EventSubscriber\CacheexcludeSubscriber

File

src/EventSubscriber/CacheexcludeSubscriber.php, line 14

Namespace

Drupal\cacheexclude\EventSubscriber
View source
class CacheexcludeSubscriber implements EventSubscriberInterface {

  /**
   * {@inheritdoc}
   */
  public static function getSubscribedEvents() {
    $events[KernelEvents::REQUEST][] = [
      'disableCacheForPage',
    ];
    return $events;
  }

  /**
   * Subscriber Callback for the event.
   */
  public function disableCacheForPage() {

    // Get cacheexclude page configuration.
    $config = \Drupal::config('cacheexclude.settings');
    $pages = trim($config
      ->get('cacheexclude_list'));

    // If the current page is one we want to exclude from the cache,
    // disable page cache temporarily.
    if (!empty($pages)) {
      $current_path = \Drupal::service('path.current')
        ->getPath();
      $current_path_alias = \Drupal::service('path.alias_manager')
        ->getAliasByPath($current_path);
      $path_matches = \Drupal::service('path.matcher')
        ->matchPath($current_path, $pages);
      $alias_path_matches = \Drupal::service('path.matcher')
        ->matchPath($current_path_alias, $pages);
      if ($path_matches || $alias_path_matches) {

        // Disable page cache temporarily.
        \Drupal::service('page_cache_kill_switch')
          ->trigger();
        return;
      }
    }

    // Check if current node type is one we want to exclude from the cache.
    $node = \Drupal::routeMatch()
      ->getParameter('node');
    if ($node instanceof NodeInterface) {
      $node_type = $node
        ->getType();
    }
    $node_types = $config
      ->get('cacheexclude_node_types');
    if (!empty($node_types)) {
      $node_types = array_filter($node_types);
    }
    if (isset($node_type) && isset($node_types)) {
      if (in_array($node_type, $node_types)) {

        // Disable page cache temporarily.
        \Drupal::service('page_cache_kill_switch')
          ->trigger();
      }
    }
  }

}

Members

Namesort descending Modifiers Type Description Overrides
CacheexcludeSubscriber::disableCacheForPage public function Subscriber Callback for the event.
CacheexcludeSubscriber::getSubscribedEvents public static function Returns an array of event names this subscriber wants to listen to.