0

I create a "select" field (field_points) in comment section that users rating my content and in "manage display" I set "key" for my field format.

5|Excellent
4|Good
3|No Idea
2|Bad
1|Very Bad

Now I use this code in comment.html.twig:

{% if content.field_points == 5 %}
   *****
{% elseif content.field_points == 4 %}
    ****
{% elseif content.field_points == 3 %}
    ***
{% elseif content.field_points == 2 %}
    **
{% elseif content.field_points == 1 %}
    *
{% endif %}

When I use this code nothing is displayed But when I use {{ content.field_points }} , print for example 5 ( 5 is field key ).

How can I fix this problem in drupal 8?

2

Use the comment entity to get the raw field value instead:

{% if comment.field_points.value == 5 %}
   *****
{% elseif comment.field_points.value == 4 %}
   ****

...

{% endif %}

This doesn't work with {{ content }}, because it contains a build array to render the fields.

2
  • Thanks but when I use {% if comment.field_points.value == 5 %} just show last comment rating and others not print. And when I use {% if comment.field_points == 5 %} all comment just show * in rating section
    – RobinSon
    Sep 1 '17 at 10:17
  • 2
    This is only an example how to use the field value, I didn't code the complete logic.
    – 4k4
    Sep 1 '17 at 11:21

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.