I created a custom entity called foo. This entity has a unique name
field as well as the standard id
key. I would like to build a page where the user can see the entity rendered similarly to how it would be on the admin page, but instead of referencing it by the id
, I would like to use the name
.
I have the following in my controller:
public function content($name) {
$output = [];
//Add a couple things to the $output array
$output['first thing'] = [/*stuff*/];
$sql = "SELECT id FROM foo WHERE name=:name";
$sql_params = [':name' => $name];
$results = db_query($sql, $sql_params)->fetchAll(PDO::FETCH_ASSOC);
$render_array = [];
if (count($results) > 0) {
$id = $results[0]['id'];
$entity_type = 'foo';
$view_builder = \Drupal::entityTypeManager()->getViewBuilder($entity_type);
$controller = \Drupal::entityManager()->getStorage($entity_type);
$entity = $controller->load($id);
$build = $view_builder->view($entity);
$render_array = render($build);
$output['entity'] = $render_array;
}
return $output;
}
However, I receive an error: User error: "entity" is an invalid render array key in Drupal\Core\Render\Element::children() (line 97 of core\lib\Drupal\Core\Render\Element.php).
What do I need to do differently to make my entity render to the page?