1

I have a content type (Movie) with a taxonomy reference (Movie rating) for which can get multiple values. I would like to use the template node--movie.html.twig and get the first value inserted in the taxonomy term.

I tried using the following code, but it doesn't give any value.

{{ content.field_rating['#items'].getValue()|first.value }}

What am I doing wrong? In which way do I reference the taxonomy term value?

  • 1
    It sounds like you are doing it wrong. While you can get an answer it might not be the best solution. Normally you would want to use a field formatter or field styling to handle the output. If you need the term for logic, it might be more clean to do it in a preprocess, rather than doing it in the twig template. – googletorp Feb 5 '16 at 14:59
  • I agree with that, but you also can do adjustments in twig, but then wait until the field reaches the field twig, and not do it too early in the node twig. – 4k4 Feb 5 '16 at 17:27
7

You get the first item out of ['#items] with a "0":

{{ content.field_rating['#items'].0 }}

To get a field from the taxonomy term, I would use the node object, because you don't know how the render array in content will change if you configure the view mode differently:

{{ node.field_rating.0.entity.term_field.value }}
| improve this answer | |
  • Yes, definitely use node... However, I am a bit confused as to what exacty should be access from the term. Your example access a field on the term, maybe he just wants to term label, which would be entity.label. – Berdir Feb 5 '16 at 16:02
  • I was confused too, so I simply put term_field as placeholder for any field of the taxonomy term, or label, that you can use without value, because it is a shortcut for getlabel() – 4k4 Feb 5 '16 at 17:10
-1

I got the answer of my problem.

Actually first i wanted to show only label or title of term just in my node template. To get the title of first term is like below -

{{ content.field_rating[0]['#title'] }}

OR

{{ node.field_rating.0.entity.label }}

But I got to know that I have to show entire term (comma separated ) with link of term as well. The code below works -

{{ content.field_rating[0] }}
{% for key, value in content.field_rating %}
       {% if key > 0 %}
           {{ ' , ' }}
           {{ content.field_rating[key] }}
        {% endif %}
{% endfor %}
| improve this answer | |
  • You shouldn't do it like that. Instead you should make a template for the field_rating field where you can add the "," and customize other parts of the markup. – googletorp Feb 8 '16 at 9:55
  • Or use the ui to configure this comma separated list, and some minimal changes in the field twig, if needed. – 4k4 Feb 8 '16 at 14:29
  • Yes i can create the file template and process the thing over that template. But could you please tell me what is the wrong with this approach? Because i thought i am doing some markup changes in my custom node template. – ashish kushwah Feb 8 '16 at 15:02
  • The loop that you are coding is already in the templates. And your code depends on data structures that may change, either through configuration or changes by other code like contrib modules. – 4k4 Feb 8 '16 at 17:00

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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