0

I am trying to write a module that displays several entities in one page, but I don't see how I should theme them.

I've found (an mostly followed) this: https://drupal.org/node/1238606.

But the point where I get stuck is the return value of the page callback. The link above suggests:

function my_entity_page_view($entity, $view_mode='full') {
  $entity_type = $entity->entityType();
  $entity_id = entity_id($entity_type, $entity);

  $entity->content = array();
  $entity->title = filter_xss($entity->title);

  field_attach_prepare_view($entity_type, array($entity_id => $entity), $view_mode);
  entity_prepare_view($entity_type, array($entity_id => $entity));
  $entity->content += field_attach_view($entity_type, $entity, $view_mode);

  $entity->content += array(
    '#theme'     => $entity_type,
    '#element'   => $entity,
    '#view_mode' => $view_mode,
    '#language'  => LANGUAGE_NONE,
  );
  return $entity->content;
}

I however try to load several entities at once and have no fields, so I tried this:

$entities = // Array of entities keyed by id;

entity_prepare_view('entity_type', $entities);

$return = array(
  '#theme'     => 'entity_type',
  '#element'   => $entities,
  '#view_mode' => 'full',
  '#language'  => LANGUAGE_NONE,
);

return $return;

Since entity_prepare_view() accepts an array of entities, I'd suppose there should be a way to theme several entities at once as well. Or am I wrong?

0

After a bit more fiddling, this worked for me. I've tried a similar approach earlier, but I guess there was something wrong. I've commented out the field functions, but these are the places in case you need them.

$entities = // Array of entities keyed by id;

// Prepare all entities at once. Attach fields if desired.
//field_attach_prepare_view('entity_type', $entities, 'full');
entity_prepare_view('entity_type', $entities);

// Expose all entities individual to hook_theme.
foreach($entities as $key => $entity) {
  $entity->content = array();
  //$entity->content += field_attach_view('entity_type', $entity, 'full');
  $entity->content += array(
    '#theme'     => 'entity_type',
    '#element'   => $entity,
    '#view_mode' => 'full',
    '#language'  => LANGUAGE_NONE,
  );
  $entities[$key] = $entity->content;
}

return $entities;

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.