How to override a theme function from another module

If your theme has a template engine you can override a theme function in both your module and in your theme template.php

To override the function call theme('item_list') you have to copy the original function named theme_item_list and add it to your module or template.php replace theme_ with phptemplate_ and make your changes to the code.

<?php
//Copy the original theme function
function theme_item_list($items = array(), $title = NULL, $type = 'ul', $attributes = NULL) {
 
$output = '<div class="item-list">';
  if (isset(
$title)) {
   
$output .= '<h3>'. $title .'</h3>';
  }

  if (!empty(
$items)) {
   
$output .= "<$type". drupal_attributes($attributes) .'>';
   
$num_items = count($items);
    foreach (
$items as $i => $item) {
     
$attributes = array();
     
$children = array();
      if (
is_array($item)) {
        foreach (
$item as $key => $value) {
          if (
$key == 'data') {
           
$data = $value;
          }
          elseif (
$key == 'children') {
           
$children = $value;
          }
          else {
           
$attributes[$key] = $value;
          }
        }
      }
      else {
       
$data = $item;
      }
      if (
count($children) > 0) {
       
$data .= theme_item_list($children, NULL, $type, $attributes); // Render nested list
     
}
      if (
$i == 0) {
       
$attributes['class'] = empty($attributes['class']) ? 'first' : ($attributes['class'] .' first');
      }
      if (
$i == $num_items - 1) {
       
$attributes['class'] = empty($attributes['class']) ? 'last' : ($attributes['class'] .' last');
      }
     
$output .= '<li'. drupal_attributes($attributes) .'>'. $data ."</li>\n";
    }
   
$output .= "</$type>";
  }
 
$output .= '</div>';
  return
$output;
}


//Your version. In this case I have just added "my-class" for showing
function phptemplate_item_list($items = array(), $title = NULL, $type = 'ul', $attributes = NULL) {
 
$output = '<div class="item-list my-class">';
  if (isset(
$title)) {
   
$output .= '<h3 class="my-class">'. $title .'</h3>';
  }

  if (!empty(
$items)) {
   
$output .= "<$type". drupal_attributes($attributes) .'>';
   
$num_items = count($items);
    foreach (
$items as $i => $item) {
     
$attributes = array();
     
$children = array();
      if (
is_array($item)) {
        foreach (
$item as $key => $value) {
          if (
$key == 'data') {
           
$data = $value;
          }
          elseif (
$key == 'children') {
           
$children = $value;
          }
          else {
           
$attributes[$key] = $value;
          }
        }
      }
      else {
       
$data = $item;
      }
      if (
count($children) > 0) {
       
$data .= theme_item_list($children, NULL, $type, $attributes); // Render nested list
     
}
      if (
$i == 0) {
       
$attributes['class'] = empty($attributes['class']) ? 'first' : ($attributes['class'] .' first');
      }
      if (
$i == $num_items - 1) {
       
$attributes['class'] = empty($attributes['class']) ? 'last' : ($attributes['class'] .' last');
      }
     
$output .= '<li'. drupal_attributes($attributes) .'>'. $data ."</li>\n";
    }
   
$output .= "</$type>";
  }
 
$output .= '</div>';
  return
$output;
}
?>

Your theme must be using phptemplate engine for its templates and functions to be discovered. Other engines should behave the same way. For engineless themes, it must be done manually. See phptemplate_theme to see how it is done.

Knowledge keywords: