background image

Content tagged with: preprocess_page

Eric's picture

If someone clicks on a taxonomy term they land on a page showing all the content tagged with that term. The title on those pages is simply the taxonomy term. I thought it would be more usable if I changed the verbiage of the title to let the user know what they are viewing. This code snippet shows how you can change the title for the taxonomy term landing pages:

<?php
function MYTHEME_preprocess_page(&$variables) {
  if (
arg(0)=='taxonomy' && arg(1)=='term') {
   
$variables['title'] = "Content tagged with: " . $variables['title'];
  }
}
?>

Eric's picture

This morning I realized my blog needed some CSS tweaks for Camino (sorry, Camino users). Here's the code I used to test for the Camino browser and conditionally load a style sheet to fix the CSS issues.

<?php
// I put this function in my theme's template.php file:
function MYTHEME_preprocess_page(&$variables) {
  if (
stripos($_SERVER['HTTP_USER_AGENT'], 'Camino')!==false) {
   
$variables['styles'] .= "<link type='text/css' rel='stylesheet' media='all' href='" . base_path() . path_to_theme() . "/style.camino.css' />";
  }
}
?>

I then created a file in my theme directory called "style.camino.css" and put my CSS tweaks in there.

Eric's picture

This code snippet will show you how to search through the breadcrumbs and remove a particular item. Since I added this code to my theme in a preprocess_page function, I had to recreate $variables['breadcrumb']. NOTE: If I had chose to add this code to a module instead, I could have simply used the drupal_get_breadcrumb() and drupal_set_breadcrumb functions.

<?php
function MYTHEME_preprocess_page(&$variables) {
 
// in this example, I'm checking to see if icon_users.png exists in the breadcrumbs
 
if (stripos($variables['breadcrumb'],'icon_users.png')) {
   
// get the current breadcrumbs
   
$bc = drupal_get_breadcrumb();

   
// loop through the breadcrumbs
   
foreach ($bc as $k => $v) {
     
// check for a condition and remove as necessary
     
if (stripos($v,'icon_users.png')) unset($bc[$k]);
    }

   
// recreate the breadcrumbs using the theme_breadcrumb function
   
$variables['breadcrumb'] = theme('breadcrumb', $bc);
  }
}
?>

Eric's picture

Most SEO tutorials claim that meta keywords are not very important to search engines. But, if you insist on inserting meta keywords for each node's taxonomy terms, this tutorial will show you how to accomplish this. I added the following function to my template.php file in my theme.

<?php
function _phptemplate_variables($hook, $vars) {
 
// check for page scope
 
if ($hook == 'page') {
   
// ensure this page is a node view
   
if (is_object($vars['node'])) {
     
// get a list of taxonomy terms for this node
     
$terms = taxonomy_node_get_terms($vars['node']->nid);
           
     
// ensure terms exist
     
if (is_array($terms)) {

       
// loop through terms, and collect the names
       
$t = array();
        foreach (
$terms as $k => $v) {
         
$t[] = $v->name;   
        }
               
       
// ensure names exist
       
if (is_array($t)) {
         
// implode the terms into a string and add to the head variable
         
$vars['head'] .= "<meta name='keywords' content='" . implode(",", $t) . "'>";
        }

      }

    }
       
  }
  return
$vars;
}
?>

To verify this code is working, check out the tags I used on this page and then view the source. Hopefully, they match!

(NOTE: my homepage is a view, so there show not be any meta keywords.)


NOTE: when I upgraded my theme to Drupal 6, I had to update this above code:

<?php
function MYTHEME_preprocess_page(&$variables) {
  if (
is_object($variables['node']) && is_array($variables['node']->taxonomy)) {
   
$tags = array();
    foreach (
$variables['node']->taxonomy as $tid => $term) {
      if (!
in_array($term->name, $tags)) $tags[] = $term->name;
    }
       
    if (
count($tags)) {
     
sort($tags);
     
$variables['head'] .= "<meta name='keywords' content='" . implode(",", $tags) . "'>";
    }
  }
}
?>

Eric's picture

If you're running a multi-site Drupal configuration, it's helpful to know which settings.php is being loaded. This information may be useful to your module or theme, or for troubleshooting purposes. For instance, you might be sharing the same theme across a few sites, but you'd like to modify the theme for one of your sites. Sharing themes (and modules) is capable in a multi-site configuration by placing your themes and modules in the [sites/all] folder, while defining a directory and settings.php file for each site installation in [sites]. To be able to tell which settings.php is being loaded, you could add the following code to each settings.php file you define:

<?php
$exploded
= explode("/", __FILE__);
$conf['multi_site_hostname'] = $exploded[count($exploded)-2];
?>

For example, you could pass this information to your page.tpl theme file:

<?php
// defining a preprocess_page function, placed in my template.php file:
function MYTHEME_preprocess_page(&$variables) {
 
// example: pass the configuration directly to the page.tpl.php:
 
$variables['multi_site_hostname'] = variable_get('multi_site_hostname',NULL);

 
// another example:
 
switch (variable_get('multi_site_hostname',NULL)) {
    case
'thedrupalblog.erl.dev':
    case
'thedrupalblog.com':
     
$variables['siteHostName'] = 'thedrupalblog.com';
      break;
    default:
     
$variables['siteHostName'] = 'default';
      break;
  }

}
?>