Taxonomy Templates
The previous section described how to build new templates based on URL aliases and content type. When you are designing a site to have category-specific enhancements, it is very likely that you want to change the colors or graphical elements of the page template. This section explores ways to create a new page template so as to add color-specific sections and new variables. To accomplish this feat, you will use the same techniques you learned in the previous section.
Unfortunately, categories are easily edited and are not associated with permanent machine names. You may find it helpful to print the taxonomy variable to the page to see how categories are stored and accessed. You can also obtain this information by using the developer module Themer Info tool in the Devel module. Refer to Chapter 2 for more information on using this module.
Here are the contents of one taxonomy variable:
[taxonomy] => Array ( [3] => stdClass Object ( [tid] => 3 [vid] => 1 [name] => Available for retail and wholesale. [description] => [weight] => 0 ) [11] => stdClass Object ( [tid] => 11 [vid] => 2 [name] => Books Published by The Ginger Press [description] => [weight] => 0 ) )
In this example, the category being used to change the template variable is the first category contained in the array of data in the taxonomy variable. The first four steps of the preprocess_page function described in “New Templates from Aliased URLs” section are repeated. At this point, you should adjust the variable $target_tax so that it matches the position of the category you want to use to distinguish between sections on your site. This function assumes that you are working within one vocabulary and that each term is a different template. You will need to adjust the scripting if your site differs from this model.
The explanations of steps 1 through 4 can be found in the section “New Templates from Aliased URLs.” The new steps perform the following actions:
- Check whether this page has a system path of node/nid. This snippet will work only if you are displaying a single node of any content type.
- Check whether this page has been assigned a category. Retrieve the whole array of categories if it does.
- Retrieve the name of the category.
- Convert the category name to a plain text string of characters suitable for a file name. This operation includes replacing spaces with a dash and converting all characters to lowercase.
- Add the new template suggestion to the list of page template suggestions; add the category name to the list of existing body classes.
function bolg_preprocess_page(&$variables) { // Step 1: if (module_exists('path')) { // Step 2: $url_alias = drupal_get_path_alias($_GET['q']); // Step 3: $alias_parts = explode('/', $url_alias); // Step 4: $last = array_reverse($alias_parts); $last_part = $last[0]; if ($last_part != "edit") { // Step 5: if (arg(0) == "node" && is_numeric(arg(1))) { // Step 6: if (isset($variables['node']->taxonomy)) { $target_tax = 0; $node_tax = $variables['node']->taxonomy; // Step 7: $tid = array_keys($node_tax); $name = $node_tax[$tid[$target_tax]]->name; // Step 8: $clean_name = check_plain($name); $dash_name = str_replace(" ", "-", $clean_name); $lc_name = strtolower($dash_name); // Step 9: array_push($variables['template_files'], "page-tax-$lc_name.tpl.php"); $variables['body_classes'] .= $variables['body_classes'] . " tax-$lc_name"; } // End of the taxonomy check } // End of the node/nid check } // End of the edit check } // End of the check for the path module } // End of the preprocess_page function
Graphical Headers
The last function introduced in this chapter allows you to change the template or add a new CSS class to a page based on the category assigned to a page. Wouldn’t it be neat if you could change the graphical header for that page as well? With the snippet of code provided here, you will be able to place images into a folder in your theme directory and have them be automatically displayed for unique categories within your Web site.
This snippet can be used as a replacement for step 9 in the preceding section, or it can be used as a further enhancement. It assumes that all of the images reside in a subdirectory of your theme named tax and that all image files are named with the lowercase extension jpg. You may change these settings, if necessary. The image files should all be named according to the following convention: Using the term name, replace all spaces with a dash and convert all letters to lowercase. A default image should also be available if a matching taxonomy-specific image cannot be found.
$image_dir = "tax"; $ext = "jpg"; $default_image_file = "FILENAME.jpg"; $image_dir = drupal_get_path('theme', 'bolg') . "/$image_dir"); $default_image = "$image_dir/$default_image_file"; $image = "$image_dir/$lc_name.$ext"; if (file_exists($image){ $variables['tax_header'] = theme('image', $image, $clean_name, $clean_name); } elseif (file_exists($default_image){ $variables['tax_header'] = theme('image', $default_image, $clean_name, $clean_name); } else { $variables['tax_header'] = ""; }
Remember to put the default header graphic into the appropriate image folder in your theme!