Changed Method label() in Taxonomy Terms results in Field/Name Changes

Created on 12 July 2021, almost 4 years ago
Updated 10 May 2024, 12 months ago

Problem/Motivation

The method label() of entities is abstract and does not (necessarily) correspond to one specific field. Some entity types have implemented a specific version of that method while taxonomy terms do have a corresponding field (name) by default. Which is why those three expressions are basically identical:

  • $term->get('name')->value
  • $term->getName()
  • $term->label()

Please correct me if I'm wrong but I think that the method label() is not supposed to change any actual functionality. It is there to show a string representation of that entity. Nothing else.

In a project I needed that string representation to be different for taxonomy terms, so I changed the method label() (by using my own class for taxonomy terms). Now here is what happened. As soon as I saved a taxonomy term, the field name was lost and instead the output of the label() method was saved in that field.

After debugging this for a bit, I found that the method getName() was using the method label() instead of fetching the content of the field name directly. I assume that the method getName() is used when a term is saved to fetch the current value of the name field, but I don't think that we should rely on the label() method returning that field. If we do, why even have a label() method in the first place? As soon as we change it, the name field is not usable anymore. At least not manually. It will always hold a copy of the label() result.

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
    • I think there is also a callback method that can be registered which is but marked as deprecated
  • Save a taxonomy term
  • Edit the taxonomy term again
  • The field name has changed

Proposed resolution

I don't think that the method getName() should be using the method label(). Instead it should return the name of the taxonomy term, just as the method name suggests: return this->get('name')->value;.

I would like to hear your thoughts and please correct me if I misunderstood anything.

πŸ› Bug report
Status

Needs work

Version

11.0 πŸ”₯

Component
TaxonomyΒ  β†’

Last updated about 9 hours ago

  • Maintained by
  • πŸ‡ΊπŸ‡ΈUnited States @xjm
  • πŸ‡¬πŸ‡§United Kingdom @catch
Created by

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

Comments & Activities

Not all content is available!

It's likely this issue predates Contrib.social: some issue and comment data are missing.

  • First commit to issue fork.
Production build 0.71.5 2024