Commit 3e275d65 authored by alexpott's avatar alexpott
Browse files

Issue #2459819 by Wim Leers, rteijeiro: Remove CacheableInterface (and no...

Issue #2459819 by Wim Leers, rteijeiro: Remove CacheableInterface (and no longer let block plugins implement it)
parent 0972ecb3
......@@ -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;
}
}
......@@ -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.
......
<?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();
}
......@@ -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.
......
......@@ -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()
......
......@@ -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;
}
}
......
......@@ -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'];
}
/**
......
......@@ -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;
}
}
......@@ -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;
}
}
......@@ -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;
}
}
......@@ -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;
}
}
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment