Problem/Motivation
EntityReferenceFormatterBase::view() is applying directly the field level access cacheability to the render array which is removing any already present cacheable metadata.
This could happen if you have a field formatter extending from this one and e.g. in ::viewElements add not only the children to the render array, but also cacheable metadata at the first level of the render array. In this case the cacheable metadata will simply be overwritten by EntityReferenceFormatterBase::view(), but it should be merged instead.
Proposed resolution
Replace
$field_level_access_cacheability->applyTo($elements);
with
$field_level_access_cacheability->merge(CacheableMetadata::createFromRenderArray($elements))->applyTo($elements);
Remaining tasks
User interface changes
API changes
Data model changes
| Comment | File | Size | Author |
|---|---|---|---|
| #10 | interdiff-7-10.txt | 1.69 KB | Maouna |
| #10 | cache-formatter_merge_cache_data-2854454-10.patch | 3.07 KB | Maouna |
| #10 | cache-formatter_merge_cache_data-2854454-10-FAIL.patch | 2.19 KB | Maouna |
| #7 | cache-formatter_merge_cache_data-2854454-7.patch | 3.16 KB | Maouna |
| #7 | 3-7-interdiff.txt | 1.82 KB | Maouna |
Comments
Comment #2
hchonovComment #3
hchonovComment #4
hchonovComment #5
Maouna commentedComment #6
Maouna commentedFailing test for demonstration.
Comment #7
Maouna commentedAdded the test.
Comment #9
hchonov"Custom cache tag" should be sufficient.
Why do you extend from EntityReferenceIdFormatter instead from EntityReferenceFormatterBase?
The empty lines are not really needed.
Comment #10
Maouna commentedThanks @hchonov for the feedback. I adapted the patches accordingly.
Comment #12
berdirLooks good to me, fix makes sense and has a test.
Comment #13
hchonovComment #16
catchFixed this on commit:
Committed/pushed to 8.4.x and cherry-picked to 8.3.x. Thanks!