From 4b292ef55b50cde27411bb9681ab7de594fe07c5 Mon Sep 17 00:00:00 2001 From: webchick Date: Fri, 25 Sep 2015 08:56:26 -0700 Subject: [PATCH] Issue #2569083 by dsnopek, tim.plunkett, Wim Leers: Allow passing cacheable metadata to display variants (to enable Panels Everywhere) --- core/lib/Drupal/Core/Display/VariantBase.php | 2 ++ core/lib/Drupal/Core/Display/VariantInterface.php | 7 ++++++- core/lib/Drupal/Core/Render/MainContent/HtmlRenderer.php | 1 + .../Core/Render/PageDisplayVariantSelectionEvent.php | 7 ++++++- .../modules/system/src/Tests/Render/DisplayVariantTest.php | 1 + .../EventSubscriber/TestPageDisplayVariantSubscriber.php | 1 + .../src/Plugin/DisplayVariant/TestDisplayVariant.php | 4 ++++ 7 files changed, 21 insertions(+), 2 deletions(-) diff --git a/core/lib/Drupal/Core/Display/VariantBase.php b/core/lib/Drupal/Core/Display/VariantBase.php index c4c8c650c45..a579ab2508e 100644 --- a/core/lib/Drupal/Core/Display/VariantBase.php +++ b/core/lib/Drupal/Core/Display/VariantBase.php @@ -7,6 +7,7 @@ namespace Drupal\Core\Display; +use Drupal\Core\Cache\RefinableCacheableDependencyTrait; use Drupal\Core\Form\FormStateInterface; use Drupal\Core\Plugin\PluginBase; use Drupal\Core\Plugin\PluginDependencyTrait; @@ -23,6 +24,7 @@ use Drupal\Core\Session\AccountInterface; abstract class VariantBase extends PluginBase implements VariantInterface { use PluginDependencyTrait; + use RefinableCacheableDependencyTrait; /** * {@inheritdoc} diff --git a/core/lib/Drupal/Core/Display/VariantInterface.php b/core/lib/Drupal/Core/Display/VariantInterface.php index 8561b6a98a5..6c440bb9373 100644 --- a/core/lib/Drupal/Core/Display/VariantInterface.php +++ b/core/lib/Drupal/Core/Display/VariantInterface.php @@ -9,6 +9,7 @@ namespace Drupal\Core\Display; use Drupal\Component\Plugin\ConfigurablePluginInterface; use Drupal\Component\Plugin\PluginInspectionInterface; +use Drupal\Core\Cache\RefinableCacheableDependencyInterface; use Drupal\Core\Plugin\PluginFormInterface; use Drupal\Core\Session\AccountInterface; @@ -20,7 +21,7 @@ use Drupal\Core\Session\AccountInterface; * @see \Drupal\Core\Display\VariantManager * @see plugin_api */ -interface VariantInterface extends PluginInspectionInterface, ConfigurablePluginInterface, PluginFormInterface { +interface VariantInterface extends PluginInspectionInterface, ConfigurablePluginInterface, PluginFormInterface, RefinableCacheableDependencyInterface { /** * Returns the user-facing display variant label. @@ -79,6 +80,10 @@ interface VariantInterface extends PluginInspectionInterface, ConfigurablePlugin /** * Builds and returns the renderable array for the display variant. * + * The variant can contain cacheability metadata for the configuration that + * was passed in setConfiguration(). In the build() method, this should be + * added to the render array that is returned. + * * @return array * A render array for the display variant. */ diff --git a/core/lib/Drupal/Core/Render/MainContent/HtmlRenderer.php b/core/lib/Drupal/Core/Render/MainContent/HtmlRenderer.php index 598f249e944..96b909fe7d0 100644 --- a/core/lib/Drupal/Core/Render/MainContent/HtmlRenderer.php +++ b/core/lib/Drupal/Core/Render/MainContent/HtmlRenderer.php @@ -236,6 +236,7 @@ class HtmlRenderer implements MainContentRendererInterface { } $page_display ->setMainContent($main_content) + ->addCacheableDependency($event) ->setConfiguration($event->getPluginConfiguration()); // Some display variants need to be passed an array of contexts with // values because they can't get all their contexts globally. For example, diff --git a/core/lib/Drupal/Core/Render/PageDisplayVariantSelectionEvent.php b/core/lib/Drupal/Core/Render/PageDisplayVariantSelectionEvent.php index 627fc9d8bc1..56c881d9a2e 100644 --- a/core/lib/Drupal/Core/Render/PageDisplayVariantSelectionEvent.php +++ b/core/lib/Drupal/Core/Render/PageDisplayVariantSelectionEvent.php @@ -7,6 +7,8 @@ namespace Drupal\Core\Render; +use Drupal\Core\Cache\RefinableCacheableDependencyInterface; +use Drupal\Core\Cache\RefinableCacheableDependencyTrait; use Drupal\Core\Routing\RouteMatchInterface; use Symfony\Component\EventDispatcher\Event; @@ -17,11 +19,14 @@ use Symfony\Component\EventDispatcher\Event; * information along to the selected variant: * - self::setPluginConfiguration() * - self::setContexts() + * - self::addCacheableDependency() * * @see \Drupal\Core\Render\RenderEvents::SELECT_PAGE_DISPLAY_VARIANT * @see \Drupal\Core\Render\MainContent\HtmlRenderer */ -class PageDisplayVariantSelectionEvent extends Event { +class PageDisplayVariantSelectionEvent extends Event implements RefinableCacheableDependencyInterface { + + use RefinableCacheableDependencyTrait; /** * The selected page display variant plugin ID. diff --git a/core/modules/system/src/Tests/Render/DisplayVariantTest.php b/core/modules/system/src/Tests/Render/DisplayVariantTest.php index a50a0ce8dc4..c4c37bbedbb 100644 --- a/core/modules/system/src/Tests/Render/DisplayVariantTest.php +++ b/core/modules/system/src/Tests/Render/DisplayVariantTest.php @@ -33,6 +33,7 @@ class DisplayVariantTest extends WebTestBase { $this->drupalGet(''); $this->assertRaw('A very important, required value.'); $this->assertRaw('Explicitly passed in context.'); + $this->assertCacheTag('custom_cache_tag'); } } diff --git a/core/modules/system/tests/modules/display_variant_test/src/EventSubscriber/TestPageDisplayVariantSubscriber.php b/core/modules/system/tests/modules/display_variant_test/src/EventSubscriber/TestPageDisplayVariantSubscriber.php index d71f1867c35..3eca4d6b059 100644 --- a/core/modules/system/tests/modules/display_variant_test/src/EventSubscriber/TestPageDisplayVariantSubscriber.php +++ b/core/modules/system/tests/modules/display_variant_test/src/EventSubscriber/TestPageDisplayVariantSubscriber.php @@ -27,6 +27,7 @@ class TestPageDisplayVariantSubscriber implements EventSubscriberInterface { public function onSelectPageDisplayVariant(PageDisplayVariantSelectionEvent $event) { $event->setPluginId('display_variant_test'); $event->setPluginConfiguration(['required_configuration' => 'A very important, required value.']); + $event->addCacheTags(['custom_cache_tag']); $context = new Context(new ContextDefinition('string', NULL, TRUE)); $context->setContextValue('Explicitly passed in context.'); diff --git a/core/modules/system/tests/modules/display_variant_test/src/Plugin/DisplayVariant/TestDisplayVariant.php b/core/modules/system/tests/modules/display_variant_test/src/Plugin/DisplayVariant/TestDisplayVariant.php index a1a7373ccc3..0bee2a91724 100644 --- a/core/modules/system/tests/modules/display_variant_test/src/Plugin/DisplayVariant/TestDisplayVariant.php +++ b/core/modules/system/tests/modules/display_variant_test/src/Plugin/DisplayVariant/TestDisplayVariant.php @@ -7,6 +7,7 @@ namespace Drupal\display_variant_test\Plugin\DisplayVariant; +use Drupal\Core\Cache\CacheableMetadata; use Drupal\Core\Display\VariantBase; use Drupal\Core\Display\PageVariantInterface; use Drupal\Core\Display\ContextAwareVariantInterface; @@ -86,6 +87,9 @@ class TestDisplayVariant extends VariantBase implements PageVariantInterface, Co $build['content']['default'] = [ '#markup' => $config['required_configuration'] . ' ' . $contexts['context']->getContextValue(), ]; + + CacheableMetadata::createFromObject($this)->applyTo($build); + return $build; }