In Drupal 7, I have a module that implements hook_node_load. It creates a variable that I'd like to make use of in my page.tpl.php file. Let's pretend it is the count of nodes that are of the same type as the currently loaded node. As of now, I'm just making the variable global, like so:

global $node_count;
$node_count = db_query("SELECT count FROM {node} WHERE type = :type", array(":type" => $node->type"))->fetchAll()[0]->count;

Then, within my page.tpl.php file, I have to do something like the following:

global $node_count;
echo "<p>$node_count</p>";

I know that there is a way to make my $node_count variable in scope, in the same way that $node or $user are. The tutorials I find on the subject seem to involve rewriting the entire theme function - I'm not trying to do anything that complicated. Is there a simple way to just make this variable in scope in my .tpl.php files? Something like a function?

2 Answers 2


In Drupal theming, template preprocess functions are the functions that prepare the variables that can be used in a certain template. Template preprocess functions are executed one after another and they pass around an array with all variables. You can add your own in your theme's template.php, in the form of function THEMENAME_preprocess_TEMPLATENAME(&$variables) { // Your code here. }.

If you're adding a value to a node object in a hook_node_load implementation, that probably looks something like this:

function mymodule_node_load($nodes, $types) {
  foreach ($nodes as $node) {
    $node->myvariable = 'somevalue';

Now you want to access $node->myvariable in your page.tpl.php. For this, you don't even need to write a preprocess function, because Drupal already adds the current node object to the variables by default. (This happens in template_prepocess_page.) This means that you can simply add this to your page.tpl.php:

<?php if ($node->myvariable): ?>
  <div class="myvariable">
    <?php print $node->myvariable; ?>
<?php endif; ?>
  • I may not be understanding this correctly, but I am not quite sure this is what I want. The thing is, I want to create the variable within a hook_node_load() function. This would live within my module, not my theme. Am I able to do this with themename_preprocess_page? If so, any chance you could give an example? Thanks.
    – rybosome
    Commented Nov 15, 2011 at 22:06
  • OK, I see, I'll update my answer. Commented Nov 16, 2011 at 8:09
  • Answer updated. Hope that helps! Commented Nov 16, 2011 at 8:58

Variables that are accessible from a template file can be set in its preprocess function, which can be implemented in a module too.

In your case, your module should implement code similar to the following one (replace "mymodule" with the short name of your module):

function mymodule_preprocess_page(&$variables) {
  if (!empty($variables['node'])) {
    $variables['node_count'] = db_query("SELECT count FROM {node} WHERE type = :type", array(":type" => $variables['node']->type"))->fetchField();

I verify $variables['node'] is not empty because the page template is also used for pages that are not associated with a node.
In page.tpl.php, you access the same variable set from the preprocess function using $node_count.

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.