Entities and Twig
Helpful links
- Entity reference values in twig template | Drupal.org
- Drupal 8 Entity Cheat Sheet | Drupal informatie van Wizzlern, de Drupal trainers
- Get referenced node fields in twig - Drupal Answers
Tips
If you want to access fields and their values, start off with node, not content, which is a render array with the configured fields.
when trying to figure out what's in a content entity, use node.toArray()
, that gives you an array representation that's very close to how you can access it as an object. While the internal structure is quite different.
Then, you can access a field value with
block_content.field_name.property
. So in your case,block_content.field_align.value
. The property is value for most field types, for references, you can either use target_id for the ID or entity for the referenced entity object. Yes, you can directly access fields on that, but make sure to always check that a reference exists otherwise you can end up with fatal errors or exceptions. To access the label of a term reference for example, you can access it asblock_content.field_tags.entity.name.value
.If you don't specify the field delta, it defaults to the first. If you want to access a different delta, you can use entity.field_name.1.valueand so on. You can also loop over them.
This all maps directly to PHP, you can also do
$block_content->field_tags->entity->name->value
in preprocess and other places where you have theblock_content
.
Snippets
{{ node.field_blog_author.entity.title.value }}