Problem/Motivation

Similar to #2550941: Allow passing contexts to display variants (to enable Panels Everywhere to use static context and relationships).

  1. The variant is passed it's configuration (like any plugin implementing ConfigurablePluginInterface) via $variant->setConfiguration()
  2. Since the variant can be used by multiple systems, ie. in core, in Page Manager, in mini-Panels, Panelizer, etc -- it has no idea where that configuration actually came from
  3. However, in order to correctly implement render caching, we need to put information on the render array about the source of the configuration, so that the render cache can be cleared when the configuration changes
  4. The variant itself is kind of a "black box" in that from the outside we don't really know what it's going to put in the render array and it could do clever stuff with caching, like setting $element['#cache']['keys'] on the blocks so they're cached independently or using $element['#pre_render'] to prevent loading the blocks if the full variant is in the cache, etc. These are good performance improvements! But only the variant itself is really fully qualified to know where the cacheable metadata should be added (and what should be added, ex. adding only the cache tags on the blocks)

So... we need to pass the cacheable metadata to the variant so it can do what needs to be done!

Like these other issues that came before it - this issue blocks panels_everywhere (without fixing this, panels_everywhere is impossible in Drupal 8):

  1. #2550941: Allow passing contexts to display variants (to enable Panels Everywhere to use static context and relationships)
  2. #2512062: VariantInterface extends ConfigurablePluginInterface so PageDisplayVariantSelectionEvent should allow passing configuration to the Variant (to enable Panels Everywhere)

Proposed resolution

  • VariantInterface should extend RefinableCacheableDependencyInterface
  • VariantBase should use RefinableCacheableDependencyTrait
  • PageDisplayVariantSelectionEvent.php should use RefinableCacheableDependencyTrait so we can pass in the right cacheable metadata
  • HtmlRendere should pass the cacheable metadata from the event to the variant

Remaining tasks

  • Write patch
  • Get reviews/input
  • Commit!

User interface changes

None.

API changes

This is only API additions of some new methods - no changes.

Data model changes

None.

Beta phase evaluation

Reference: https://www.drupal.org/core/beta-changes
Issue category Bug because without allowing cacheable metadata to passed to the variant, it won't be possible to set the render cache information correctly -- for variants that depend on their configuration to decide what to do. This doesn't affect the variants in core because they work off of no or global data, but I suspect most variants provided by contrib will change what they do depending on their configuration!
Issue priority Major because it renders the PageDisplayVariantSelectionEvent useless for any Variant that depends on configuration and wants to do render caching correctly, which will likely be any Variant provided by contrib. The core variants are kind of exceptional in NOT depending on their configuration.
Disruption Disruption for core and contrib will be minimal because this only causes some existing classes implement an additional interface and use a trait -- it doesn't change any existing method signatures.

Comments

tim.plunkett created an issue. See original summary.

tim.plunkett’s picture

Status: Active » Needs review
StatusFileSize
new5.6 KB
new4.97 KB
dsnopek’s picture

This looks perfect! Assuming the tests pass, this pretty much RTBC as far as I'm concerned. :-) I'll wait until we have a new patch for Page Manager on #2511570: Remove Drupal\page_manager\Plugin\PageAwareVariantInterface that uses this to actually mark RTBC.

Note: This is a blocker for Page Manager and Panels Everywhere!

This is very similar in nature to this other core issue #2550941: Allow passing contexts to display variants (to enable Panels Everywhere to use static context and relationships), but for contexts rather than cache metadata.

Status: Needs review » Needs work

The last submitted patch, 2: 2569083-cacheablevariants-2-FAIL.patch, failed testing.

eclipsegc’s picture

