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
Comment #2
diamondseaComment #3
socialnicheguru commentedTried 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).
Comment #4
joel_osc commentedAlso 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.
Comment #5
brightboldThanks 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.
Comment #6
thomasmurphy commentedI got this to work, but with different syntax:
Comment #7
darrell_ulm commentedThe solution in #6 worked for me.
Comment #8
megclaypool commented#6 worked for me too... Thanks @thomasmurphy 😀
Comment #9
goldin commented#6 +1
Comment #10
shelaneI've added the info to the project page.