0

I created a custom entity (Representative) using the Drupal Console generate:entity:content command.

The entity works correctly. I added fields, added some content, and I can query this entity.

My problem is that I can't render this entity in a template file, using the following code.

$build = [
  '#theme' => 'representatives',
  '#representative_entity' => $representative
];

I get the following error.

Exception: Object of type Drupal\kelk_representatives\Entity\RepresentativeEntity cannot be printed. in Drupal\Core\Template\TwigExtension->escapeFilter() (line 476 of core/lib/Drupal/Core/Template/TwigExtension.php).

Can anyone help?

1 Answer 1

3

You need to use the entity view builder, to correctly render an entity, like the following code.

$view_builder = \Drupal::entityTypeManager()
  ->getViewBuilder('representatives');
$entity_view = $view_builder->view($representative);

$build = [
  '#theme' => 'representatives',
  '#representative_entity' => $entity_view,
];
0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.