6

Are there any issues I should know about using a pre/process function and, simultaneously, a template file?

For example, I have some logic I would like to apply to a field and at the same time, I would also like to customize the HTML output for that field as well.

Can I create a pre/process function and create a template file specific for that field?

14

Before answering YES/NO, let me make the context clear:

The .tpl.php files live near the city gates and are ready to produce code for the rest of the world. Moving towards the core of the city (our Drupal site) there is a very strong file: template.php. This file supports .tpl.php by providing new variables, changing variables, getting the HTML ready for pieces of our site and more.

Inside template.php there are 4 categories of things one can do:

  • write preprocess functions, like MYTHEME_preprocess_page(&$variables) or MYTHEME_preprocess_node(&$variables). With the aid of preprocess functions we can add or alter variables inside tpl.php files. Our main weapon: kpr($variables) or dpm($variables) provided by the devel module.
  • write theme functions like MYTHEME_links($variables) or MYTHEME_breadcrumb($variables). Through theme functions we change the HTML output of some chunks of our site, like the breadcrumb. Main weapon: devel themer. (We check which theme function is responsible for the HTML output and override that one)
  • preprocess functions for theme functions. We manage to change a variable used in a theme function. For example, MYTHEME_preprocess_username(&$variables) prepares the variables for the theme_username(). We could put the code directly inside the corresponding theme functions, but then we would probably have a mess. (anyway, that's the idea behind the separation of the code here, and is not obligatory). Main weapon: kpr() or dpm()
  • implement hook functions. There are 4 hook functions available for template.php:

    hook_css_alter
    hook_js_alter
    hook_page_alter
    hook_form_alter
    

The first two change the css/js files loaded on a site (the one's we see as @import blah blah when we view the source code, and we can tell Drupal not to load a file (even if it a default module.css or anything else)

The third is used to change things around, regarding the whole page element of Drupal. So for example, we can move things around, from a node of the main content to the second block of the sidebar, etc

With the last one we can change variables inside any form in Drupal. Main methodology: call debug($form_id) to take the unique id of the form you want to play with then target this form by if ($form_id == blabla), then call dpm($form) or debug($form) to see what's inside the specific form and alter variables, etc.

And YES, of course you can use both a preprocess function AND a specific .tpl.php for example:

function MYTHEME_preprocess_node(&$variables){
if ($variables['type'] == 'article')
$variables['blabla'] = .. // creates a variable to be used in node.tpl.php
}  

then create node--article.tpl.php and print that $blabla there

  • I have wondered what those hooks are in drupal. I haven't quite wrapped my head around them yet, but you at least showed me how to use them in this context... thank you so much. – randomblink Jun 4 '13 at 20:37
  • 2
    yes, seeing the bigger picture is very important otherwise we get lost in the details. Glad to help. you said: I have some logic I would like to apply to a field; at same time, I would also like to customize the HTML output for that field as well. If you give specific example, then you can get more specific idea of how to implement these things – Vagelis Kostopoulos Jun 4 '13 at 20:48
  • the biggest use of both is to specify which template to use via the template_file or template_files variable. – Jimajamma Jun 4 '13 at 22:31
  • This is very important post – Fawwad Feb 24 '16 at 11:11
9

It is absolutely OK to have both pre/process functions in your template.php file and a template file for that content type (or even field) in addition to that.

Typically, the best practice in Drupal is the following:

  • Use your template files to specify the HTML and layout for your content types and fields. Most template files do indeed have PHP in them, but that should be light and primarily for some small if/else logic. There is a strong tendency to try to separate form from function.
  • If you find yourself doing complicated PHP logic in your template files, that's your cue to move things to your template.php file. Do heavy calculations in preprocessing functions here. Keep your template files clean and easy to read. The preprocessing functions are the ones that should do the heavy lifting in getting your variables all good and ready to go. Once they are all prepped, the preprocessing functions pass them on all ready to be displayed to the appropriate template file, which should then apply the necessary HTML.
  • And that's what I thought. Thank you so much for the clarification. – randomblink Jun 4 '13 at 20:36
  • although in Drupal 8 things will be in .tpl.php and the concept of preprocess functions will go away :) – Vagelis Kostopoulos Jun 5 '13 at 13:52

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.