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

Created on 2 January 2025, 19 days 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.

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.

🐛 Bug report
Status

Active

Version

10.3

Component

taxonomy.module

Created by

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

Merge Requests

Comments & Activities

  • Issue created by @s-ude
  • First commit to issue fork.
  • Merge request !107883496939 - Taxonomy tokens update → (Open) created by Unnamed author
  • Pipeline finished with Failed
    18 days ago
    Total: 604s
    #385121
  • Pipeline finished with Failed
    17 days ago
    Total: 488s
    #385749
  • Pipeline finished with Failed
    17 days ago
    Total: 2139s
    #385776
  • Merge request !108003496939 - Use entity name instead of label → (Open) created by Unnamed author
  • Pipeline finished with Failed
    17 days ago
    Total: 112s
    #385836
  • Pipeline finished with Success
    17 days ago
    Total: 545s
    #385837
  • 🇮🇳India ramprassad

    ramprassad changed the visibility of the branch 3496939-taxonomy-token-entityname to hidden.

  • 🇮🇳India ramprassad

    The entity:name token refers to the label() only in taxonomy term and vocabulary entities. The same token returns title for node, subject for comment and the account name for user. So I think it makes sense to use the name for taxonomy and vocabulary. This change would require the test in the token module to be updated as well for which I may need to create a new issue.

  • 🇮🇳India ramprassad

    Unassigned and raised the MR

  • 🇨🇭Switzerland berdir Switzerland

    Since there is no label token and by default name is the same as the label, I'm not entirely sure that this is really desired. It's just as likely that someone relies on this behavior.

    This was recently changed in 🐛 Return translated term name on views "Content: Has taxonomy term ID (with depth)" Fixed but getName() always used label(), at least since 2014, so I'm not exactly sure when and why this would have changed.

    If you customize label() you can also implement your own token that returns what you wan't.

Production build 0.71.5 2024