0

I know, that calling SQL queries in template files is bad practise. But is it bad practise also for theme functions?

For example when my form is rendering with my custom theme function:

<?php
$form['#theme'] = '_my_custom_theme_form_method';
?>

My custom method *_my_custom_theme_form_method* is placed in .module file. Is it the same bad practise as it is for template files?

Is it any serious performance issue, calling SQL queries form theme method?

thanks Tomas

closed as too broad by Mołot, Bala, AjitS, Beebee, Anil Sagar Mar 1 '14 at 3:42

Please edit the question to limit it to a specific problem with enough detail to identify an adequate answer. Avoid asking multiple distinct questions at once. See the How to Ask page for help clarifying this question. If this question can be reworded to fit the rules in the help center, please edit the question.

  • If you really need to call SQL at a theming level, there is possibility that something is fundamentally wrong with your design, and while call itself will rather not harm you much, your design may have problems that you will regret later. Everything you should usually need at theming stage is data passed to your theme function and module / theme configuration variables. Also, caching assumes that result depends only on input and these variables, so when it's not the case, you may cause some interesting cache glitches. – Mołot Feb 26 '14 at 11:00
  • Also, keep in mind that "You should only ask practical, answerable questions based on actual problems that you face." - for now precise answer is not quite possible, it all depends on too many aspects of your project. – Mołot Feb 26 '14 at 11:01
1

Any theming function only format variables passed inside them to specified format. If you want to get some additional variables from database you should use hook_preprocess_HOOK https://api.drupal.org/api/drupal/modules!system!theme.api.php/function/hook_preprocess_HOOK/7.

So, in your case you should implement:

  • hook_theme() in module to register new theme-function;
  • hook_form_alter() in module to modify form theme (as showed in your example);
  • template_preprocess_HOOK() in module, to get aditional info from database;
  • [theme]_hook() in template.php of theme to format variables as you wish.

There is no any performance issues, it is "must do" practice. If you have version control system, and you need to change some output, it's no required to change module files.

  • 1
    "There is no any performance issues," - I disagree, it may conflict with caching so it may cause performance hit or data inconsistency sometimes. – Mołot Feb 26 '14 at 10:51
  • 1
    I mean, that in most cases nothing wrong except structural failure of the project will not happen. If a developer selects from a database 1000 lines one by one, there is a problem of his qualifications. – Oleg Sherbakov Feb 26 '14 at 10:56
  • Thank you for comments. I use to have SQL queries in form theme functions, where data are changed so often that it is not advantage to cache them (data is changing more often then a page is viewing) – tomas.teicher Feb 26 '14 at 12:08
  • @tomas.teicher and that's exactly anti-pattern you should avoid. Do it the normal way and simply disable caching for pages that shows your data. – Mołot Feb 28 '14 at 8:48

Not the answer you're looking for? Browse other questions tagged or ask your own question.