Score:0

Which cache should I invalidate in a block plugin that builds render arrays for one or more views?

in flag

The real issue is that the plugin will sometimes be returning render arrays that are discrete block displays from the same view, possibly also with different arguments. What I'm discovering is that in these cases, the first render array is cached so that the subsequent displays are the same as the initial one. I'm building the render arrays with buildRenderable($display_id, $args, FALSE), which will apparently disable tag and keys caching. But, I'm unclear how to manually provide a custom cache context without a varying request.

4uk4 avatar
cn flag
This is related https://drupal.stackexchange.com/questions/307728/render-view-inside-block-with-caching. The best strategy for your custom cache context depends on how many variations this context is producing.
in flag
I've seen the issue you referenced, could you elaborate on what you mean in your comment @4uk4?
Score:1
cn flag

But, I'm unclear how to manually provide a custom cache context without a varying request.

There are three strategies:

  1. Add the custom cache context to the render array, then the cache context bubbles up to the block and page adding variations on each level upstream.

  2. Add a lazy-builder and set the property #create_placeholder. Then the custom cache context doesn't bubble up anymore. Instead the cached block and page includes the placeholder which gets replaced by the lazily built content later when the cached page is delivered.

  3. Add a cache max-age of 0. Blocks are already built in a lazy builder and are placeholdered if the content meets the auto-placeholder condition. This results in a block never cached and always placeholdered.

Use the first option for a cache context not having too many variations. The second option is a bit more effort to implement, since Drupal 8.8 you need an extra class implementing TrustedCallbackInterface or RenderCallbackInterface, but is more flexible to define static and dynamic content, even within the same block. Easier is the third option, use this if the block content is so volatile that caching is not worth the effort.

in flag
Does it make sense that the `content` variable in the template contains two distinct render arrays?
4uk4 avatar
cn flag
Yes, often you use containers to separate them in a renderable structure which is also themable. See https://api.drupal.org/api/drupal/core%21lib%21Drupal%21Core%21Render%21Element%21Container.php/class/Container
in flag
I guess what I was asking is, the `content` variable in the template contains the _correct_ render arrays, using the discrete view display ids. However, they are not in the end rendered as distinct, which is the problem I'm having. I'm simply rendering the `content` with `{% for value in content %} {{ value }} {% endfor %}`.
4uk4 avatar
cn flag
Normally the variable name `content` in a Drupal template is a render array rendered recursively like any other render array without assuming a specific structure. If you have control on both ends of the variable you could make such assumptions, but I wouldn't do this. If someone else is not aware of that and deletes the customized block template or switches to a different theme not containing this template things will break.
4uk4 avatar
cn flag
If you need to render separate variables then add a custom template to your module for the inside and use the block template to theme the outside of the block if necessary.
in flag
I think I'm probably not being clear. What we're discussing is a custom module, with a block plugin class that returns one or more view blocks as render arrays in the `build` method. These render arrays are themed in a custom block template in the module. My question in these comments about the `content` variable that is passed to the template is this: Does it makes sense, with regard to the issue I'm having with render array caching, to have the _correct_, distinct render arrays passed to the template, which are still nevertheless being finally being incorrectly rendered as the same?
4uk4 avatar
cn flag
We are discussing the same, the difference is that I'm suggesting these render arrays are themed in a custom template which is not a block template. See https://www.drupal.org/docs/theming-drupal/twig-in-drupal/create-custom-twig-templates-for-custom-module#s-step-33-render-as-part-of-another-plugin-such-as-block.
in flag
Let us [continue this discussion in chat](https://chat.stackexchange.com/rooms/135849/discussion-between-steve-and-4uk4).
Score:0
in flag

In the end, I could not for all these suggestions, get the first array not the cache. This could still be due to a deficiency in my process, but I finally just used two discrete views, rather than trying to get the arrays to vary on the display id for a single view.

mangohost

Post an answer

Most people don’t grasp that asking a lot of questions unlocks learning and improves interpersonal bonding. In Alison’s studies, for example, though people could accurately recall how many questions had been asked in their conversations, they didn’t intuit the link between questions and liking. Across four studies, in which participants were engaged in conversations themselves or read transcripts of others’ conversations, people tended not to realize that question asking would influence—or had influenced—the level of amity between the conversationalists.