1

I am writing a query inside the following function of template.php file

        function theme_name_process_views_view_field(&$vars) { 
 if ($vars['view']->name == 'view_name') { 
// query is written here
      foreach( $vars['view']->result as $key => $value ) {
          $nid = $value->nid;
          $query = db_select('abc_in', 'abc')
            ->fields('abc', array('study_abc'))
            ->condition('abc_nid', $nid , '=');
          $result =   $query->execute()->fetchField();
        $vars['view']->result[$key]->field_field_abc_in[0]['rendered']['#markup'] = "<div class='abc_code'>" .  $result . "</div>" ;

    } }

Is it ok to write a db_query inside template.php file ? I an taking a data from custom table and inserting value into field of a view ?

  • 1
    It is not considered good practice; you should separate your display from your logic, so a query should be made in a module and then the results passed to the theme layer. Your question will be better answered if you describe exactly what you're trying to achieve :) – njp Sep 25 '13 at 10:08
  • For sure way better than writing it in .tpl.php files. See this answer as it pretty much applies here too. – Mołot Sep 25 '13 at 10:09
  • pls check the edited question – harshal Sep 25 '13 at 10:13
3

You can definitely do it, however you really shouldn't.

The theme layer is for presenting the data. The actual functionality and the data itself should come from modules.

If you mention what you are trying to do I can give some more information on where it should go instead

Edit

Based on your code example, if you are dealing with a custom data table the best thing would be to use hook_views_data() to let views know about your database table and fields. Then you would just use your new fields via the UI.

If you didn't want to go to that much trouble though you could use basically the same solution as you are using now in a custom module in one of the views hooks, like hook_views_post_execute(), where you can loop through the results in a similar fashion as you are doing now.

You generally wouldn't be dealing with markup here though (like your abc_code div). You're really only changing data here and then changing markup in the theme. Although your field should already have classes that let you specifically style it so adding that class probably shouldn't be necessary.

  • What if presentation settings are kept in database? It's a gray area. – Mołot Sep 25 '13 at 10:12
  • Generally you should have those settings passed into the theme function from a module, so the settings you need are already available in the $variables and you don't have to query for them. For example in views your settings should probably be in the view object. There are of course exceptions to rules and you are under no obligation to follow any rules, but it's a rule of thumb to make any other developers lives easier when they work on that site. – rooby Sep 25 '13 at 10:15
  • Most contrib themes with advanced settings does not force to use companion module ;) and sometimes it might be inconvenient to put a lot of stuff into variables table as it is read at every request, even if particular setting is not needed. – Mołot Sep 25 '13 at 10:18
  • Oh yeah, I was thinking module settings, not theme settings. Generally theme_get_setting() should do in that case, unless you have some pretty crazy settings, in which case you are probably an exception to the rule. You still shouldn't have to be writing sql in your preprocess functions I feel. – rooby Sep 25 '13 at 10:33
  • A bit of if logic to check settings is much different to pulling new data from the database. – rooby Sep 25 '13 at 10:35
1

It is not a recommended practice to add db queries to your theme you should always separate logic behaviour from your presentation layer

Plus also you should always follow drupal standard for doing things

0

You shouldn´t write queries in "theme functions" or ".tpl files".

If you need the data to render the element you could considering to write the query in the preprocess function making the data available in the "theme function" or ".tpl file" in this way

  • Can I write query in hook_views_pre_render() ? – harshal Sep 25 '13 at 10:19
  • or should create a mention in the hook_theme() ? – harshal Sep 25 '13 at 10:22
  • Why do you need the query? Can´t you get the same aproach with a relatioship? If you need specific business logic you could considering to write your custom "views field handler" – Enxebre Sep 25 '13 at 10:33

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.