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.
Even though the test case I mentioned above is located in the token
module, the ticket that I opened for the token
project was closed and I was directed to the core project instead:
https://www.drupal.org/project/token/issues/3494279
🐛
Token "entity:name" seems to use label()
Active
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 term, 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.