0

Is it ok to query the database in a template.php file of a sub-theme. If not where do I put it? Below I have function that is querying the database and it works fine but I want to make sure I doing it the right way =).

function inTheTemplateFile() {
   $query = db_query("SELECT tid FROM taxonomy_menu");
   return  $query->fetchAll();
}
4
  • "NO! Absolutely not!" Is what passes through my head when I read the question. I believe webchich has informally suggested that she'd have a stern talk with anyone who did so as well.
    – Letharion
    Commented Nov 10, 2014 at 13:54
  • You did not answer the question. This is a rant
    – purencool
    Commented Nov 10, 2014 at 19:33
  • Granted. Hence it's just a comment. :) I get the idea though, that since you ask the question to begin with, you are well aware that it's not a good idea. :)
    – Letharion
    Commented Nov 10, 2014 at 19:38
  • Yes I did. I am working on the getting implementation right now and sometimes Drupal can be so confusing =)
    – purencool
    Commented Nov 10, 2014 at 20:07

2 Answers 2

1

The template should idealy not be used to do database queries or other code that belongs in the Model or Controller of the MVC pattern. The problem is that it is impossible to have a perfect implementation of the MVC pattern in Drupal.

What I mostly use as a rule of thumb is this:

  • Is this functionality that belongs to a module? Then put it in a module.
  • Would I have to copy this to a new theme if I made a second one? Then put it in a module (or parent theme if it should be in a subset of themes).
  • Does this function get/compute data or results which should be provided by a module? (like fetching user-data or a list of items) Then put it in a module.

In general the theme should only contain HTML, JS, CSS and a minimum of PHP used for simple loops, printing and simple conditional statements.

So the answer to your question would be; Probably not, it should probably be in a preprocess function or a getter function (like book_get_books()) in the module.

Edit: As Clive points out in the comments, Drupal does not strive to be fully MVC. The MVC pattern is only used to make it clear what belongs in the template, the controller and view don't really exist in drupal. Even the view is not as clear cut as it is in something like Symfony or Zend framework.

3
  • 1
    i think this is a bit confusing - Drupal never intended to expose an MVC architecture so trying to talk in terms of that pattern doesn't really make sense. There's also the subtle implication that MVC should somehow be the preferred 'goal' and Drupal's failure to conform makes this question harder to answer (because one "should" only be striving for MVC). I think that's way off, though I agree with everything else you've said
    – Clive
    Commented Nov 10, 2014 at 14:03
  • Ok create a module thanks for your help. I thought that might be the case but I wanted to make sure. Thanks
    – purencool
    Commented Nov 10, 2014 at 19:32
  • I found this it helped me after I got some direction from the above answer stackoverflow.com/questions/7951457/…
    – purencool
    Commented Nov 10, 2014 at 19:37
-1

You should never use query database on your templates.

Instead, data should come from views, and after, from a controller o event that render this view

2
  • drupal is not MVC, it does not have controllers or views in that sense
    – Clive
    Commented Nov 10, 2014 at 14:41
  • I know Drupal is not MVC pattern, but I am almost sure that template is not the best place to place functions that calls to the database.
    – daniroyo
    Commented Nov 11, 2014 at 10:48

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.