background image
HomeRecent PostsDrupalSearchTagsRSSContactAboutAccount
Eric.London'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.London's picture
  • Most importantly, read all the documentation
  • Second most important, learn when to use a preprocess function versus a template file. The Devel module's Theme Information will help determine the name of core template suggestions and preprocess functions
  • Put your code in a preprocess functions, your html in tpl.php files, and your CSS in a style sheet. Avoid mixing these files when possible. Following the rules of MVC frameworks will help promote flexible and clean code
  • Before creating a new template file (or preprocess function), see if the theme can be adjusted using: 1) a configuration change (for example, changing a view's layout); or 2) simply adding or modifying CSS
  • If you find yourself reusing code, create a function in template.php to promote consistency
  • Include javascript and style sheets conditionally to improve performance and reduce overhead
  • Avoid using inline style or javascript. Put CSS in a style sheet; and use drupal_add_js() to include javascript
  • When creating new template files, be sure to register them in hook_theme() and use the theme() function to call them
  • When creating a new tpl.php template file, make sure to include the same variables used in the default template, or you might be missing something in your theme (tabs, title, help, etc).
  • When defining new variables in a preprocess function, prefix the variable names with a common identifier so you call easily tell your custom variables apart from the Drupal default variables.
  • When theming a view, take advantage of the "Theme: Information" link under basic settings to help determine the right template file to use. For instance, if you want to modify the output for a single field, don't create a views-view.tpl.php file; use a views-view-field.tpl.php file instead
  • Keep scalability in mind at all times. For instance, when theming a view, will your code break if the admin switches from a page layout to a block layout? Also, try to avoid setting a fixed height on regions, there is no telling how many content items & blocks the user will attempt to assign to a region.
  • When creating regions and columns in your theme, test for their contents before displaying them. If your regions do not have any blocks assigned, you can increase the size of your main column/region and avoid large areas of awkward white space.
  • When theming a menu block in a region, user CSS selectors specific to the region (not the block). That way when you assign another menu to the same region, you will not have to replicate the CSS for the menu block.
  • Modify forms using hook_form_alter() when possible
  • Use file versioning software like Subversion to track and deploy your theme edits
  • NOTE: more tips coming soon, please check back again.
Eric.London's picture

This code snippet shows you how you can create alternate theme layouts for certain pages using a preprocess function. In this function, we'll test for certain conditions and apply different CSS classes to the body tag. The following function belongs in your theme's template.php file:

<?php
function MYTHEME_preprocess_page(&$variables) {
 
$exploded = explode('/', $_REQUEST['q']);
  if (
$variables['is_front']) {
   
$bodyClass = 'front';
  } elseif (
$exploded[0]=='admin') {
   
$bodyClass = 'admin';   
  } else {
   
$bodyClass = 'interior';   
  }
 
$variables['bodyClass'] = $bodyClass;
}
?>

Next, in your page.tpl.php file, replace your opening body tag with the following:

<body class="<?php print $bodyClass; ?>">

Now, you can add CSS to change the layout of your page based off of the classes you applied to the body tag. For example:

/* hide right column for admin pages */
body.admin .column_right{ display: none; }

/* set width for right column on interior pages */
body.interior .column_right { width: 250px; }

/* set different width for right column on home page
body.front .column_right { width: 500px; }

When creating your theme, you may need absolute control over your node fields, and creating a node specific template file (CONTENTTYPE.tpl.php) does not give you enough flexibility. This code snippet defines a preprocess_node hook to modify the $content variable, so the field html has already been modified before it reaches your theme.

<?php
function MYMODULE_preprocess_node(&$variables) {
 
// test for your node type that you'd like to modify
 
if ($variables['type']=='MYNODETYPE') {
   
// prepare your node object so the fields can be rendered individually
   
$newNode = node_build_content(node_load($variables['nid']));

   
// define a variable to hold your rendered content
   
$newContent = "";

   
// example: define a list of fields to show in an item list
   
$itemListFields = array('field_itemlist_1','field_itemlist_2','field_itemlist_3');
   
$itemListFieldsData = array();

   
// loop through the node content
   
foreach ($newNode->content as $k => $v) {
     
// example: to prevent a field from being shown,
      // simply render the field and don't capture the output
     
if ($k == 'MYFIELDTOHIDE') {
       
drupal_render($v);
      } elseif (
in_array($k, $itemListFields)) {
       
// example: group some fields in an item list
       
$itemListFieldsData[] = drupal_render($v);
      } elseif (
$k == 'MYOTHERFIELD') {
       
// example: surround a field in a div
       
$newContent .= "<div id='MYHTMLID'>" . drupal_render($v) . "</div>";
      } else {
       
// render the remaining fields and capture the output
       
$newContent .= drupal_render($v);
      }

    }

   
// example: generate the output for the item list
   
if (count($itemListFieldsData)) $newContent .= theme('item_list', $itemListFieldsData);

   
// replace the content variable
   
if (strlen($newContent)) $variables['content'] = $newContent;

  }

}
?>

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;
  }
}
?>

Syndicate content