Support for Drupal 7 is ending on 5 January 2025—it’s time to migrate to Drupal 10! Learn about the many benefits of Drupal 10 and find migration tools in our resource center.
Setup
An image field on a node is displayed as a banner using DS Region Block.
Current result
Node 1: https://i.imgur.com/8c87FsY.png
Node 2: https://i.imgur.com/uEmUn3f.png
Expected result
Node 1: https://i.imgur.com/7M1Fx0i.png
Node 2: https://i.imgur.com/ZJF7ZPZ.png
DsRegionBlock should define #cache
settings, to prevent Drupal from caching the first entity it renders.
We decided to deprecate the functionality, we documented a better way to achieve the same functionality
Comment | File | Size | Author |
---|---|---|---|
#23 | 2669766-23.patch | 2.25 KB | aspilicious |
#8 | region_block_gets-2669766-8.patch | 2.26 KB | mr.baileys |
#3 | 2669766-cache-region-block.patch | 509 bytes | aspilicious |
#2 | 2669766-ds-region-block-caching.patch | 544 bytes | Algeron |
Comments
Comment #2
Algeron CreditAttribution: Algeron at XIO commentedProposed patch in attachment. A better solution would be to cache based on entity viewed rather than url.
Comment #3
aspilicious CreditAttribution: aspilicious commentedComment #4
aspilicious CreditAttribution: aspilicious commentedWe need a test for this, someone up for the job?
Comment #5
Screenack CreditAttribution: Screenack as a volunteer and commented@aspilicious: I have been seeing this very behavior on my development build. Switching 8.x-2.3 to dev, and running your patch #3 addresses this issue on my development site. Thanks!
Comment #6
weri CreditAttribution: weri at Previon Plus AG commentedI think this is not a problem of the block content and his caching information. It's a problem that the DsRegionBlock class does not add an additional context for the region block (maybe the viewed entity).
For example we can add an additional context based on the requested url in the DsRegionBlock class:
I don't know for the moment, how we can easy add a context to the entity which is displayed. But I think this would be the right approach to solve this problem.
Comment #7
Algeron CreditAttribution: Algeron at XIO commentedI also had to disable render cache in order to get it to work on the production environment.
$settings['cache']['bins']['render'] = 'cache.backend.null';
Comment #8
mr.baileysI started writing the test requested in #3. Attached is a test case for this particular scenario, it seems though that the region block caching-related functionality is broken in more ways than one: the DsRegionBlock::build() function relies on a global which is set in ds_extras_entity_view_alter(). However, an entity view might get cached in the render array, while the block that relies on the static being set is not cached or cached differently, causing the block to often just be empty (global not set, even though you are looking at an entity/view mode where a block region is defined/populated).
This seems to also cause some strange behaviour (which affects the attached test): if you put 2 blocks (one core block, one DS block region) in the left sidebar and view an entity, then return to block placement, move both blocks to the right sidebar and return to the entity, the core block will have moved to the right sidebar as expected, but the DS region block is missing until caches are cleared, after which it suddenly appears in the right sidebar along with the core block.
In order to finish the tests for the caching bug reported here, I think the above caching-related issue needs to be fixed first (either as part of this issue or in a separate issue).
I attempted to fix the issue by using a #lazy_builder to set the ds_region_block global value, but the lazy_builder is firing after the DsRegionBlock::build() invocation. A probably cleaner approach might be to have the block itself determine which node/entity is being viewed, and call entity rendering itself rather than relying on the brittle global?
Comment #9
BerdirI discussed this a bit with @aspilicious the other day and yes, the only sane option here is to instead use a block that just displays an entity with a given view mode.
ctools has such generic block plugin that supports any entity type.
Comment #10
Algeron CreditAttribution: Algeron at XIO commentedGood point, Berdir. Since your suggestion, I've been using "Entity view (Content)" blocks in every situation where I'd have used a DS region block before. No issues so far.
Comment #11
aspilicious CreditAttribution: aspilicious commentedI think we need to deprecate this feature in DS, adding a link to this issue and probably apply the patch in 2 for existing sites.
@berdir, @mr.baileys thoughts?
Comment #12
BerdirIt could still fail sometimes with that patch. It still assumes that the node is built in the same request as well.
If for some reason only the block is invalidated but the node is not, then that information won't exist and the block will be empty.
Comment #13
4aficiona2 CreditAttribution: 4aficiona2 commentedWhat is the suggestion for existing D8 sites which already depend on DS block region? Two sites will soon get launched by us (end of April), depend on DS block regions and have caching issues like mentioned above. Those issues occurred after the deployment on the staging where caching is on, locally during development caching was off and worked during development like expected.
How to handle this situation more short term / more mid and long term?
Comment #14
4aficiona2 CreditAttribution: 4aficiona2 commentedI disabled the DS block region feature and handle those situation with a preprocessor function where I provide certain variables for Twig which I then render in the page template.
I used to pull out the title into a cover region (above ) the content with DS block regions but now do this manually by content type and provide a variable in the page template.
This works fine for me and there are no more caching issues like I had with DS block regions. Hopefully this helps somebody who used to use DS block regions.
Comment #15
Screenack CreditAttribution: Screenack as a volunteer and commented4aficiona2 brilliant idea — thanks for sharing.
Comment #16
4aficiona2 CreditAttribution: 4aficiona2 commented@screenack you're welcome ;-)
Here is the corresponding page template (
page.html.twig
) section. Beside the title I also provide a background image and there is some logic for the front cover classes. Hope this helps too. And a scroll to main content link.Comment #17
nicholas.alipaz CreditAttribution: nicholas.alipaz at L.A. BioMed commentedThe alternative solution to DS using ctools is not immediately apparent as described in this thread. Berdir can you clarify what you meant in #9? thanks
Comment #18
nicholas.alipaz CreditAttribution: nicholas.alipaz at L.A. BioMed commentedSo, I assumed what was meant by Berdir was to use page manager and the block layout plugin with ctools, however I only experience errors and I abandoned the idea. I ended up using the field as block module and things work as needed.
Comment #19
BerdirNo, this is not about page_manager. You can place ctools Entity view blocks in the normal block layout as well.
Comment #20
nicholas.alipaz CreditAttribution: nicholas.alipaz at L.A. BioMed commentedBerdir, thanks for the clarification, however in testing I was not able to get Entity View blocks using the core block layout to render anything in 8.1.0, I doubt I am making an user error, but who knows.
Comment #21
aspilicious CreditAttribution: aspilicious commentedDid you install the ctools module? That one is needed.
Comment #22
nicholas.alipaz CreditAttribution: nicholas.alipaz at L.A. BioMed commentedaspilicious, thanks for the suggestion. I do have ctools enabled, but looks like "Chaos tools blocks" is not. that is likely the issue I would guess.
Comment #23
aspilicious CreditAttribution: aspilicious commentedHere is a patch that marks the functionality as deprecated with a link to a new handbook page I created.
I hope this is enough to help people with reconfiguring their website.
Comment #24
aspilicious CreditAttribution: aspilicious commentedHere is a link to the new handbook page: https://www.drupal.org/node/2754967
Comment #25
aspilicious CreditAttribution: aspilicious commentedComment #27
aspilicious CreditAttribution: aspilicious commentedSo yeah this is kinda "fixed".