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?