Skip to content
Snippets Groups Projects
Commit cbfa1926 authored by project update bot's avatar project update bot Committed by Ken Rickard
Browse files

Issue #3428332 by project update bot, agentrickard, sleitner: Automated Drupal...

Issue #3428332 by project update bot, agentrickard, sleitner: Automated Drupal 11 compatibility fixes for cacheexclude
parent d36b2b0f
No related branches found
No related tags found
1 merge request!6Automated Project Update Bot fixes
Pipeline #436094 passed
cacheexclude
......@@ -2,5 +2,7 @@ name: Cache Exclude
type: module
description: 'Exclude certain pages from being cached.'
version: VERSION
core_version_requirement: ^9 || ^10
core_version_requirement: ^9 || ^10 || ^11
configure: cacheexclude.settings
dependencies:
- drupal:path_alias
services:
cacheexclude_event_subscriber:
class: Drupal\cacheexclude\EventSubscriber\CacheexcludeSubscriber
arguments: ['@config.factory', '@path.matcher', '@path.current', '@path_alias.manager', '@current_route_match', '@page_cache_kill_switch']
tags:
- {name: event_subscriber}
......@@ -4,15 +4,88 @@ namespace Drupal\cacheexclude\EventSubscriber;
use Symfony\Component\HttpKernel\KernelEvents;
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
use Drupal\Core\Config\ConfigFactoryInterface;
use Drupal\Core\Path\PathMatcherInterface;
use Drupal\Core\Path\CurrentPathStack;
use Drupal\path_alias\AliasManagerInterface;
use Drupal\Core\Routing\RouteMatchInterface;
use Drupal\Core\PageCache\ResponsePolicy\KillSwitch;
use Drupal\node\NodeInterface;
/**
* Class CacheexcludeSubscriber.
* Event subscriber for excluding pages from the cache.
*
* @package Drupal\cacheexclude.
*/
class CacheexcludeSubscriber implements EventSubscriberInterface {
/**
* The config factory.
*
* @var \Drupal\Core\Config\ConfigFactoryInterface
*/
protected $configFactory;
/**
* The path matcher.
*
* @var \Drupal\Core\Path\PathMatcherInterface
*/
protected $pathMatcher;
/**
* The current path stack.
*
* @var \Drupal\Core\Path\CurrentPathStack
*/
protected $currentPath;
/**
* The alias manager.
*
* @var \Drupal\path_alias\AliasManagerInterface
*/
protected $aliasManager;
/**
* The route match.
*
* @var \Drupal\Core\Routing\RouteMatchInterface
*/
protected $routeMatch;
/**
* The page cache kill switch.
*
* @var \Drupal\Core\PageCache\ResponsePolicy\KillSwitch
*/
protected $killSwitch;
/**
* Constructs a new CacheexcludeSubscriber.
*
* @param \Drupal\Core\Config\ConfigFactoryInterface $config_factory
* The config factory.
* @param \Drupal\Core\Path\PathMatcherInterface $path_matcher
* The path matcher.
* @param \Drupal\Core\Path\CurrentPathStack $current_path
* The current path stack.
* @param \Drupal\path_alias\AliasManagerInterface $alias_manager
* The alias manager.
* @param \Drupal\Core\Routing\RouteMatchInterface $route_match
* The route match.
* @param \Drupal\Core\PageCache\ResponsePolicy\KillSwitch $kill_switch
* The page cache kill switch.
*/
public function __construct(ConfigFactoryInterface $config_factory, PathMatcherInterface $path_matcher, CurrentPathStack $current_path, AliasManagerInterface $alias_manager, RouteMatchInterface $route_match, KillSwitch $kill_switch) {
$this->configFactory = $config_factory;
$this->pathMatcher = $path_matcher;
$this->currentPath = $current_path;
$this->aliasManager = $alias_manager;
$this->routeMatch = $route_match;
$this->killSwitch = $kill_switch;
}
/**
* {@inheritdoc}
*/
......@@ -30,24 +103,24 @@ class CacheexcludeSubscriber implements EventSubscriberInterface {
// Path checking routine.
if ($this->checkPath() === TRUE) {
// Disable page cache temporarily.
\Drupal::service('page_cache_kill_switch')->trigger();
$this->killSwitch->trigger();
return;
}
// Check if current node type is one we want to exclude from the cache.
$node = \Drupal::routeMatch()->getParameter('node');
$node = $this->routeMatch->getParameter('node');
if ($node instanceof NodeInterface) {
$node_type = $node->getType();
}
$config = \Drupal::config('cacheexclude.settings');
$config = $this->configFactory->get('cacheexclude.settings');
$node_types = $config->get('cacheexclude_node_types');
if (!is_null($node_types)) {
$node_types = array_filter($node_types);
if (isset($node_type) && in_array($node_type, $node_types, TRUE)) {
// Disable page cache temporarily.
\Drupal::service('page_cache_kill_switch')->trigger();
$this->killSwitch->trigger();
}
}
}
......@@ -60,17 +133,17 @@ class CacheexcludeSubscriber implements EventSubscriberInterface {
*/
private function checkPath() {
// Get cacheexclude page configuration.
$config = \Drupal::config('cacheexclude.settings');
$config = $this->configFactory->get('cacheexclude.settings');
// Only trim if config exists.
$pages = !is_null($config->get('cacheexclude_list')) ? trim($config->get('cacheexclude_list')) : NULL;
// If the current page is one we want to exclude from the cache,
// disable page cache temporarily.
if (!is_null($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);
$current_path = $this->currentPath->getPath();
$current_path_alias = $this->aliasManager->getAliasByPath($current_path);
$path_matches = $this->pathMatcher->matchPath($current_path, $pages);
$alias_path_matches = $this->pathMatcher->matchPath($current_path_alias, $pages);
if ($path_matches || $alias_path_matches) {
return TRUE;
......
......@@ -61,7 +61,7 @@ class AdminSettingsForm extends ConfigFormBase {
// Clear the page cache when new settings are added.
drupal_flush_all_caches();
$config = \Drupal::service('config.factory')->getEditable('cacheexclude.settings');
$config = $this->configFactory->getEditable('cacheexclude.settings');
$config->set('cacheexclude_list', $form_state->getValue('cacheexclude_list'))->save();
$config->set('cacheexclude_node_types', $form_state->getValue('cacheexclude_node_types'))->save();
parent::submitForm($form, $form_state);
......
......@@ -64,7 +64,8 @@ class CacheExcludeHeaderTest extends BrowserTestBase {
// No cache for <front>.
$this->drupalGet($paths['excluded_path']);
$this->assertSession()->responseHeaderDoesNotExist('X-Drupal-Cache');
// See https://www.drupal.org/node/2958442
$this->assertSession()->responseHeaderEquals('X-Drupal-Cache', 'UNCACHEABLE (response policy)');
// Cache for /node, but first request should MISS.
$this->drupalGet($paths['cached_path']);
......
......@@ -2,7 +2,6 @@
namespace Drupal\Tests\cacheexclude\Functional;
use Drupal\Core\Url;
use Drupal\Tests\BrowserTestBase;
use Drupal\Tests\system\Functional\Cache\AssertPageCacheContextsAndTagsTrait;
......@@ -83,7 +82,8 @@ class CacheExcludeNodeTypeTest extends BrowserTestBase {
// No cache for the article.
$this->drupalGet($paths['article_path']);
$this->assertSession()->responseHeaderDoesNotExist('X-Drupal-Cache');
// See https://www.drupal.org/node/2958442
$this->assertSession()->responseHeaderEquals('X-Drupal-Cache', 'UNCACHEABLE (response policy)');
// Cache for the page, but first request should MISS.
$this->drupalGet($paths['page_path']);
......
......@@ -19,6 +19,7 @@ class CacheExcludeMigrateTest extends MigrateDrupal7TestBase {
// Text module is enabled because we need "text_with_summary" plugin.
'text',
'cacheexclude',
'path_alias',
];
/**
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment