How to correctly access entity elements in a twig template I have an array of entities that i would like to render in a list.

$ids = \Drupal::entityQuery('chemical')
  -&gt;condition('tid', $tid)
  -&gt;execute();
$controller = \Drupal::entityManager()-&gt;getStorage('chemical');
$entities = $controller-&gt;loadMultiple($ids);

$chemicals = [];
foreach ($entities as $entity){
  $chemicals[] = ['id' =&gt; $entity-&gt;id(), 'label' =&gt; $entity-&gt;label()];
}

$output[] = [
  '#theme' =&gt; 'chemical_taxonomy',
  '#chemicals' =&gt; $chemicals,
];
return $output;


The twig template then creates the unordered list of URLs using the array of {{ chemicals.label }} and {{ chemicals.id }}. However, I should be able to just pass $entities to the template instead of extracting the id and label and building a new array, shouldn't I?

If I replace '#chemicals' =&gt; $chemicals with '#chemicals' =&gt; $entities I receive an error:

Uncaught PHP Exception Exception: "Object of type Drupal\\Core\\Field\\FieldItemList cannot be printed." at /var/www/Drupal_Sites/console/core/lib/Drupal/Core/Template/TwigExtension.php line 443,
As the exception explains, you tried to print an object that is not renderable and cannot be converted to a string. In this case it was Drupal\Core\Field\FieldItemList which is the object returned by drupal entities when you ask for a field ({{ entity.id }} / {{ entity.label }}).

To get the actual value you need to call to methods of your entity (the actual value of the fields is protected from outside access).

The long way:

{{ entity.get('id').get(0).get('value').getValue() }}


The above is the actual path you need to take to get from the entity object to the value of the id field.

However drupal supplies shortcuts through what is known as magic properties.
The short way

{{ entity.id.value }}