Skip to main content
  • Guides & Documentation

Knowledgebase

Featured articles, how-to guides and quick tips.

Get Boolean/List/Integer/Field Value in Block Templates - Twig - Drupal 8

Sometimes you might need to access the value of a specific field, such as a boolean or a computed value (such as an integer) of a field in your custom block template in Drupal 8.

Of course, since you're theming in Drupal 8, you'll be required to use Twig markup.

But accessing the value of a field, or rather—its key—requires a specific syntax.

Here's a good one to use, which works providing you set the field display settings to 'Key' rather than 'Default' in the content type 'Manage Display' area in the Drupal administration interface:

{% if '1' in content.field_custom_integer[0] %}
{% endif %}

Just replace the numerical value (which is 1, used as an example) in the above syntax, and switch out the field name for your own, and you should be good to go!