Problem/Motivation
Follow-up to #2640994: Fix label token replacement for views entity reference arguments to also fix argument summaries for entity references. #2912332: Allow entity reference views arguments to display the entity label in titles and summaries proposed doing both, was marked as duplicate with #2640994, but #2640994 doesn't deal with summaries. They currently use the entity ID as the link text, not the (correctly translated) label as everyone expects.
Steps to reproduce
- Install Drupal 10.2.5 (for example, just about any modern Drupal will do)
- Add an entity reference field to an entity type (e.g. add a 'field_tags' reference to a 'Tags' taxonomy vocabulary)
- Create some tag terms
- Create some articles that point to tags
- Create a View of articles with an argument on the 'field_tags' entity reference
- Configure the argument that if the value is not provided to generate a summary
- Visit the summary
- Behold term IDs, not tag labels
Views was created with plumbing to handle this case (the summaryName() method in argument handlers), but we're not taking advantage of that functionality for entity reference fields.
Proposed resolution
Once #2640994: Fix label token replacement for views entity reference arguments lands, add something like this to core/modules/views/src/Plugin/views/argument/EntityReferenceArgument.php:
/**
* {@inheritdoc}
*/
public function summaryName($data) {
$id = $data->{$this->name_alias};
$entity = $id ? $this->entityTypeManager->getStorage($this->definition['target_entity_type_id'])->load($id) : NULL;
if ($entity) {
return $this->entityRepository->getTranslationFromContext($entity)->label();
}
if (($id === NULL || $id === '') && isset($this->definition['empty field name'])) {
return $this->definition['empty field name'];
}
return $id;
}
Remaining tasks
Land #2640994: Fix label token replacement for views entity reference argumentsSee if we've got existing test coverage of summaries we can extend for thisAmazingly, it doesn't seem so.If not, convert the steps to reproduce into a test (ideally Kernel)Done.Take the code from the summary and put it in an MR- Reviews / refinements
- RTBC
- Commit & rejoice
User interface changes
No changes to the Views UI, just that Views start working as expected.
Before

After

API changes
Not really a change, but core/modules/views/src/Plugin/views/argument/EntityReferenceArgument overrides summaryName() from its parent (ArgumentPluginBase)
Data model changes
Release notes snippet
| Comment | File | Size | Author |
|---|---|---|---|
| #15 | Screenshot 2024-05-31 at 2.26.44 PM.png | 18.95 KB | smustgrave |
| #15 | Screenshot 2024-05-31 at 2.26.11 PM.png | 14.66 KB | smustgrave |
Issue fork drupal-3442227
Show commands
Start within a Git clone of the project using the version control instructions.
Or, if you do not have SSH keys set up on git.drupalcode.org:
- 3442227-use-labels-entity-reference-arg-summary
changes, plain diff MR !7678
Comments
Comment #2
dwwComment #3
dwwComment #5
dwwThis is technically still blocked on #2640994: Fix label token replacement for views entity reference arguments. I pushed a single commit with all the changes from that issue into this issue fork, then another commit to actually fix the bug here. It'll be trivial to rebase this and remove the 1st commit once #2640994 lands. But I wanted to get this moving in the hopes we can fix both in time for 10.3.0.
Comment #6
dwwNote to reviewers: Until #2640994 lands, the "Changes" tab in the MR is a ton of stuff you don't need to look at. This is the only change being proposed here:
https://git.drupalcode.org/project/drupal/-/merge_requests/7678/diffs?co...
Thanks,
-Derek
Comment #7
smustgrave commentedOnly comment is is it possible to add typehint and return?
Comment #8
dwwThanks for taking a look!
Re types: I don’t believe so, since this is implementing a parent method that doesn’t have types and that’d be an api change. I believe. 😂 I’m honestly not sure what our policy is on that.
Comment #9
smustgrave commentedNot 100% but if it doesn’t break anything adding I’ve seen typehints added. But if it does break then it gets left.
Not sure the plan in the future to go back and update these
Comment #10
dwwI tried this locally:
Then this:
Which resulted in this:
Reverting the type changes to
summaryName()and that test is passing fine. So either I botched it somehow, or this just won't work until we add types to Views plugins (or at least toArgumentplugins). /shrugComment #11
dwwNeeds testsI searched (a lot), and didn't find any test coverage of argument summaries. So I wrote a Kernel test for this on a flight earlier today. The MR still has a ton of changes since this needs #2640994: Fix label token replacement for views entity reference arguments to land. But I squashed that into a single commit in this issue fork, so it'll be easy to rebase that out of here once that issue lands. If anyone wants to review, the actual changes are just 3 commits:
https://git.drupalcode.org/project/drupal/-/merge_requests/7678/diffs?co...
https://git.drupalcode.org/project/drupal/-/merge_requests/7678/diffs?co...
https://git.drupalcode.org/project/drupal/-/merge_requests/7678/diffs?co...
Comment #12
dwwp.s. I tried triggering the test-only GitLab CI job, but it's all confused by the #2640994 changes. Probably test-only via GitLab won't tell us much until that issue lands. However, locally, if I revert the fix and just run the new test, I see:
Which is exactly what we'd expect. The summary is only showing term IDs and the node counts, not the term labels...
Comment #13
dwwWoo hoo, the blocker is in. This needs a rebase, but then should be ready. I’ll do it later tonight when I’m back at a computer, unless someone else is inspired to do it before then.
Comment #14
dwwReady for review (and hopefully quick RTBC). Would be lovely to also get this in before 10.3.0.
Thanks!
-Derek
Comment #15
smustgrave commentedThanks for doing all the review steps @dww haha
Pushed some nitpicky typehints. Wonder if we need a follow up as summaryName() has no return documented, which seems out of scope of this issue definitely.
Confirmed the issue following the steps
Before
After
LGTM!
Comment #23
catchThis looks good. Adding some credit over from #2912332: Allow entity reference views arguments to display the entity label in titles and summaries.
Committed/pushed to 11.x and cherry-picked back through to 10.3.x, thanks!