Skip to content
Snippets Groups Projects
Commit 66e9ae77 authored by Stephen Mustgrave's avatar Stephen Mustgrave
Browse files

Resolve #2483181 "Attempt2 cache"

parent e9a95303
No related branches found
No related tags found
1 merge request!66Resolve #2483181 "Attempt2 cache"
Pipeline #312721 passed
......@@ -65,3 +65,7 @@ services:
# This tag ensures that Drupal's cache_tags.invalidator service
# invalidates also this cache data.
- { name: cache.bin.memory }
book.setting_save.subscriber:
class: Drupal\book\EventSubscriber\BookSettingsSaveEventSubscriber
tags:
- { name: event_subscriber }
<?php
namespace Drupal\book\EventSubscriber;
use Drupal\Core\Cache\Cache;
use Drupal\Core\Config\ConfigCrudEvent;
use Drupal\Core\Config\ConfigEvents;
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
/**
* Clears cache tag when Book settings is saved.
*/
class BookSettingsSaveEventSubscriber implements EventSubscriberInterface {
/**
* Acts on changes to book settings to cache tag.
*
* @param \Drupal\Core\Config\ConfigCrudEvent $event
* The configuration event.
*/
public function onConfigSave(ConfigCrudEvent $event): void {
$config = $event->getConfig();
if ($config->getName() === 'book.settings') {
// Now that the block is cached it needs to be invalidated.
Cache::invalidateTags(['book_settings']);
}
}
/**
* {@inheritdoc}
*/
public static function getSubscribedEvents(): array {
return [ConfigEvents::SAVE => 'onConfigSave'];
}
}
......@@ -216,16 +216,23 @@ class BookNavigationBlock extends BlockBase implements ContainerFactoryPluginInt
* {@inheritdoc}
*/
public function getCacheContexts(): array {
return Cache::mergeContexts(parent::getCacheContexts(), ['route.book_navigation']);
// ::build() varies by the "book navigation" cache context.
// Additional cache contexts, e.g. those that determine link text or
// accessibility of a menu, will be bubbled automatically.
return Cache::mergeContexts(parent::getCacheContexts(), [
'route.book_navigation',
'user.node_grants:view',
]);
}
/**
* {@inheritdoc}
*
* @todo Make cacheable in https://www.drupal.org/node/2483181
*/
public function getCacheMaxAge(): int {
return 0;
public function getCacheTags(): array {
return Cache::mergeTags(parent::getCacheTags(), [
'node_list',
'book_settings',
]);
}
/**
......
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