7

After reading the documentation and searching through code, I can't quite figure out what the entity_view function is for.

The documentation explains it executes the view callback if defined, with a fallback to the entity controller's view function. Since I use the EntityAPIController I want to use that view function, that adds a similar structure like below.

So I assumed entity_view is to replace a structure like this (part of a page callback).

// Prepare the entities
entity_prepare_view('entity_type', $entity_field_query_result);

// Apply theme to all entities
foreach($entity_field_query_result as $key => $item) {
  $entity->content = array();
  $entity->content += array(
    '#theme'     => 'my_entity_theme',
    '#element'   => $item,
    '#view_mode' => 'full',
    '#language'  => LANGUAGE_NONE,
  );
  $render[$key] = $entity->content;
}
return $render;

Thus I've replaced that with

$render = entity_view('entity_type', $entity_field_query_result);

return $render;

This however shows an empty page, where the previous function showed me a list of entities. I have custom themes applied to these entities as follows, could that be an issue?

function my_module_theme($existing, $type, $theme, $path) {
  return array(
    'my_entity_theme' => array(
      'variables' => array('element' => null),
      'template' => 'my_entity_theme',
      'path' => $path . '/template',
    ),
  );
}

So, is my assumption on entity_view correct, or am I missing steps to come to a renderable output?

4

A EntityFieldQuery doesn't return fully loaded entities as result. Try running the query result through entity_load('type', $result['type']). Then you can render the resulting entites through entity_view('type', $entities).

Here an example:

$query = new EntityFieldQuery();
$query->entityCondition('entity_type', 'node');
$query->entityCondition('bundle', 'article');
$result = $query->execute();
if (!empty($result)) {
  $entities = entity_load('node', array_keys($result['node']));
  return entity_view('node', $entities, 'full');
} else {
  return array('#markup' => t('No article nodes found.'));
}
  • Thanks for the reply, I've written my own query wrapper that in fact does return loaded entities. I read over that when writing the question. So I assume it has to be something else... – Neograph734 Feb 16 '14 at 18:21
  • How do the values of $entity_field_query_result look like? Are they renderable? You put them directly in the '#element' field of the render-array - therefore they have to be renderable themself. – gue Feb 16 '14 at 18:29
  • Stupid me - they will be sent to your theme function where you can handle them the way you want. – gue Feb 16 '14 at 18:31
  • So I guess entity_view is not working on your custom theme function. You might dig into that problem: how to use your own theme for certain entity types. – gue Feb 16 '14 at 18:32
  • After dpm'ing the render array I've found out that the entries have #theme value of entity, where it should be my_entity_theme. So the wrong theme is called. I'll see where that goes wrong. – Neograph734 Feb 16 '14 at 18:49

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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