I am quite new to Drupal so this question may be very simple or basic. I created a module and I've extracted list of a custom content type for each user by using EntityFieldQuery and then I used entity_load to make the results ready. the question is how should I use the $archive_items in my template ?!?

$archive_items = entity_load('node', $archive_items_nids);

2 Answers 2


Your function will give an array of entity objects, indexed by their entity_id's. So:

foreach ($archive_items as $entity_id => $archive_entity) {
  /* Process each entity */

will allow you to iterate through the entities and to do what you need to each. Beyond that, we will have to know what result you are trying to achieve.

  • I want to generate an HTML output from what I have in $archive_items; So it doesn't mater if I generate my html code in my module and return it as a an output? I thought it's not a good idea to do so.
    – nooshinha
    Apr 3, 2013 at 5:25

Assuming that you want to render your collection of nodes, you would use node_view_multiple(). It's worth noting there's a node specific version of entity_load(), node_load_multiple().

With that in mind I imagine you're looking for code like the following:

$efq = new EntityFieldQuery();
$efq->entityCondition('entity_type', 'node');
// etc...

$results = $efq->execute();
$nodes = node_load_multiple(array_keys($results['node']));
$views = node_view_multiple($nodes);

$output = drupal_render($views);

$output will contain a list of teasers representing the nodes found in the EFQ.

It's also worth noting this kind of logic should not happen in the template file, but rather in a preprocess function. This will prevent potential problems with caching later on, and is just good programming practice all-round.

  • I didn't know how to define a template for menu. I did try hook_theme but it didn't work for menus. I didn't try preprocess functions because I didn't know how to. but I will search for it. tanx a lot, your answer is very helpfull.
    – nooshinha
    Apr 3, 2013 at 5:23

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.