Use labels in Views argument summaries for entity references

Created on 20 April 2024, 7 months ago
Updated 15 June 2024, 5 months ago

Problem/Motivation

Follow-up to ๐Ÿ› Fix label token replacement for views entity reference arguments Fixed 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

  1. Install Drupal 10.2.5 (for example, just about any modern Drupal will do)
  2. Add an entity reference field to an entity type (e.g. add a 'field_tags' reference to a 'Tags' taxonomy vocabulary)
  3. Create some tag terms
  4. Create some articles that point to tags
  5. Create a View of articles with an argument on the 'field_tags' entity reference
  6. Configure the argument that if the value is not provided to generate a summary
  7. Visit the summary
  8. 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 ๐Ÿ› Fix label token replacement for views entity reference arguments Fixed 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

  1. Amazingly, it doesn't seem so.
  2. Done.
  3. Reviews / refinements
  4. RTBC
  5. 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

๐Ÿ› Bug report
Status

Fixed

Version

10.3 โœจ

Component
Viewsย  โ†’

Last updated about 3 hours ago

Created by

๐Ÿ‡บ๐Ÿ‡ธUnited States dww

Live updates comments and jobs are added and updated live.
Sign in to follow issues

Merge Requests

Comments & Activities

Production build 0.71.5 2024