core/lib/Drupal/Core/Block/BlockBase.php | 18 --------- .../lib/Drupal/Core/Block/BlockPluginInterface.php | 4 +- core/lib/Drupal/Core/Cache/CacheableInterface.php | 43 ---------------------- core/modules/block/src/BlockViewBuilder.php | 18 ++------- .../src/Plugin/Block/TestAccessBlock.php | 5 ++- .../forum/src/Plugin/Block/ForumBlockBase.php | 9 ----- .../node/src/Plugin/Block/SyndicateBlock.php | 4 +- .../system/src/Plugin/Block/SystemMainBlock.php | 4 +- .../src/Plugin/Block/SystemMessagesBlock.php | 4 +- .../src/Plugin/Block/SystemPoweredByBlock.php | 7 ---- 10 files changed, 15 insertions(+), 101 deletions(-) diff --git a/core/lib/Drupal/Core/Block/BlockBase.php b/core/lib/Drupal/Core/Block/BlockBase.php index 9a6fcf7..40f6536 100644 --- a/core/lib/Drupal/Core/Block/BlockBase.php +++ b/core/lib/Drupal/Core/Block/BlockBase.php @@ -308,13 +308,6 @@ public function setTransliteration(TransliterationInterface $transliteration) { /** * {@inheritdoc} */ - public function getCacheKeys() { - return []; - } - - /** - * {@inheritdoc} - */ public function getCacheContexts() { return []; } @@ -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 10ec23e..0a58b33 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 f57f236..0000000 --- a/core/lib/Drupal/Core/Cache/CacheableInterface.php +++ /dev/null @@ -1,43 +0,0 @@ - $derivative_id, '#id' => $entity->id(), '#cache' => [ + 'keys' => ['entity_view', 'block', $entity->id()], 'contexts' => Cache::mergeContexts($default_cache_contexts, $plugin->getCacheContexts()), 'tags' => Cache::mergeTags( $this->getCacheTags(), // Block view builder cache tag. @@ -78,25 +79,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'] = String::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/tests/modules/block_test/src/Plugin/Block/TestAccessBlock.php b/core/modules/block/tests/modules/block_test/src/Plugin/Block/TestAccessBlock.php index b3ed511..1aa7d6b 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 ec5431a..7251bdf 100644 --- a/core/modules/forum/src/Plugin/Block/ForumBlockBase.php +++ b/core/modules/forum/src/Plugin/Block/ForumBlockBase.php @@ -86,15 +86,6 @@ public function blockSubmit($form, FormStateInterface $form_state) { /** * {@inheritdoc} */ - public function getCacheKeys() { - $keys = parent::getCacheKeys(); - $keys[] = Cache::keyFromQuery($this->buildForumQuery()); - return $keys; - } - - /** - * {@inheritdoc} - */ public function getCacheTags() { return ['node_list']; } diff --git a/core/modules/node/src/Plugin/Block/SyndicateBlock.php b/core/modules/node/src/Plugin/Block/SyndicateBlock.php index 436a570..763501f 100644 --- a/core/modules/node/src/Plugin/Block/SyndicateBlock.php +++ b/core/modules/node/src/Plugin/Block/SyndicateBlock.php @@ -65,10 +65,10 @@ public function buildConfigurationForm(array $form, FormStateInterface $form_sta /** * {@inheritdoc} */ - public function isCacheable() { + public function getCacheMaxAge() { // 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; + return 0; } } diff --git a/core/modules/system/src/Plugin/Block/SystemMainBlock.php b/core/modules/system/src/Plugin/Block/SystemMainBlock.php index 0e744db..3d2d45e 100644 --- a/core/modules/system/src/Plugin/Block/SystemMainBlock.php +++ b/core/modules/system/src/Plugin/Block/SystemMainBlock.php @@ -59,9 +59,9 @@ public function buildConfigurationForm(array $form, FormStateInterface $form_sta /** * {@inheritdoc} */ - public function isCacheable() { + public function getCacheMaxAge() { // The main content block is never cacheable, because it may be dynamic. - return FALSE; + return 0; } } diff --git a/core/modules/system/src/Plugin/Block/SystemMessagesBlock.php b/core/modules/system/src/Plugin/Block/SystemMessagesBlock.php index 59dde2a..92dc62b 100644 --- a/core/modules/system/src/Plugin/Block/SystemMessagesBlock.php +++ b/core/modules/system/src/Plugin/Block/SystemMessagesBlock.php @@ -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 25e244d..38fd083 100644 --- a/core/modules/system/src/Plugin/Block/SystemPoweredByBlock.php +++ b/core/modules/system/src/Plugin/Block/SystemPoweredByBlock.php @@ -50,11 +50,4 @@ public function getCacheMaxAge() { return Cache::PERMANENT; } - /** - * {@inheritdoc} - */ - public function isCacheable() { - return TRUE; - } - }