background image

Content tagged with: preprocess_page

Eric's picture

If your site uses the admin_menu & menu_breadcrumb modules, you might have noticed something wrong with your page titles on the /user page, similar to this:

# / # <img src="/sites/all/modules/admin_menu/images/icon_users.png" width="16" height="15" alt="Current anonymous / authenticated users" title="Current anonymous / authenticated users" />

The incorrect HTML is being output from the admin_menu in admin_menu.module on line 286, but it's caused from the menu_breadcrumb module.

As a quick solution, I added some code to my theme template.php file in a preprocess_page function to replace the titles.

<?php
function MYTHEME_preprocess_page(&$variables) {
  if (
stripos($variables['head_title'],'icon_users.png')) {
   
$variables['head_title'] = 'User account | ' . variable_get('site_name', '');
  }
  if (
stripos($variables['title'],'icon_users.png')) {
   
$variables['title'] = 'User account';
  }
}
?>

Eric's picture

Here's a quick code snippet that shows you how you can create custom breadcrumbs for a certain node type. This code would reside in your template.php theme file.

<?php
function MYTHEME_preprocess_page(&$variables) {
  if (
$variables['node']->type == 'MYNODETYPE') {
       
   
$links = array();

   
// creating a link to the home page
   
$links[] = l('Home', '<front>');

   
// here's how you could add a link to a taxonomy page
   
$vid = 2;
    foreach (
$variables['node']->taxonomy as $k => $v) {
      if (
$v->vid = $vid) {  
       
$links[] = l($v->name, 'taxonomy/term/' . $v->tid);
        break;
      }
    }
       
   
// yet another link
   
$links[] = l('Some Other Link', 'SOMEOTHERLINK');

   
// lastly, overwrite the contents of the breadcrumbs variable in the page scope
   
$variables['breadcrumb'] = theme('breadcrumb', $links);
  }
}
?>

NOTE: if you were adding this code to a module, you could use the drupal_set_breadcrumb() function to do the same functionality.

Eric's picture

When using views to handle the taxonomy term pages, you may not have the taxonomy term description available in your page variable scope (either page.tpl.php or page-taxonomy.tpl.php). If you'd like to display it at the top of the page, you can add a preprocess function in your theme to add the variable:

<?php
function MYTHEME_preprocess_page(&$variables) {
 
// check to see if this is a taxonomy term page
 
if (arg(0)=='taxonomy' && arg(1)=='term') {
   
// load the taxonomy term object
   
$term = taxonomy_get_term(arg(2));

   
// add the taxonomy term description to the variables
   
$variables['taxonomy_term_description'] = $term->description;
  }   
}
?>

Now in your page.tpl.php (or page-taxonomy.tpl.php) file, you can add the following line of PHP to output the description. NOTE: I enclosed it in a div with a class so I can add necessary CSS easily.

<?php
if ($taxonomy_term_description) print "<div class='taxonomy_term_description'>$taxonomy_term_description</div>";
?>

Eric's picture

Today I wanted to add a unique identifier to each menu item in the primary links so I could theme each one individually. In this example, I added a incrementing class to each menu item so I could add an image via css. Here's a snippet of my theme function (found in template.php):

<?php
function MYTHEME_preprocess_page(&$variables) {
 
$linkCount = 0;
  foreach (
$variables['primary_links'] as $k => $v) {
   
$linkCount++;
   
$class = trim($v['attributes']['class'] . " nav$linkCount");
   
$variables['primary_links'][$k]['attributes']['class'] .= $class;
  }
}
?>