0

I have a view that is outputting fields from a paragraph. I want to be able to output different markup. There are two available values in the dropdown

1|Wrapping Link
2|Internal Link

and they should determine if the markup is wrapped by a link or if the same link is a contained within the markup as a stand-alone link.

In my custom version of views-view-fields.html.twig I want to have some logic in place that will check the value of the dropdown and display the correct markup. I have tried the following but this is invalid.

{%- if {{ fields.field_banner_type.content }} = "1" -%}
    // wrapping link
{%- else -%}
    // no wrapping link
{%- endif %}
6
  • I think no need fo {{ in if statement did you try with {% if fields.field_banner_type.content == 1 %} // wrapping link {% else %} // no wrapping link {% endif %}
    – berramou
    Commented Nov 24, 2018 at 11:07
  • You've solved my syntax errors but it outputs // wrapping lnk irrespective of the dropdown value selected. The field is outputting the value as opposed to the key so I'm guessing I need to get access to this
    – LMG
    Commented Nov 24, 2018 at 11:47
  • if the output is // wrapping lnk thats because the if condition verified, and the comment in twig should be like {#wrapping lnk #} not // wrapping lnk
    – berramou
    Commented Nov 24, 2018 at 15:36
  • Ah, so the solution was the following {% if fields.field_banner_type.content == 'Wrapping Link' %}. Even if I set the formatter of the view for this filed to key I still had to set it as a string in the twig file - e.g. {% if fields.field_banner_type.content == '1' %}. All good now. Thanks, @berramou
    – LMG
    Commented Nov 24, 2018 at 19:05
  • You are welcome, i'm glad that help.
    – berramou
    Commented Nov 25, 2018 at 9:42

1 Answer 1

0

No need to {{ fields.field_banner_type.content }} inside if statement use fields.field_banner_type.content like the following:

{% if fields.field_banner_type.content == "1" %}
  {# wrapping link #}
{% else %}
  {# no wrapping link #}
{% endif %}

For more info See Twig if Documentation

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.