Blog/ How to override theme functions in Drupal 7

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.

  1. Find out the theme function which produces the output that you want to change.
  2. Copy the code and paste it into template.php
  3. Rename the function.  "theme_functionname()" will be renamed as "YourThemeName_functionname()". For eg: "theme_username()" will be renamed as "yourthemename_username()".
  4. 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>';


Related Articles:

Ready to get started?REQUEST A QUOTE