1

I'm working on a twig template for use with a commerce product. Each product has a term applied to it like "Green Ball", "Yellow Ball", "Red Block", and "Purple Block".

I'm trying to take the variable that holds the term, check if it has a string in it, and then use that as the basis for a label. For example if its a Green Ball it would say "Order Ball" and if it was "Red Block" it would say "Order Block".

I'm marking this up based on examples but it always returns false.

{% if 'Ball' in product.field_myproduct_type|field_value %} Order Ball {% endif %}
{% if 'Block' in product.field_myproduct_type|field_value %} Order Block {% endif %}

I've both tried outputting the variable on its own to confirm the variable is correct and also tried replacing the variable from the conditional with a string to make sure the conditional is the proper syntax and both work as expected. I just can't seem to check in the variable.

I'm doing this from within a twig template that is a variant on commerce-product.twig

3
  • Maybe try {% if 'ball' in product.field_myproduct_type|field_value|raw|lower %} Order Ball {% endif %}
    – 100pic
    Jun 26, 2019 at 5:46
  • It didn't work unfortunately. Jun 26, 2019 at 18:11
  • 1
    Maybe try {{ dump(product.field_myproduct_type|field_value) }} to make sure you are getting a regular string as the output.
    – 100pic
    Jun 26, 2019 at 23:17

1 Answer 1

1

Via kint and a bunch of trial and error I identified the right way to do this:

{% if 'Ball' in product.field_myproduct_type.0['#plain_text'] %} Order Ball {% endif %}
{% if 'Block' in product.field_myproduct_type.0['#plain_text'] %} Order Block {% endif %}

Thank you 100pic for your assistance.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.