Skip to main content
  • Agency for web development
  • Content Management with Primer
  • Open Source Leadership with Drupal
    Language
  • Deutsch
  • English
  • Contact
Site logo
Agentur für Webentwicklung
  • References
  • Services
  • News
  • About us
  • Agency for web development
  • Content Management with Primer
  • Open Source Leadership with Drupal
close
  1. Home
  2. Techblog Archive

Twig and Entity Field API or how to take control of node.html.twig

20. February 2017
Sascha Grossenbacher Porträt
Sascha Grossenbacher

The node template, like any other entity template has two primary variables available.

The first, content contains the render arrays prepared by the field formatters according to the entity view display configuration, visible on the "Manage display" page for the given view mode and node type.

The second, node is the node object, which contains the fields and all their raw values and referenced entities. It is useful to check if a field has values and for conditions based on boolean, list, number and similar fields.

While it is possible to access the raw values through the content variable as well, it is a lot easier to write and read when doing that through node.

Displaying formatted field output

Print a specified field, including the default or overriden field template. {{ content.field_name }}

Print ony the first field item, without the wrapping field template, useful when the standard div wrappers should not be added. {{ content.field_name.0 }}

Print all field items, without the wrapping template. useful for e.g. listing them as a comma separated list, without needing a separate field template. {% for key, item in content.field_name if key|first != '#' %} {{ item }} {% endfor %}

Check field values and display output conditionally

Field values in the node object are always accessed like this: node.name_of_the_field.name_of_the_property.

Different field types and their properties.

Each field type can have different so called properties. Most simple field types (text, numbers, checkbox, lists, and so on) have a value property, that stores the raw, unformatted value. Reference fields that point to some other content (Including file and image fields as well as references to terms, users, ...) have a target_id which is the identifier of the reference as well as the entity property, which is the referenced entity. Check out the Entity Field API cheat sheet for more information.

Only display a field if a checkbox is checked, optionally with custom wrapping HTML

{% if node.field_checkbox.value %}

{{ content.field_name }}

{% end %}

Display an image field if it has a value, otherwise display another. It is recommended to use the entity for such checks, for rare cases where the reference has been deleted.

{% if node.field_checkbox.entity %}

{{ content.field_image }}

{% else %}

{{ content.field_fallback }}

{% end %}

Check a field on a referenced entity, for example you might a highlight checkbox on a term reference that you want to display differently.

{% if node.field_category.entity.field_highlight.value %}

{{ content.field_category.0 }}

{% else %}

{{ content.field_category.0 }}

{% end %}

Hol Dir den Newsletter

Jetzt für unseren Newsletter anmelden und monatlich wichtige Insights aus der Branche und MD Systems erhalten. 

Zur Anmeldung

About MD Systems

MD Systems, headquartered in Zurich, is a unique team of international open source initiative leaders for the Drupal content management system.

With our experts for software architecture and design and our industry solutions, you digitize your organization successfully and efficiently.

MD Systems GmbH

Hermetschloostrasse 77, CH-8048 Zürich

Schweiz

+41 44 500 45 95

[email protected]

  • Contact
  • Impressum
  • Data protection
To top

© Copyright 2023 - 2024 MD Systems GmbH. Alle Rechte vorbehalten. Erstellt mit PRIMER - powered by Drupal.