core/includes/theme.inc | 60 ------------------- core/lib/Drupal/Core/Theme/ThemeSettings.php | 10 +++- core/modules/comment/comment.module | 1 - core/modules/node/node.module | 1 - core/modules/shortcut/shortcut.module | 1 - .../src/EventSubscriber/ThemeSettingsCacheTag.php | 67 ++++++++++++++++++++++ .../src/Plugin/Block/SystemBrandingBlock.php | 3 - core/modules/system/system.module | 1 - core/modules/system/system.services.yml | 5 ++ core/modules/user/user.module | 1 - core/themes/bartik/bartik.theme | 1 - 11 files changed, 80 insertions(+), 71 deletions(-) diff --git a/core/includes/theme.inc b/core/includes/theme.inc index cb7581e..3dac164 100644 --- a/core/includes/theme.inc +++ b/core/includes/theme.inc @@ -319,63 +319,7 @@ function drupal_find_theme_templates($cache, $extension, $path) { return $implementations; } -/** - * For use in preprocess functions only; adds cache tags for theme settings. - * - * When a theme setting is used in a preprocess function, it is used to - * determine whether something should be visible or not. Hence the cache tags - * associated with that theme setting must *always* be added. - * - * Whenever theme_get_setting() is used in a preprocess function, this must also - * be called, to ensure that the rendered HTML (and the render cache items that - * contain this HTML) are tagged with the necessary cache tags. - * - * E.g.: - * @code - * theme_add_setting_cache_tag(); - * if (theme_get_setting('features.node_user_picture')) { - * $variables['author_picture'] = user_view($node->getOwner(), 'compact'); - * } - * @endcode - * - * @see theme_get_setting() - */ -function theme_add_setting_cache_tag() { - $build = [ - '#cache' => [ - 'tags' => theme_setting_get_cache_tags(), - ], - ]; - drupal_render($build); -} -/** - * Returns the cache tags to be used when using a theme setting. - * - * If a render array depends on a theme setting, then these cache tags should be - * associated with the render array. - * - * E.g.: - * @code - * $logo = theme_get_setting('logo'); - * $build['site_logo'] = array( - * '#theme' => 'image', - * '#uri' => $logo['url'], - * '#alt' => t('Home'), - * '#access' => $this->configuration['use_site_logo'], - * '#cache' => [ - * 'tags' => theme_setting_get_cache_tags(), - * ], - * ); - * @endcode - * - * There's no efficient way to figure out if it was the global theme setting - * or a theme-specific overriding setting, so we return cache tags for both. - * - * @return string[] - * - * @see theme_get_setting() - */ function theme_setting_get_cache_tags() { $theme = \Drupal::theme()->getActiveTheme()->getName(); return [ @@ -400,9 +344,6 @@ function theme_setting_get_cache_tags() { * * @return * The value of the requested setting, NULL if the setting does not exist. - * - * @see theme_setting_get_cache_tags() - * @see theme_add_setting_cache_tag() */ function theme_get_setting($setting_name, $theme = NULL) { /** @var \Drupal\Core\Theme\ThemeSettings[] $cache */ @@ -1501,7 +1442,6 @@ function template_preprocess_page(&$variables) { } } - theme_add_setting_cache_tag(); $variables['base_path'] = base_path(); $variables['front_page'] = \Drupal::url(''); $variables['language'] = $language_interface; diff --git a/core/lib/Drupal/Core/Theme/ThemeSettings.php b/core/lib/Drupal/Core/Theme/ThemeSettings.php index a43d3f8..ab0d081 100644 --- a/core/lib/Drupal/Core/Theme/ThemeSettings.php +++ b/core/lib/Drupal/Core/Theme/ThemeSettings.php @@ -17,8 +17,6 @@ * not persisted. * * @see theme_get_setting() - * @see theme_add_setting_cache_tag() - * @see theme_setting_get_cache_tags() */ class ThemeSettings extends ConfigBase { @@ -48,4 +46,12 @@ public function __construct($theme) { public function getTheme() { return $this->theme; } + + /** + * {@inheritdoc} + */ + public function getCacheTags() { + return ['rendered']; + } + } diff --git a/core/modules/comment/comment.module b/core/modules/comment/comment.module index f3724a6..d9f9674 100644 --- a/core/modules/comment/comment.module +++ b/core/modules/comment/comment.module @@ -708,7 +708,6 @@ function template_preprocess_comment(&$variables) { $variables['changed'] = format_date($comment->getChangedTime()); } - theme_add_setting_cache_tag(); if (theme_get_setting('features.comment_user_picture')) { // To change user picture settings (e.g., image style), edit the 'compact' // view mode on the User entity. diff --git a/core/modules/node/node.module b/core/modules/node/node.module index 0e709a0..918e738 100644 --- a/core/modules/node/node.module +++ b/core/modules/node/node.module @@ -604,7 +604,6 @@ function template_preprocess_node(&$variables) { $variables['author_attributes'] = new Attribute(); $variables['display_submitted'] = $node_type->displaySubmitted(); if ($variables['display_submitted']) { - theme_add_setting_cache_tag(); if (theme_get_setting('features.node_user_picture')) { // To change user picture settings (e.g. image style), edit the 'compact' // view mode on the User entity. Note that the 'compact' view mode might diff --git a/core/modules/shortcut/shortcut.module b/core/modules/shortcut/shortcut.module index 526f0da..ba9c43d 100644 --- a/core/modules/shortcut/shortcut.module +++ b/core/modules/shortcut/shortcut.module @@ -330,7 +330,6 @@ function shortcut_preprocess_page(&$variables) { $route_parameters = array('shortcut' => $shortcut_id); } - theme_add_setting_cache_tag(); if (theme_get_setting('third_party_settings.shortcut.module_link')) { $variables['title_suffix']['add_or_remove_shortcut'] = array( '#attached' => array( diff --git a/core/modules/system/src/EventSubscriber/ThemeSettingsCacheTag.php b/core/modules/system/src/EventSubscriber/ThemeSettingsCacheTag.php new file mode 100644 index 0000000..bfabb2d --- /dev/null +++ b/core/modules/system/src/EventSubscriber/ThemeSettingsCacheTag.php @@ -0,0 +1,67 @@ +themeHandler = $theme_handler; + } + + /** + * Invalidate the 'rendered' cache tag whenever a theme setting is modified. + * + * @param \Drupal\Core\Config\ConfigCrudEvent $event + * The Event to process. + */ + public function onSave(ConfigCrudEvent $event) { + // Global theme settings. + if ($event->getConfig()->getName() === 'system.theme.global') { + Cache::invalidateTags(['rendered']); + } + + // Theme-specific settings. + $matches = []; + if (preg_match('/(.*)\.settings$/', $event->getConfig()->getName(), $matches)) { + if (in_array($matches[1], array_keys($this->themeHandler->listInfo()))) { + Cache::invalidateTags(['rendered']); + } + } + } + + /** + * {@inheritdoc} + */ + public static function getSubscribedEvents() { + $events[ConfigEvents::SAVE][] = ['onSave']; + return $events; + } + +} diff --git a/core/modules/system/src/Plugin/Block/SystemBrandingBlock.php b/core/modules/system/src/Plugin/Block/SystemBrandingBlock.php index dfddabf..6d10aff 100644 --- a/core/modules/system/src/Plugin/Block/SystemBrandingBlock.php +++ b/core/modules/system/src/Plugin/Block/SystemBrandingBlock.php @@ -170,9 +170,6 @@ public function build() { '#uri' => $logo['url'], '#alt' => t('Home'), '#access' => $this->configuration['use_site_logo'], - '#cache' => [ - 'tags' => theme_setting_get_cache_tags(), - ], ); $build['site_name'] = array( diff --git a/core/modules/system/system.module b/core/modules/system/system.module index 1d9b1eb..8f2a8ab 100644 --- a/core/modules/system/system.module +++ b/core/modules/system/system.module @@ -530,7 +530,6 @@ function system_page_attachments(array &$page) { } // Attach favicon. - theme_add_setting_cache_tag(); if (theme_get_setting('features.favicon')) { $favicon = theme_get_setting('favicon.url'); $type = theme_get_setting('favicon.mimetype'); diff --git a/core/modules/system/system.services.yml b/core/modules/system/system.services.yml index e95c91d..97cd4b9 100644 --- a/core/modules/system/system.services.yml +++ b/core/modules/system/system.services.yml @@ -42,3 +42,8 @@ services: arguments: ['@cron', '@config.factory', '@state'] tags: - { name: event_subscriber } + system.theme_settings_cache_tag: + class: Drupal\system\EventSubscriber\ThemeSettingsCacheTag + arguments: ['@theme_handler'] + tags: + - { name: event_subscriber } diff --git a/core/modules/user/user.module b/core/modules/user/user.module index c23ab68..a02d098 100644 --- a/core/modules/user/user.module +++ b/core/modules/user/user.module @@ -535,7 +535,6 @@ function template_preprocess_username(&$variables) { $variables['extra'] = ''; $variables['uid'] = $account->id(); if (empty($variables['uid'])) { - theme_add_setting_cache_tag(); if (theme_get_setting('features.comment_user_verification')) { $variables['extra'] = ' (' . t('not verified') . ')'; } diff --git a/core/themes/bartik/bartik.theme b/core/themes/bartik/bartik.theme index 6700652..4d6ef3a 100644 --- a/core/themes/bartik/bartik.theme +++ b/core/themes/bartik/bartik.theme @@ -143,7 +143,6 @@ function _bartik_process_page(&$variables) { $site_config = \Drupal::config('system.site'); // Always print the site name and slogan, but if they are toggled off, we'll // just hide them visually. - theme_add_setting_cache_tag(); $variables['hide_site_name'] = theme_get_setting('features.name') ? FALSE : TRUE; $variables['hide_site_slogan'] = theme_get_setting('features.slogan') ? FALSE : TRUE; if ($variables['hide_site_name']) {