3 namespace Drupal\system\EventSubscriber;
5 use Drupal\Core\Cache\CacheTagsInvalidatorInterface;
6 use Drupal\Core\Config\ConfigCrudEvent;
7 use Drupal\Core\Config\ConfigEvents;
8 use Drupal\Core\Extension\ThemeHandlerInterface;
9 use Symfony\Component\EventDispatcher\EventSubscriberInterface;
12 * A subscriber invalidating cache tags when system config objects are saved.
14 class ConfigCacheTag implements EventSubscriberInterface {
19 * @var \Drupal\Core\Extension\ThemeHandlerInterface
21 protected $themeHandler;
24 * The cache tags invalidator.
26 * @var \Drupal\Core\Cache\CacheTagsInvalidatorInterface
28 protected $cacheTagsInvalidator;
31 * Constructs a ConfigCacheTag object.
33 * @param \Drupal\Core\Extension\ThemeHandlerInterface $theme_handler
35 * @param \Drupal\Core\Cache\CacheTagsInvalidatorInterface $cache_tags_invalidator
36 * The cache tags invalidator.
38 public function __construct(ThemeHandlerInterface $theme_handler, CacheTagsInvalidatorInterface $cache_tags_invalidator) {
39 $this->themeHandler = $theme_handler;
40 $this->cacheTagsInvalidator = $cache_tags_invalidator;
44 * Invalidate cache tags when particular system config objects are saved.
46 * @param \Drupal\Core\Config\ConfigCrudEvent $event
47 * The Event to process.
49 public function onSave(ConfigCrudEvent $event) {
50 // Changing the site settings may mean a different route is selected for the
51 // front page. Additionally a change to the site name or similar must
52 // invalidate the render cache since this could be used anywhere.
53 if ($event->getConfig()->getName() === 'system.site') {
54 $this->cacheTagsInvalidator->invalidateTags(['route_match', 'rendered']);
57 // Theme configuration and global theme settings.
58 if (in_array($event->getConfig()->getName(), ['system.theme', 'system.theme.global'], TRUE)) {
59 $this->cacheTagsInvalidator->invalidateTags(['rendered']);
62 // Theme-specific settings, check if this matches a theme settings
63 // configuration object, in that case, clear the rendered cache tag.
64 foreach (array_keys($this->themeHandler->listInfo()) as $theme_name) {
65 if ($theme_name == $event->getConfig()->getName()) {
66 $this->cacheTagsInvalidator->invalidateTags(['rendered']);
75 public static function getSubscribedEvents() {
76 $events[ConfigEvents::SAVE][] = ['onSave'];