I have an array of entities that i would like to render in a list.
$ids = \Drupal::entityQuery('chemical')
->condition('tid', $tid)
->execute();
$controller = \Drupal::entityManager()->getStorage('chemical');
$entities = $controller->loadMultiple($ids);
$chemicals = [];
foreach ($entities as $entity){
$chemicals[] = ['id' => $entity->id(), 'label' => $entity->label()];
}
$output[] = [
'#theme' => 'chemical_taxonomy',
'#chemicals' => $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' => $chemicals
with '#chemicals' => $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,
id
property of taxonomy terms istid
and thelabel
property isname
. And to access their values you will need to use either{{ entity.tid.value }}
or{( entity.id() }}
. I'm writing this from a phone so syntax errors are possible.