From 3e275d65dc4d569177b08607dc884bddaf89ed28 Mon Sep 17 00:00:00 2001
From: Alex Pott <alex.a.pott@googlemail.com>
Date: Wed, 8 Apr 2015 13:27:27 +0100
Subject: [PATCH] Issue #2459819 by Wim Leers, rteijeiro: Remove
 CacheableInterface (and no longer let block plugins implement it)

---
 core/lib/Drupal/Core/Block/BlockBase.php      | 18 --------
 .../Core/Block/BlockPluginInterface.php       |  4 +-
 .../Drupal/Core/Cache/CacheableInterface.php  | 43 -------------------
 core/modules/block/src/BlockViewBuilder.php   | 18 ++------
 .../DisplayVariant/BlockPageVariant.php       |  7 +++
 .../src/Plugin/Block/TestAccessBlock.php      |  5 ++-
 .../forum/src/Plugin/Block/ForumBlockBase.php |  6 +--
 .../node/src/Plugin/Block/SyndicateBlock.php  | 15 ++++---
 .../src/Plugin/Block/SystemMainBlock.php      | 14 ++----
 .../src/Plugin/Block/SystemMessagesBlock.php  |  6 +--
 .../src/Plugin/Block/SystemPoweredByBlock.php | 12 ++----
 11 files changed, 35 insertions(+), 113 deletions(-)
 delete mode 100644 core/lib/Drupal/Core/Cache/CacheableInterface.php

diff --git a/core/lib/Drupal/Core/Block/BlockBase.php b/core/lib/Drupal/Core/Block/BlockBase.php
index bf349cdfadeb..7ec2e6cffe16 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 10ec23ef2c32..0a58b3356a1e 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 f57f23644af6..000000000000
--- 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 9d5c7a88e1d2..216dfa85db29 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 798805e6840b..7b6709372b6b 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 b3ed511e7b0b..1aa7d6b0c37b 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 ec5431a3d9a8..c48767cf7868 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 436a5701d73b..f2a6a3e93f2b 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 0e744dbd9827..f164f6689d24 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 59dde2a85477..611fd23ce0fd 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 25e244da2050..c768e216c225 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;
-  }
-
 }
-- 
GitLab