Issue #2569083 by dsnopek, tim.plunkett, Wim Leers: Allow passing cacheable metadata to display variants (to enable Panels Everywhere)

8.0.x
webchick 2015-09-25 08:56:26 -07:00
parent dc95ff1c81
commit 4b292ef55b
7 changed files with 21 additions and 2 deletions

View File

@ -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}

View File

@ -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.
*/

View File

@ -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,

View File

@ -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.

View File

@ -33,6 +33,7 @@ class DisplayVariantTest extends WebTestBase {
$this->drupalGet('<front>');
$this->assertRaw('A very important, required value.');
$this->assertRaw('Explicitly passed in context.');
$this->assertCacheTag('custom_cache_tag');
}
}

View File

@ -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.');

View File

@ -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;
}