Skip to content

Instantly share code, notes, and snippets.

@thamas
Last active December 11, 2018 14:30
Show Gist options
  • Save thamas/8b63601e03ad50c31b877ae5a65f1b77 to your computer and use it in GitHub Desktop.
Save thamas/8b63601e03ad50c31b877ae5a65f1b77 to your computer and use it in GitHub Desktop.
Drupal 8 Twig: hide a (paragraph) field based on the value of a sibling field
{#
This snippet is from a component based (Emulsify / Pattern Lab) custom Drupal theme.
The "span_…" variables are the properties of the BEM Twig function.
See: https://www.fourkitchens.com/blog/development/bem-twig-function/
The goal is to set the default display correctly (when the page loads).
After that the field visibility is manipulated by JS based on user interaction.
#}
{% set amount = element['#object'].field_amount.value %}
{% if amount <= 1 %}
{% set span_extra_value = ['hidden'] %}
{% endif %}
{% include "@atoms/_field-span.twig" with {
span_blockname: 'ingredient',
span_base_class: 'unit',
span_modifiers: ['plural'],
span_extra: span_extra_value,
} %}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment