Token "entity:name" seems to use label()

Created on 16 December 2024, 4 months ago

Problem/Motivation

When I use a token pattern like this one: [node:field_my_category:entity:name]
... I would expect the name field of the taxonomy term (my_category) to be used. But instead the label() method of that entity seems to be called.

I think that, because in our project we have updated the label() method for taxonomy terms. It does not return the name field anymore. Instead it is producing another string representation of the taxonomy term (based on a custom field). And in that scenario the name field is ignored and our custom field is used when the token pattern is resolved.

I have found a test case (Drupal\Tests\token\Kernel\EntityTest::testEntityMapping()) that is comparing [term:name] with $term->label(), but I don't think that is a correct test, because the name and the label are two different things. If we use the token name for the label of the entity, I don't think there is a way to express the actual name field of the term, which is possible for every other field.

This behaviour was different in the past. I think with Drupal 9, the actual name field was used.

The issue may be related to a problem that I described in this ticket: https://www.drupal.org/project/drupal/issues/3223302

Steps to reproduce

  • Update the method label() for your taxonomy terms
    • Overwrite the class for taxonomy terms (which I did), extend the original class and overwrite the method label().
    • Write a temporary core patch
  • Use a token pattern (e.g. create a URL alias pattern) that uses the name token part of the taxonomy term entity.
  • The resolved string will use the output of the custom label() method instead of the name.

Proposed resolution

I would suggest to...

  • ... either change back the current behaviour so that the entity:name is returning the content of the name field of the entity, like it was in Drupal 9. There could be another keyword like entity:label that is using the label() method.
  • ... or add any other possibility to use the name field, like entity:field_name or entity:field:name

If the latter already exists, then I must have missed it. I browsed the available tokens in the backend, but I did not find any way to get the name field of a taxonomy term.

🐛 Bug report
Status

Active

Version

1.10

Component

Code

Created by

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

Comments & Activities

Production build 0.71.5 2024