1

In a Matrix field i want to check if block.logolink (simple text) OR block.logolinkDropdown (dropdown with empty field) are empty or not:

{% if block.logolink or block.logolinkDropdown %}

If i check them with {{ block.fieldHandle }} they're empty. But still it passes the condition.

Where's my mistake in this statement?

{% for block in entry.logomatrix %}
{% switch block.type %}

{% case "logo" %}
{% if block.logobild %}
    {% for logo in block.logobild %}
    {% if block.logolink or block.logolinkDropdown %}
        {% if block.internerLink %}
            <a href={{ block.logolink }}{{ block.logolinkDropdown }}>
        {% else %}
            <a href={{ block.logolink }}{{ block.logolinkDropdown }} TARGET="_blank">
        {% endif %}
    {% else %}
        <a href="{{ logo.getUrl('logogross')}}" data-lightbox="image" data-title="{{ block.logobeschreibung }}"></a>
    {% endif %}

    {% endfor %}
{% endif %}

{% case "textbutton" %}     
...

{% endswitch %} 
{% endfor %}
4
  • 1
    Did you try Twig's length filter?
    – Johannes
    Dec 30, 2016 at 12:04
  • Yes, i tried the length filter first. It ouputs a 0 for the "block.logolink" and a 1 for the "block.logolinkDropdown", altough a empty field is chosen in the dropdown.
    – TomS
    Dec 30, 2016 at 12:37
  • And how about block.logolinkDropdown.value|length?
    – Johannes
    Dec 30, 2016 at 12:42
  • That's the solution! It explains also some other questions i had. Thank you very much rostockahoi.
    – TomS
    Dec 30, 2016 at 14:29

1 Answer 1

4

You can get a dropdown field's selected value by using entry.fieldhandle.value. So if entry.fieldhandle.value|length should do the trick. I think you can even omit the length filter in this case, as if you do it with the plain text field. Both an empty string and the length of an empty string resolve to false.

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.