Problem/Motivation
When used with a field formatter where each field item has a cache key set (such as on an entity reference field with the rendered entity formatter) the suffix is appended to this item which can put incorrect markup into the render cache for the field item value.
Reporting as a new issue - even though I suspect
https://www.drupal.org/project/field_delimiter/issues/3321484 →
is a result of this issue - there is not enough details in that issue to be sure this is a duplicate.
Steps to reproduce
Using this module with standard install profile
- On article default display - configure the tags field to use Rendered entity, use default view mode and add a delimiter
- On article teaser display - configure the tags field to use Rendered entity, use default view mode and add a different delimiter than above
- Add an article with more that 1 tag.
- View the article in the full view mode
- View the article in the teaser view mode
Observe that the field is rendered on the teaser view mode with the settings from the default view mode.
This is because the field item will have cache keys set - e.g
["entity_view", "taxonomy_term", "1", "default"]
And the preprocess function is adding #suffix
to that render array - so if it is the first time being rendered it will include the suffix as part of the rendered HTML for that entity (the referenced taxonomy term).
This is not correct behaviour. When rendered elsewhere, this information which should be related only to field / view mode of the host entity is now part of the render cache for the entity being referenced.
Likewise, if the taxonomy term has been first rendered elsewhere using the same view mode but outside the context of a field with a delimiter, it will cache the correct markup, but when the node / field with the delimiter set is rendered it is using the cached markup and will not render the prefix added by this module.
Proposed resolution
I initially thought of adding a representation of the delimiter as the final part of the cache key - but this would reduce the cacheability of rendered entities - and require work to ensure we are using something that can safely be used as keys (e.g the delimiter can contain HTML so I wasn't immediately sure of supported characters for cache keys).
So perhaps a simpler alternative is to use a nested render array instead of a suffix. Given the delimiter is already sanitized it could so something like:
$content_with_suffix = [
$item['content'],
['#markup' => $safe_delimiter],
];
$item['content'] = $content_with_suffix;
This would keep the cache benefits of using the rendered entity cache and allow the module to insert the delimiter in the same position.
Remaining tasks
- Agree on approach
- Patch
- Review
- Commit
User interface changes
n/a
API changes
n/a
Data model changes
Proposed approach would change modify the field item content render array to be a nested array contain the original render array and the suffix.