3

I've been to figure out how I can output the machine-name of a taxonomy term on my entity field to my twig template. using .label prints out the name of the taxonomy term. .id get a numerical value, was actually hoping to get the machine term instead. Using kint I can't seem to find it

{% for themes in node.field_themes %}

        <span>{{ themes.entity.id }}</span>
{% endfor %}
3
  • 1
    You can try this one {{ themes.entity.name|lower|replace({' ': '-'}) }} Jul 26, 2018 at 10:02
  • using .entity.id output a numerical value
    – clestcruz
    Jul 26, 2018 at 10:04
  • 1
    @AlexKuzava the correct way is {{ themes.entity.label|lower|replace({' ':'-'}) }} but thanks for the hint. Kindly submit this as an answer
    – clestcruz
    Jul 28, 2018 at 5:05

3 Answers 3

3

I see, the output I'm looking for somewhat like the key value of a list field. For example Taxonomy term it becomes taxonomy-term

As discussed the correct way is

{{ themes.entity.label|lower|replace({' ':'-'}) }}

It's suitable for other entity types as well

3

I found this to work in the taxonomy term individual twig template: taxonomy-term.html.twig :

{% set term_machine_name = term.name.value|raw|lower|replace({' ':'-'})|replace({',':'-'}) %}

Then use term_machine_name as required.

If you are outputting the Taxonomy term: Name field in a View, I found this to work, in the rewrite results setting in the Views Display Config, when editing a View in the site's admin pages:

<a href="#{{ name |lower|replace({' ':'-'})|replace({',':'-'}) }}">{{ name__value }}</a>
1

Taxonomy terms are content entities, and don’t have machine names.

They have IDs, that’s how they’re generally identified.

2
  • I see, the output I'm looking for somewhat like the key value of a list field. For example Taxonomy term it becomes taxonomy-term
    – clestcruz
    Jul 26, 2018 at 9:38
  • You probably need to use php to achieve that. Preprocess the theme and use preg_replace or something.
    – Beebee
    Jul 26, 2018 at 9:53

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.