class CacheexcludeSubscriber in CacheExclude 8
Same name and namespace in other branches
- 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'
1 service uses CacheexcludeSubscriber
File
- src/
EventSubscriber/ CacheexcludeSubscriber.php, line 14
Namespace
Drupal\cacheexclude\EventSubscriberView 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
Name | Modifiers | Type | Description | Overrides |
---|---|---|---|---|
CacheexcludeSubscriber:: |
public | function | Subscriber Callback for the event. | |
CacheexcludeSubscriber:: |
public static | function | Returns an array of event names this subscriber wants to listen to. |