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.
Problem/Motivation
The block_field formatters don't propagate the access cacheability metadata properly.
This can cause problems, for example, if the referenced block is automatically generated (as a derivative) from an entity that can be unpublished. In such cases, when you unpublish the entity the cache should be invalidated but it doesn't.
Proposed resolution
Simply apply the cacheability metadata from the access object to the render array when appropriate on the formatter classes.
Comment | File | Size | Author |
---|---|---|---|
#27 | interdiff-26-27.txt | 3.49 KB | sardara |
#27 | 3114053-27.patch | 13.33 KB | sardara |
| |||
#26 | interdiff-25-26.txt | 8.14 KB | sardara |
#26 | 3114053-26.patch | 13.38 KB | sardara |
| |||
#25 | interdiff.txt | 11.44 KB | paulocs |
Comments
Comment #2
ieguskiza CreditAttribution: ieguskiza at European Commission and European Union Institutions, Agencies and Bodies commentedI attach two patch files: one with tests that highlight the problem and another with the solution.
Comment #3
Chewie CreditAttribution: Chewie at Petend for European Commission and European Union Institutions, Agencies and Bodies commentedI have tested this patch together with https://www.drupal.org/project/block_field/issues/3114056
Fix and tests looks good!
Just 2 questions:
- Why we didn't include this fix also in these changes (https://www.drupal.org/project/block_field/issues/3114056) issue for make testing simpler.
- little question regarding this part:
Are we really need this part of setUp()?
Comment #4
Chewie CreditAttribution: Chewie at Petend for European Commission and European Union Institutions, Agencies and Bodies commentedMaybe it is simpler to use the existing test module 'block_field_test' and implement the plugin in the existing one.
Comment #5
ieguskiza CreditAttribution: ieguskiza at European Commission and European Union Institutions, Agencies and Bodies commentedI took your comments into account @chewie and provided a new set of patches.
Comment #6
ieguskiza CreditAttribution: ieguskiza at European Commission and European Union Institutions, Agencies and Bodies commentedIt was mentioned to me that we should also make sure the tags are propagated if the access is denied. I made some adjustments to the kernel test to take this into consideration. I attach the new patches with the interdiffs.
Comment #7
Chewie CreditAttribution: Chewie at Petend for European Commission and European Union Institutions, Agencies and Bodies commentedChanges looks good.
But first should be merged this issue: https://www.drupal.org/project/block_field/issues/3068281
Comment #8
sardara CreditAttribution: sardara at Randstad Digital for European Commission and European Union Institutions, Agencies and Bodies commentedNit: remove empty line.
$element is not used.
The comment could be improved, the block is still available, it's just unpublished.
Again, let's talk about visibility/published and not availability.
Same here.
I don't think we should use class properties for these. For example, in the test method you end up creating a local variable for $fieldName, which outlines how we don't need these. Let's just use the strings around.
Nit: double
>
at the end.AllowedIf
(capital I)assertEquals()
assertEquals()
Nit: empty line after function closing bracket.
Nit: remove empty line.
To make sure that our block is working well, let's assert that the rendered markup contains or doesn't contain the label.
In order to render the block properly, make sure to include the block module in the list of modules above.
Also, add an extra message in the loops assertions to understand on which formatter they would be failing.
We could have used the block_content module to make this test, but I have noticed that the block label formatter would return only one cache tag (block_content:) while the other formatter would return also the list cache tag. We can keep our custom block for ease of test.
Comment #9
sardara CreditAttribution: sardara at Randstad Digital for European Commission and European Union Institutions, Agencies and Bodies commentedComment #10
sardara CreditAttribution: sardara at Randstad Digital for European Commission and European Union Institutions, Agencies and Bodies commentedSince the tests cannot be green unless #3068281: BlockFieldLabelFormatter CacheableMetadata::createFromRenderArray points to incorrect array is merged first, add here a patch that includes the other one too, so we see the tests are green.
Comment #11
ieguskiza CreditAttribution: ieguskiza at European Commission and European Union Institutions, Agencies and Bodies commentedHi @sardara,
thanks for the input. I fixed all the comments you had and as you proposed I add a new patch that merges the two solutions to showcase everything being green.
Comment #12
ieguskiza CreditAttribution: ieguskiza at European Commission and European Union Institutions, Agencies and Bodies commentedReupload of the patch that contains both solutions using the backwards compatible code.
Comment #13
ieguskiza CreditAttribution: ieguskiza at European Commission and European Union Institutions, Agencies and Bodies commentedComment #14
sardara CreditAttribution: sardara at Randstad Digital for European Commission and European Union Institutions, Agencies and Bodies commentedLet's remove also this property, like the others.
There are also some double empty lines, let's clear those and for me we are good to go.
Comment #15
ieguskiza CreditAttribution: ieguskiza at European Commission and European Union Institutions, Agencies and Bodies commentedFixed the latest comments, attached are all the patches and interdiffs.
Comment #16
ieguskiza CreditAttribution: ieguskiza at European Commission and European Union Institutions, Agencies and Bodies commentedFixed minor issue with previous patches.
Comment #17
sardara CreditAttribution: sardara at Randstad Digital for European Commission and European Union Institutions, Agencies and Bodies commentedWe can use @inheritdoc here.
Missing @inheritdoc.
Let's use "Tests that block access cache metadata is propagated".
Also the method name, let's use "testBlockAccessCacheMetadata" or similar.
We are asserting that they are not rendered here.
Double empty line.
Comment #18
ieguskiza CreditAttribution: ieguskiza at European Commission and European Union Institutions, Agencies and Bodies commentedI fixed the last comments and uploaded a fresh batch of patches.
Comment #19
sardara CreditAttribution: sardara at Randstad Digital for European Commission and European Union Institutions, Agencies and Bodies commentedGood for me! Moving to RTBC.
Comment #20
BerdirThis will need a an update after the Drupal 9 patch has been committed.
Comment #21
paulocsPatch re-rolled.
Comment #22
paulocsPatch #21 is wrong.
Attaching new one.
Comment #23
BerdirLooks good to me, great test coverage. However, it already needs another reroll due to the coding standard changes I think.
Comment #24
BerdirThis also contained the fix for #3068281: BlockFieldLabelFormatter CacheableMetadata::createFromRenderArray points to incorrect array but I think without test coverage for that unlike the other issue, so I committed that one, another thing to check when rerolling.
Comment #25
paulocsAttached a new patch.
Comment #26
sardara CreditAttribution: sardara at Randstad Digital for European Commission and European Union Institutions, Agencies and Bodies commentedLatest uploaded re-roll is missing some of the original test coverage.
Comment #27
sardara CreditAttribution: sardara at Randstad Digital for European Commission and European Union Institutions, Agencies and Bodies commentedMissed the coding standards fixes.
Comment #28
paulocsPatch looks good to me!
Comment #30
paulocsFixed in dev branch.