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
In https://www.drupal.org/node/2916667 a standardised way to implement computed fields has been introduced. However, if you create an entity reference computed field, whose field item list class extends the EntityReferenceFieldItemList
class, you cannot use the ::referencedEntities()
method. This is proved by the attached test.
Proposed resolution
- Use
::isEmpty()
in::referencedEntities()
to precompute the values.
Remaining tasks
None.
User interface changes
None.
API changes
None.
Data model changes
None.
Comment | File | Size | Author |
---|---|---|---|
#9 | 2978848-9.patch | 4.52 KB | claudiu.cristea |
#9 | 2978848-9.interdiff.txt | 2.13 KB | claudiu.cristea |
#5 | entityreference-computed-fail.patch | 4.08 KB | claudiu.cristea |
Comments
Comment #2
claudiu.cristeaThe fix.
Comment #3
claudiu.cristeaComment #4
claudiu.cristeaComment #5
claudiu.cristeaOuch... the failing patch
Comment #6
amateescu CreditAttribution: amateescu for Pfizer, Inc. commentedThe test-only patch from #5 looks very good, but I'm not sure about the fix. The proposed fix from #2 would require that every computed entity reference field item list to know that it needs to use a different trait than the standard one.
I think this should be better and actually fix all existing computed reference fields without the need for any changes on their part.
It would also mean that we don't need any change record update :)
Comment #7
amateescu CreditAttribution: amateescu for Pfizer, Inc. commentedComment #9
claudiu.cristea@amateescu, yes that was my first thought but I missed that
->isEmpty()
is aware of computed fields so I thought that it would make the method too complex :)Comment #10
claudiu.cristeaUntagged, updated IS.
Comment #11
claudiu.cristeaComment #12
amateescu CreditAttribution: amateescu for Pfizer, Inc. commentedCool, looks good to me then :)
Comment #13
Sam152 CreditAttribution: Sam152 as a volunteer and at PreviousNext commentedThis is an elegant fix, nice!
I also did a quick scan for other instances of
empty($this->list)
which could be similarly problematic, but the only occurrence inCommentFieldItemList
doesn't look like it would be appropriate for use with the trait/computed values. +1 RTBC.Comment #14
claudiu.cristeaComment #15
alexpottCommitted and pushed 7896891da5 to 8.6.x and 7dfb2637ff to 8.5.x. Thanks!
Comment #19
jibran