In my Drupal 7 site I want to display how many times user's content was flagged. As option I want to set a global variable counts and display it in different parts of *.tpl.php files. The value can be calculated with flag_get_counts($entity_type, $entity_id) for each user's node_id. Function that calculates counts should be called in template_page_preprocess() to update value each time page is reloaded.

What is more efficient approach to implement this?

1 Answer 1


The right way is using template_preprocess_node().

Place the following code in template.php of your front end theme:

function YOUR_THEME_preprocess_node(&$vars) {
  // I suppose you don't want to do this for every content type on the site.
  switch ($vars['node']->type) {
      $vars['count'] = flag_get_counts('node', $vars['node']->nid);

This will make the $count variable available in .tpl.php files that are used for displaying nodes of your content type.

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.