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

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

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
    4 months ago
    Total: 604s
    #385121
  • Pipeline finished with Failed
    4 months ago
    Total: 488s
    #385749
  • Pipeline finished with Failed
    4 months ago
    Total: 2139s
    #385776
  • Merge request !108003496939 - Use entity name instead of label → (Open) created by Unnamed author
  • Pipeline finished with Failed
    4 months ago
    Total: 112s
    #385836
  • Pipeline finished with Success
    4 months 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.

  • 🇳🇿New Zealand quietone

    Changes are made on on 11.x (our main development branch) first, and are then back ported as needed according to the Core change policies .

  • I agree that Term::getName() seems to have always used Term::label(), which I criticised in this ticket: https://www.drupal.org/project/drupal/issues/3223302

    We had to patch the Term::getName() method to get around the bug that I described in the linked ticket. Without that patch it is basically not possible to have your own implementation of the Term::label() method because it breaks terms when you try to save them.

    I guess, you could say, the issue that we experienced here is because of a combination of the name token using Term::label() and Term::getName() using Term::label(). We just had a patch in place for the latter already, which is why we did not notice it within the scope of this ticket.

    Apart from all of this, I believe that my point remains: It is possible to fetch the value of all taxonomy term fields (even custom fields) with tokens, except for the name field. That's the only field that relies on the output of the label() function. Other entity types do not seem to have this problem.

    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.

    If someone has changed the Term::label() method, they have very likely also updated the Term::getName() method because of the bug of which I have linked the report. If that's the case, they had the token name return the actual name of the term in Drupal 9, just like we did, until the token implementation changed from Term::getName() to Term::label(). I would conclude that it is not very likely that someone relies on the Term::label() output being used specifically for the name token.

    I hope I did not misunderstand anything. Please let me know your thoughts and let me know if I missed a point.

Production build 0.71.5 2024