Problem/Motivation
When attempting to alter the render arrays of multiple entities using hook_entity_display_build_alter()
, only the final entity in the $build_list
array is actually altered. This is because the wrong array key is used in this line:
\Drupal::moduleHandler()->alter('entity_display_build', $build_list[$key], $context);
In #2450151: Don't try to render all fields (including hidden ones) for single entity display the variable name for the array key used in the loop where that line of code appears was changed, but the variable name was not changed on that particular line of code.
Proposed resolution
Patch attached, which includes the correction, and a test.
Remaining tasks
Needs community review.
User interface changes
None.
API changes
hook_entity_display_build_alter()
will apply correctly to multiple entities passed in, not just to the last one.
Data model changes
None.
Comments
Comment #2
danmuzyka CreditAttribution: danmuzyka at Phase2 for Workday, Inc. commentedAdding another comment to the test in the patch.
Comment #3
jhedstrom@danmuzyka could you attach the test-only patch to demonstrate the issue and fix?
I think lowercase
@todo
is preferred, also without the:
. Additionally, I don't think new@todos
are allowed in core unless they link to an open issue, so adding that follow-up issue and adding the link to the code would be good.Comment #4
jhedstromNit: this exceeds the 80-character line-wrap limit by one :)
Comment #5
danmuzyka CreditAttribution: danmuzyka at Phase2 for Workday, Inc. commentedAttaching a modified version of the patch, and a patch with the test only, to demonstrate the failing condition.
Comment #7
danmuzyka CreditAttribution: danmuzyka at Phase2 for Workday, Inc. commentedThe TESTS_ONLY patch failed as expected.
Comment #8
jhedstromI think this is good to go. It addresses a clear bug, and demonstrates the issue with a test.
The follow-up issue in the
@todo
is #2755353: Add test coverage for entity view/render hooks.Comment #9
alexpottNo need to use
Markup::create()
here - #markup will do automatic XSS filtering anyways and adding more usages for Markup::create() where it is not necessary could cause proliferation of unsafe practices.I would add the entity ID in the alter and assert each entity's customised version exists. It is a more explicit test.
Comment #10
danmuzyka CreditAttribution: danmuzyka at Phase2 for Workday, Inc. commentedHere's an updated version of the patch based on @alexpott's feedback.
Comment #11
alexpottIf you put the full patch last then the test-only patch won't result in the issue being set needs work by the bot. I've adjusted the weights but i don't think it is enough to stop the test above marking this needs work.
Comment #13
jhedstromI think this is ready to go! Thanks @danmuzyka!
Comment #14
alexpottCommitted and pushed 7c0a466ba66121489c7cf7889e3c7453327d2ed8 to 8.2.x and 41663bd to 8.1.x. Thanks!