1

Is it possible to check for a value in a supertable field during the for loop:

{% for item in craft.products.supertable.lightswitch(1).all() %}
  {# stuff #}
{% endfor %}

This returns

Impossible to invoke a method ("lightswitch") on a null variable.

So I guess the content of the supertable field is not available yet? Because if I write:

{% for item in craft.products.all() %}
  {% if item.supertable.lightswitch == 1 %}
    {# stuff #}
  {% endif %}
{% endfor %}

This works nicely. Is this not a part of the element criteria model?

1 Answer 1

1

The Super Table field value is a query, so the .all() needs to be called on the field itself (not on lightswitch). But looping over queries is in itself deprecated in Craft, so you should set it to a variable first, and then loop over that.

In any case, the .all() will return all rows of your Super Table field, each of which contains your lightswitch field (even if there is only one row).

If you know there is only one row, perhaps something like this would work, but I haven't tested it:

{% if craft.products.supertable.one().lightswitch == 1 %}

I would anyways recommend performing the query first, then use the result to loop or get your field.

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.