In many cases in D8 this module may be obsolete, because using Twig for Views rewriting we can now use conditional logic. It would be helpful to note this on the module page in order to help reduce module bloat on systems that may not need the extra UI functionality that this offers.

In the field's rewrite section, we can use Twig conditionals to do the same thing. For example to display literal values:

{% if {{ field_type == "old" }} %}
OLD
{% else %}
NEW
{% endif %}

or to display view fields (fields must have already been loaded and be available under the Replacement Patterns section):

{% if {{ field_show_other_flag }} %}
{{ field_other_thing }}
{% else %}
{{ field_main_thing }}
{% endif %}

Comments

diamondsea created an issue. See original summary.

diamondsea’s picture

Status: Active » Needs review
socialnicheguru’s picture

Tried this in a global custom text field and got an error:

Twig\Error\SyntaxError: A hash key must be a quoted string, a number, a name, or an expression enclosed in parentheses (unexpected token "punctuation" of value "{" in "{# inline_template_start #}{% if {{ field_date_recur }} %}#015#012{{ field_date_recur }}#015#012{% else %}#015#012{{ field_event_date }} #015#012{% endif %}" at line 1. in Twig\ExpressionParser->parseHashExpression() (line 323 of drupal/vendor/twig/twig/src/ExpressionParser.php).

joel_osc’s picture

Also be aware that the all the field values get filtered using Xss::filterAdmin meaning that you may have valid markup that gets wiped out when rendering via a rewrite in Twig. Refer to: #3049467: Token rendering inconsistent with field rendering.

brightbold’s picture

Thanks for this explanation of my problem, @joel_osc. Like the OP in the issue you linked, I have a field that contains code for an iframe (jn my case, a Google 360° image). When I render it with Views, it renders perfectly. But when I attempt to use Views Conditional to determine whether there is any content in the field (so that I can replace it with a static image if not), the field always evaluates as empty. I hadn't thought the XSS filter could be responsible for this since the field was rendering through Views, but I hadn't realized that tokens are processed differently, which explains why it's not working with Views Conditional.

thomasmurphy’s picture

I got this to work, but with different syntax:

{% if field_type == "old" %}
OLD
{% else %}
NEW
{% endif %}
darrell_ulm’s picture

The solution in #6 worked for me.

megclaypool’s picture

#6 worked for me too... Thanks @thomasmurphy 😀

goldin’s picture

#6 +1

shelane’s picture

Status: Needs review » Fixed

I've added the info to the project page.

Status: Fixed » Closed (fixed)

Automatically closed - issue fixed for 2 weeks with no activity.