I would like to move portions of code from template files to custom module. But i fail do find descriptive guide for such task. Idea is to migrate complex db queries and EntitiyFieldQuery out of the tpl.php files. For example:

$query = new EntityFieldQuery();
$entities = $query->entityCondition('entity_type', 'node')
  ->propertyCondition('type', 'predmet_clanak')
  ->fieldCondition('field_taxonomy_choose', 'value', 1 , '=')
  ->fieldCondition('field_vrsta', 'tid', 241);

$result = $query->execute();    
$nodes = entity_load('node', array_keys($result['node']));
foreach ($nodes as $node) {
  print ('<div class="putujuci-wrapper"><div class="putujuci">');    
  print drupal_render(node_view($node, 'teaser'));
  $nodelink = ltrim(url('node/' . $node->nid), "/");
  print ('</div>');     

  $imageLink = '<img src="' . $base_url . '/sites/all/themes/profil_edu/css/images/vise-button-static.png" width="55" height="28" border="0" class="img-swap">';
  print l($imageLink, $nodelink, array('query' => array('iframe' => 'true', 'width' => '800', 'height' => '700', 'format' => 'popup'), 'attributes' => array('class' => t('colorbox-load gumb')), 'html' => TRUE));
  print ('</div>');     

This code fetches collection of nodes (when conditions are met) in custom tpl.php file. I have been advised not to create complex queries in tpl files, but do not know yet best practices to do so.

I guess, module will firstly need to know context where i want this to occur? And after i do this in the module, do i need to pass this to the template.php first?

  • 1
    Is this like a block? a page? or something else? How would you best describe where this content maps to something in drupal?
    – mikeytown2
    Commented Aug 28, 2012 at 21:16
  • It is itended to show in various tpl.php pages. I have few content types, and i display node collections depending on context (im detecting that context by examining taxonomy field that was set for that node). More detailed, those are sliders with Node collections in it.
    – NenadP
    Commented Aug 28, 2012 at 21:44

1 Answer 1


I think you should go through the module developer guide.

You can use hook_preprocess_HOOK() in your template.php. So if your code was placed in the node template, the hook will be themename_preprocess_node($vars) and then you can check for a particular node type and create a variable like $vars['custom_variable'] = 'the query stuff'; and this variable will be available in your template.

  • Thanks for this, i already looked into that, but i need advice in my case, i already know how to create module (files etc) but i am puzzled how to make logic chain for the code.
    – NenadP
    Commented Aug 28, 2012 at 19:29
  • 1
    hook_preprocess_HOOK() will allow you to inject variables in your templates. Or replace existing value with your own. You also have hook_process_HOOK(). See documentation of the theme() function for all the available hook and their order. Commented Aug 28, 2012 at 20:10
  • Thanks for further suggestions! I hope with little documentation examination i will be able to migrate my php voodoo where it belongs :D
    – NenadP
    Commented Aug 28, 2012 at 21:40

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.