Before overriding theme functions, let us understand the concept of theme function.
In Drupal, a theme function is a PHP function that is used to output the HTML of any Drupal object. These functions are prefixed with theme_. All the functions that produce HTML are themeable and are invoked using theme() rather than being directly called.
We can override theme functions in template.php.
- Find out the theme function which produces the output that you want to change.
- Copy the code and paste it into template.php
- Rename the function. "theme_functionname()" will be renamed as "YourThemeName_functionname()". For eg: "theme_username()" will be renamed as "yourthemename_username()".
- Modify the output.
For example:
function theme_username($variables) {
if (isset($variables['link_path'])) {
// We have a link path, so we should generate a link using l().
// Additional classes may be added as array elements like
// $variables['link_options']['attributes']['class'][] = 'myclass';
$output = l($variables['name'] . $variables['extra'], $variables['link_path'], $variables['link_options']);
}
else {
// Modules may have added important attributes so they must be included
// in the output. Additional classes may be added as array elements like
// $variables['attributes_array']['class'][] = 'myclass';
$output = '' . $variables['name'] . $variables['extra'] . '';
}
$output = '<div class="username">' . $output . '</div>';
