You are here

public function CacheexcludeSubscriber::disableCacheForPage in CacheExclude 8

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

Subscriber Callback for the event.

File

src/EventSubscriber/CacheexcludeSubscriber.php, line 27

Class

CacheexcludeSubscriber
Class CacheexcludeSubscriber.

Namespace

Drupal\cacheexclude\EventSubscriber

Code

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();
    }
  }
}