0

I am calling the following function inside a template for a custom content type, but my senior developer suggested that calling a function inside a template file is not Drupal standard.

function some_function($AA_tid = 0) {
  $dis_pt = db_select('some_tble', 'v')
    ->fields('v', array('title'))
    ->condition('some_fld', $AA_tid)
    ->distinct();
  $dis_pt->join('another_tble', 'ss', 'v.title = ss.type');
  $dis_pt->fields('ss', array('name'));
  $result = $dis_pt->execute()->fetchAll();
  return $result;
}

Should I call the function which will query the database from a content type template? Generally speaking, is it better to avoid such code in a template file?

Edit: function call is present inside the template & its definition is available in some module file.

0

I believe that depends on what you're doing with your query. According to this Drupal.org community doc you should "refrain from having complex logic in" .tpl files and "for all the conditional logic and data processing of the output, there is the template.php file". IOW template files are for theming/output manipulation while other functions or logic should go into your custom module. So if your query is part of a theming process, yes it should be in your template file. Otherwise in your custom module.

AFAIK other developers will not be looking for a non-theming function in a template, which is why I think the senior dev suggested that.

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.