diff --git a/core/lib/Drupal/Core/Block/BlockBase.php b/core/lib/Drupal/Core/Block/BlockBase.php index bf349cdfadebafbc1e6b9b7b1b63e4b2e0373717..7ec2e6cffe1663a83c0bb1706699fff1b6bee900 100644 --- a/core/lib/Drupal/Core/Block/BlockBase.php +++ b/core/lib/Drupal/Core/Block/BlockBase.php @@ -305,13 +305,6 @@ public function setTransliteration(TransliterationInterface $transliteration) { $this->transliteration = $transliteration; } - /** - * {@inheritdoc} - */ - public function getCacheKeys() { - return []; - } - /** * {@inheritdoc} */ @@ -333,15 +326,4 @@ public function getCacheMaxAge() { return (int)$this->configuration['cache']['max_age']; } - /** - * {@inheritdoc} - */ - public function isCacheable() { - // Similar to the page cache, a block is cacheable if it has a max age. - // Blocks that should never be cached can override this method to simply - // return FALSE. - $max_age = $this->getCacheMaxAge(); - return $max_age === Cache::PERMANENT || $max_age > 0; - } - } diff --git a/core/lib/Drupal/Core/Block/BlockPluginInterface.php b/core/lib/Drupal/Core/Block/BlockPluginInterface.php index 10ec23ef2c32e33c273369fd635ce8cb3a2cea58..0a58b3356a1eaab12be55b79e89548a953eb3966 100644 --- a/core/lib/Drupal/Core/Block/BlockPluginInterface.php +++ b/core/lib/Drupal/Core/Block/BlockPluginInterface.php @@ -8,7 +8,7 @@ namespace Drupal\Core\Block; use Drupal\Component\Plugin\DerivativeInspectionInterface; -use Drupal\Core\Cache\CacheableInterface; +use Drupal\Core\Cache\CacheableDependencyInterface; use Drupal\Component\Plugin\PluginInspectionInterface; use Drupal\Component\Plugin\ConfigurablePluginInterface; use Drupal\Core\Form\FormStateInterface; @@ -25,7 +25,7 @@ * * @ingroup block_api */ -interface BlockPluginInterface extends ConfigurablePluginInterface, PluginFormInterface, PluginInspectionInterface, CacheableInterface, DerivativeInspectionInterface { +interface BlockPluginInterface extends ConfigurablePluginInterface, PluginFormInterface, PluginInspectionInterface, CacheableDependencyInterface, DerivativeInspectionInterface { /** * Returns the user-facing block label. diff --git a/core/lib/Drupal/Core/Cache/CacheableInterface.php b/core/lib/Drupal/Core/Cache/CacheableInterface.php deleted file mode 100644 index f57f23644af67af85232caea0c02ad774430bdba..0000000000000000000000000000000000000000 --- a/core/lib/Drupal/Core/Cache/CacheableInterface.php +++ /dev/null @@ -1,43 +0,0 @@ -<?php -/** - * @file - * Contains \Drupal\Core\CacheableInterface - */ - -namespace Drupal\Core\Cache; - -/** - * Defines an interface for objects which are potentially cacheable. - * - * All cacheability metadata exposed in this interface is bubbled to parent - * objects when they are cached: if a child object needs to be varied by certain - * cache contexts, invalidated by certain cache tags, expire after a certain - * maximum age, then so should any parent object. And if a child object is not - * cacheable, then neither is any parent object. - * The only cacheability metadata that must not be bubbled, are the cache keys: - * they're explicitly intended to be used to generate the cache item ID when - * caching the object they're on. - * - * @ingroup cache - */ -interface CacheableInterface extends CacheableDependencyInterface { - - /** - * The cache keys associated with this potentially cacheable object. - * - * These identify the object. - * - * @return string[] - * An array of strings, used to generate a cache ID. - */ - public function getCacheKeys(); - - /** - * Indicates whether this object is cacheable. - * - * @return bool - * Returns TRUE if the object is cacheable, FALSE otherwise. - */ - public function isCacheable(); - -} diff --git a/core/modules/block/src/BlockViewBuilder.php b/core/modules/block/src/BlockViewBuilder.php index 9d5c7a88e1d2794a487a71800dc745139e46e54b..216dfa85db2952b22a3bff6451d6d47cc6c1ea87 100644 --- a/core/modules/block/src/BlockViewBuilder.php +++ b/core/modules/block/src/BlockViewBuilder.php @@ -64,6 +64,7 @@ public function viewMultiple(array $entities = array(), $view_mode = 'full', $la '#derivative_plugin_id' => $derivative_id, '#id' => $entity->id(), '#cache' => [ + 'keys' => ['entity_view', 'block', $entity->id()], 'contexts' => $plugin->getCacheContexts(), 'tags' => Cache::mergeTags( $this->getCacheTags(), // Block view builder cache tag. @@ -72,25 +73,14 @@ public function viewMultiple(array $entities = array(), $view_mode = 'full', $la ), 'max-age' => $plugin->getCacheMaxAge(), ], + '#pre_render' => [ + [$this, 'buildBlock'], + ], // Add the entity so that it can be used in the #pre_render method. '#block' => $entity, ); $build[$entity_id]['#configuration']['label'] = SafeMarkup::checkPlain($configuration['label']); - if ($plugin->isCacheable()) { - $build[$entity_id]['#pre_render'][] = array($this, 'buildBlock'); - // Generic cache keys, with the block plugin's custom keys appended. - $default_cache_keys = array( - 'entity_view', - 'block', - $entity->id(), - ); - $build[$entity_id]['#cache']['keys'] = array_merge($default_cache_keys, $plugin->getCacheKeys()); - } - else { - $build[$entity_id] = $this->buildBlock($build[$entity_id]); - } - // Don't run in ::buildBlock() to ensure cache keys can be altered. If an // alter hook wants to modify the block contents, it can append another // #pre_render hook. diff --git a/core/modules/block/src/Plugin/DisplayVariant/BlockPageVariant.php b/core/modules/block/src/Plugin/DisplayVariant/BlockPageVariant.php index 798805e6840b64b07a797af9fa3ac432772238b1..7b6709372b6bcba95f54c1a430f1b9b03bed57b1 100644 --- a/core/modules/block/src/Plugin/DisplayVariant/BlockPageVariant.php +++ b/core/modules/block/src/Plugin/DisplayVariant/BlockPageVariant.php @@ -142,6 +142,13 @@ public function build() { $messages_block_displayed = TRUE; } $build[$region][$key] = $this->blockViewBuilder->view($block); + + // The main content block cannot be cached: it is a placeholder for the + // render array returned by the controller. It should be rendered as-is, + // with other placed blocks "decorating" it. + if ($block_plugin instanceof MainContentBlockPluginInterface) { + unset($build[$region][$key]['#cache']['keys']); + } } if (!empty($build[$region])) { // \Drupal\block\BlockRepositoryInterface::getVisibleBlocksPerRegion() diff --git a/core/modules/block/tests/modules/block_test/src/Plugin/Block/TestAccessBlock.php b/core/modules/block/tests/modules/block_test/src/Plugin/Block/TestAccessBlock.php index b3ed511e7b0b00047a8d9dc545ce5608a6724dc9..1aa7d6b0c37ba814bf8e414a3c19895edb438407 100644 --- a/core/modules/block/tests/modules/block_test/src/Plugin/Block/TestAccessBlock.php +++ b/core/modules/block/tests/modules/block_test/src/Plugin/Block/TestAccessBlock.php @@ -8,6 +8,7 @@ namespace Drupal\block_test\Plugin\Block; use Drupal\Core\Block\BlockBase; +use Drupal\Core\Cache\Cache; use Drupal\Core\Plugin\ContainerFactoryPluginInterface; use Drupal\Core\Session\AccountInterface; use Drupal\Core\State\StateInterface; @@ -74,8 +75,8 @@ public function build() { /** * {@inheritdoc} */ - public function isCacheable() { - return TRUE; + public function getCacheMaxAge() { + return Cache::PERMANENT; } } diff --git a/core/modules/forum/src/Plugin/Block/ForumBlockBase.php b/core/modules/forum/src/Plugin/Block/ForumBlockBase.php index ec5431a3d9a82bb8d8007c0d99ec168e40088014..c48767cf78688cb19d059040d5dbe0c94f4e359a 100644 --- a/core/modules/forum/src/Plugin/Block/ForumBlockBase.php +++ b/core/modules/forum/src/Plugin/Block/ForumBlockBase.php @@ -86,10 +86,8 @@ public function blockSubmit($form, FormStateInterface $form_state) { /** * {@inheritdoc} */ - public function getCacheKeys() { - $keys = parent::getCacheKeys(); - $keys[] = Cache::keyFromQuery($this->buildForumQuery()); - return $keys; + public function getCacheContexts() { + return ['user.node_grants:view']; } /** diff --git a/core/modules/node/src/Plugin/Block/SyndicateBlock.php b/core/modules/node/src/Plugin/Block/SyndicateBlock.php index 436a5701d73b5cb6889a739060b48c315432b5e8..f2a6a3e93f2b1f8295828412d987b1faa9379c82 100644 --- a/core/modules/node/src/Plugin/Block/SyndicateBlock.php +++ b/core/modules/node/src/Plugin/Block/SyndicateBlock.php @@ -8,6 +8,7 @@ namespace Drupal\node\Plugin\Block; use Drupal\Core\Block\BlockBase; +use Drupal\Core\Cache\Cache; use Drupal\Core\Form\FormStateInterface; use Drupal\Core\Session\AccountInterface; @@ -54,10 +55,10 @@ public function build() { public function buildConfigurationForm(array $form, FormStateInterface $form_state) { $form = parent::buildConfigurationForm($form, $form_state); - // @see ::isCacheable() + // @see ::getCacheMaxAge() $form['cache']['#disabled'] = TRUE; - $form['cache']['#description'] = $this->t('This block is never cacheable, it is not configurable.'); - $form['cache']['max_age']['#value'] = 0; + $form['cache']['max_age']['#value'] = Cache::PERMANENT; + $form['cache']['#description'] = $this->t('This block is always cached forever, it is not configurable.'); return $form; } @@ -65,10 +66,10 @@ public function buildConfigurationForm(array $form, FormStateInterface $form_sta /** * {@inheritdoc} */ - public function isCacheable() { - // The 'Syndicate' block is never cacheable, because it is cheaper to just - // render it rather than to cache it and incur I/O. - return FALSE; + public function getCacheMaxAge() { + // The 'Syndicate' block is permanently cacheable, because its + // contents can never change. + return Cache::PERMANENT; } } diff --git a/core/modules/system/src/Plugin/Block/SystemMainBlock.php b/core/modules/system/src/Plugin/Block/SystemMainBlock.php index 0e744dbd982768f14cf933fc8e62a47f27c4db76..f164f6689d24f00fcdd4e7a192757736bb5076ed 100644 --- a/core/modules/system/src/Plugin/Block/SystemMainBlock.php +++ b/core/modules/system/src/Plugin/Block/SystemMainBlock.php @@ -9,6 +9,7 @@ use Drupal\Core\Block\BlockBase; use Drupal\Core\Block\MainContentBlockPluginInterface; +use Drupal\Core\Cache\Cache; use Drupal\Core\Form\FormStateInterface; /** @@ -48,20 +49,11 @@ public function build() { public function buildConfigurationForm(array $form, FormStateInterface $form_state) { $form = parent::buildConfigurationForm($form, $form_state); - // The main content block is never cacheable, because it may be dynamic. $form['cache']['#disabled'] = TRUE; - $form['cache']['#description'] = $this->t('This block is never cacheable, it is not configurable.'); - $form['cache']['max_age']['#value'] = 0; + $form['cache']['#description'] = $this->t("This block's maximum age cannot be configured, because it depends on the contents."); + $form['cache']['max_age']['#value'] = Cache::PERMANENT; return $form; } - /** - * {@inheritdoc} - */ - public function isCacheable() { - // The main content block is never cacheable, because it may be dynamic. - return FALSE; - } - } diff --git a/core/modules/system/src/Plugin/Block/SystemMessagesBlock.php b/core/modules/system/src/Plugin/Block/SystemMessagesBlock.php index 59dde2a854775d718a637218c6cbf05d3f669619..611fd23ce0fd0292e33c390d665926194a52769a 100644 --- a/core/modules/system/src/Plugin/Block/SystemMessagesBlock.php +++ b/core/modules/system/src/Plugin/Block/SystemMessagesBlock.php @@ -46,7 +46,7 @@ public function build() { public function buildConfigurationForm(array $form, FormStateInterface $form_state) { $form = parent::buildConfigurationForm($form, $form_state); - // @see ::isCacheable() + // @see ::getCacheMaxAge() $form['cache']['#description'] = $this->t('This block is cacheable forever, it is not configurable.'); $form['cache']['max_age']['#value'] = Cache::PERMANENT; $form['cache']['max_age']['#disabled'] = TRUE; @@ -57,11 +57,11 @@ public function buildConfigurationForm(array $form, FormStateInterface $form_sta /** * {@inheritdoc} */ - public function isCacheable() { + public function getCacheMaxAge() { // The messages are session-specific and hence aren't cacheable, but the // block itself *is* cacheable because it uses a #post_render_cache callback // and hence the block has a globally cacheable render array. - return TRUE; + return Cache::PERMANENT; } } diff --git a/core/modules/system/src/Plugin/Block/SystemPoweredByBlock.php b/core/modules/system/src/Plugin/Block/SystemPoweredByBlock.php index 25e244da205044470bb46da98d8a27089e78c501..c768e216c225f993b932da8d0297d141e58f900d 100644 --- a/core/modules/system/src/Plugin/Block/SystemPoweredByBlock.php +++ b/core/modules/system/src/Plugin/Block/SystemPoweredByBlock.php @@ -34,8 +34,7 @@ public function build() { public function buildConfigurationForm(array $form, FormStateInterface $form_state) { $form = parent::buildConfigurationForm($form, $form_state); - // The 'Powered by Drupal' block is permanently cacheable, because its - // contents can never change. + // @see ::getCacheMaxAge() $form['cache']['#disabled'] = TRUE; $form['cache']['max_age']['#value'] = Cache::PERMANENT; $form['cache']['#description'] = $this->t('This block is always cached forever, it is not configurable.'); @@ -47,14 +46,9 @@ public function buildConfigurationForm(array $form, FormStateInterface $form_sta * {@inheritdoc} */ public function getCacheMaxAge() { + // The 'Powered by Drupal' block is permanently cacheable, because its + // contents can never change. return Cache::PERMANENT; } - /** - * {@inheritdoc} - */ - public function isCacheable() { - return TRUE; - } - }