+++ b/core/modules/system/tests/modules/display_variant_test/src/Plugin/DisplayVariant/TestDisplayVariant.php
@@ -48,6 +49,12 @@ public function build() {
+

Yeah that really sucks... Should the calling code be responsible for this in some way?

This looks really great other then this one issue.

Eclipse

dsnopek’s picture

Status: Needs work » Needs review

@EclipseGC: I assume you're referring to this chunk?

+++ b/core/modules/system/tests/modules/display_variant_test/src/Plugin/DisplayVariant/TestDisplayVariant.php
@@ -48,6 +49,12 @@ public function build() {
+
+    // @todo Why do I need to do this?
+    CacheableMetadata::createFromRenderArray($build)
+      ->addCacheableDependency($this)
+      ->applyTo($build);
+

So, it's a little weird that we're calling CacheableMetadata::createFromRenderArray() because we know there's nothing on the render array yet (because we just created it).

However, the rest of this is how it should be: it should be up to the variant to decide where to add the cacheable metadata that was passed to it. I could imagine variants where it would put it somewhere deeper in the render array, because maybe it's only the inner parts that are dependent on configuration and the outer parts are static (ie. not affected by changes to configuration).

(BTW, in practice I think we'll be setting at least $build['#cache']['keys'] before doing CacheableMetadata::createFromRenderArray($build) so it'll be less weird in real code.)

Setting back to NR since it's the fail patch that put us in NW.

dsnopek’s picture

Issue summary: View changes

Updated the issue summary, but this still needs a BETA evaluation.

dsnopek’s picture

Added the beta evaluation.

The last submitted patch, 2: 2569083-cacheablevariants-2-PASS.patch, failed testing.

wim leers’s picture

+    // @todo Why do I need to do this?
+    CacheableMetadata::createFromRenderArray($build)
+      ->addCacheableDependency($this)
+      ->applyTo($build);

Alternative:

CacheableMetadata::createFromObject($this)->applyTo($build);
wim leers’s picture

  1. --- a/core/lib/Drupal/Core/Render/MainContent/HtmlRenderer.php
    +++ b/core/lib/Drupal/Core/Render/MainContent/HtmlRenderer.php
    @@ -235,6 +235,7 @@ protected function prepare(array $main_content, Request $request, RouteMatchInte
           }
           $page_display
             ->setMainContent($main_content)
    +        ->addCacheableDependency($event)
    

    The $event contains cacheability metadata? That seems impossible.

  2. +++ b/core/lib/Drupal/Core/Render/PageDisplayVariantSelectionEvent.php
    @@ -16,7 +18,9 @@
    -class PageDisplayVariantSelectionEvent extends Event {
    +class PageDisplayVariantSelectionEvent extends Event implements RefinableCacheableDependencyInterface {
    +
    +  use RefinableCacheableDependencyTrait;
    

    Oh this is why it's possible.

    It's to allow cacheability metadata involved in the selection of the page variant to be bubbled, then? That makes sense!

    This does not get the cacheability metadata of the page variant instance though, right?

dsnopek’s picture

StatusFileSize
new5.51 KB
new835 bytes

Alternative:

CacheableMetadata::createFromObject($this)->applyTo($build);

Ah, yeah, that is better! Here's a new patch that just makes this change.

It's to allow cacheability metadata involved in the selection of the page variant to be bubbled, then? That makes sense!

Yep! In most cases, this will be passing the cacheability metadata from config entity that's the source of the configuration that's also being passed to the page variant.

This does not get the cacheability metadata of the page variant instance though, right?

Right. If the page variant has other cacheability metadata that it's aware of (for example, from contexts) then it'll add it to the render array itself.

wim leers’s picture

Yep! In most cases, this will be passing the cacheability metadata from config entity that's the source of the configuration that's also being passed to the page variant.

But … that's a distinctly different thing, isn't it?

The event is a value object that represents the end result of a selection process. That end result is a plugin ID (simple, block, page_manager …). Therefore, the cacheability metadata on the event should only contain the cacheability metadata that led to that plugin ID being selected.
The reason we haven't needed that thus far, is that the simple page variant is the default, and if the block module is enabled, then the block page variant is selected, always. i.e., so far, there is zero conditionality; the selected page variant depends solely on the set of enabled modules (and installing new modules clears all caches). This is also why #2565887: Fix test failures stemming from Dynamic Page Cache was necessary to make Page Manager work once Dynamic Page Cache was committed: because it can be configured to override the used page variant on certain routes, it doesn't override all routes!

Now… after that hopefully crystal clear explanation… it's totally possible that this is in fact exactly what is happening here, and therefore it is the same thing! That is, if it's the Panels/Page Manager/whatever config entity here that contains the data that tells the module whether a certain route should use a different page variant, then that config entity is the thing that affects the selection criteria.

So, my point is this: if the config entity is just something used by the page variant but not the thing that causes that page variant to be selected, then it would be wrong to add its cacheability metadata to the event value object.

Whew. Hope that made sense :)

dsnopek’s picture

But we also pass configuration to the variant in this event. So it would make sense to pass the cacheability metadata for that config here too, right?

dsnopek’s picture

And if not here, then where?

The last submitted patch, 2: 2569083-cacheablevariants-2-FAIL.patch, failed testing.

The last submitted patch, 2: 2569083-cacheablevariants-2-FAIL.patch, failed testing.

xano’s picture

+++ b/core/lib/Drupal/Core/Display/VariantInterface.php
@@ -20,7 +21,7 @@
+interface VariantInterface extends PluginInspectionInterface, ConfigurablePluginInterface, PluginFormInterface, RefinableCacheableDependencyInterface {

MUST all variants provide this metadata? If no, then the base interface should not extend CacheableDependencyInterface.

If yes, does external code really need to refine cacheability metadata directly? This looks like something the config setter methods should take care of internally. As a developer, I don't want to have call two separate methods to set config and to refine cacheability metadata.

dsnopek’s picture

MUST all variants provide this metadata? If no, then the base interface should not extend CacheableDependencyInterface.

Effectively, yes.

If the configuration passed to setConfiguration() came from CMI (which will be 99.99% of the time in Drupal 8), then cacheability metadata MUST be provided or the render array returned from VariantInterface::build() will have incorrect cacheability metadata on it, namely, the cache won't be cleared when the configuration changes.

Note: All config objects and entities from CMI have cacheability metadata on them, we just need to find a way to pass it to the variant.

If yes, does external code really need to refine cacheability metadata directly? This looks like something the config setter methods should take care of internally. As a developer, I don't want to have call two separate methods to set config and to refine cacheability metadata.

No, the external code doesn't need to refine it directly. If we could pass it into setConfiguration() that'd be awesome! But we're unfortunately stuck with ConfigurablePluginInterface which only takes the one argument. :-/

If we add a 2nd argument, even if it's optional, that could cause problems with code that is designed to work generically with classes implementing ConfigurablePluginInterface which wouldn't know the special rules for dealing with VariantInterface. The same applies to adding a special setCacheableConfiguration() method. We discussed all of these options on the page_manager issue that spawned this: #2511570: Remove Drupal\page_manager\Plugin\PageAwareVariantInterface (see the comments near #62/#63)

We couldn't come up with a better solution that didn't break BC. But if you have any ideas, they're definitely welcome! :-)

xano’s picture

If the configuration passed to setConfiguration() came from CMI (which will be 99.99% of the time in Drupal 8), then cacheability metadata MUST be provided or the render array returned from VariantInterface::build() will have incorrect cacheability metadata on it, namely, the cache won't be cleared when the configuration changes.

The plugin is the provider of its own configuration. You shouldn't set any configuration through ConfigurablePluginInterface that you didn't first export through that same interface.

No, the external code doesn't need to refine it directly. If we could pass it into setConfiguration() that'd be awesome!

That would be very far from awesome, as explained earlier. Do not ever mass-import any configuration that you did not mass-export earlier. If you want to change configuration, that must be done through separate, plugin-specific methods on the plugin instance itself.

To summarize: please stop thinking about ConfigurablePluginInterface. It's not for what you are trying to achieve. You want to add plugin-(type-)specific features, then this must be done on the plugin (type), and not on generic plugin system interfaces.

Can you show me which variant plugins depend on which exact cacheable dependency configuration? I can then write some (pseudo) code to show how you can achieve the desired functionality.

berdir’s picture

@Xano:

That is not correct. The plugin *only* deals with displaying a form that allows a user to configure it. Plugins have no understanding of persistence and where and how things are saved and changed.

Take core blocks as an example. We don't have the problem there because we keep the render cache definitions outside of the plugin but it would have it if plugins would be responsible for defining that. You save block configuration in a block config entity and then display it. When that block is edited (through the plugin, but it doesn't know who is doing that and where it is saved), the block cache tag is invalidated and we need to invalidate all rendered variations of that block.

If you look at #2511570: Remove Drupal\page_manager\Plugin\PageAwareVariantInterface, you can see that it's a bit more complicated there. We have the page manager that has many block configurations, configured through a display plugin and stored in the page. Each block has it's own render cache definition (and the sum of all blocks with their access information is also cached again). All those blocks need to be invalidated if the page is resaved. And the display plugin needs to be able to add cache tags for the page manager page and it needs to get that information from somewhere. This is about providing that.

If you look at the page there, we already did it conditinally if the interface is implemented but I think it makes sense to include it by default so that they're aware of it.

dsnopek’s picture

Title: Allow passing cacheable metadata to display variants » Allow passing cacheable metadata to display variants (to enable Panels Everywhere)
Issue summary: View changes

Updated the issue summary and title to emphasize that Panels Everywhere will be impossible in Drupal 8 without this (or similar) change!

wim leers’s picture

wim leers’s picture

I think this is ready. My only concerns are about docs.

  1. +++ b/core/lib/Drupal/Core/Display/VariantInterface.php
    

    Does this interface need an explanation what the cacheability metadata it can contain is for?

    i.e. that it's about the configuration that this variant depends upon.

  2. +++ b/core/lib/Drupal/Core/Render/PageDisplayVariantSelectionEvent.php
    

    The docblock for this class says:

    /**
     * Event fired when rendering main content, to select a page display variant.
     *
     * Subscribers of this event can call the following setters to pass additional
     * information along to the selected variant:
     * - self::setPluginConfiguration()
     * - self::setContexts()
     *
     * @see \Drupal\Core\Render\RenderEvents::SELECT_PAGE_DISPLAY_VARIANT
     * @see \Drupal\Core\Render\MainContent\HtmlRenderer
     */
    class PageDisplayVariantSelectionEvent extends Event implements RefinableCacheableDependencyInterface {
    

    Shouldn't it then also explain that the cacheability metadata describes the plugin configuration?

dsnopek’s picture

StatusFileSize
new6.43 KB
new1.29 KB

Here's a new patch that should address Wim's review in #26

dsnopek’s picture

StatusFileSize
new6.42 KB
new910 bytes

Bah! English fail in the last patch. This should be better!

tim.plunkett’s picture

Status: Needs review » Reviewed & tested by the community

Thanks SO much @Wim Leers for the review.
And thanks @dsnopek.

webchick’s picture

Status: Reviewed & tested by the community » Fixed

Committed and pushed to 8.0.x. YEAH!

  • webchick committed 4b292ef on 8.0.x
    Issue #2569083 by dsnopek, tim.plunkett, Wim Leers: Allow passing...
dsnopek’s picture

Whoa! Fastest commit ever. :-) This now retroactively makes @webchick's earlier tweet 100% true!!

dsnopek’s picture

And, of course, thanks to everyone who helped get this one finished! Panels is now fully unblocked!!

The last submitted patch, 25: drupal8-cacheable-variants-2569083-25.patch, failed testing.

The last submitted patch, 27: drupal8-cacheable-variants-2569083-27.patch, failed testing.

Status: Fixed » Needs work

The last submitted patch, 28: drupal8-cacheable-variants-2569083-28.patch, failed testing.

tim.plunkett’s picture

Status: Needs work » Fixed

PIFR, so nice of you to get back to us!

Status: Fixed » Closed (fixed)

Automatically closed - issue fixed for 2 weeks with no activity.