How to add a theme function in your custom Drupal 7 module?

By | 24th Aug 2015 | 1 min read

In Drupal 7, a theme function is a PHP function that is used to wrap the output variables with HTML. For adding a theme function in our custom Drupal 7 module, we need to implement hook_theme(). A theme function is prefixed with 'theme_'. However, they can't override existing theme functions. Such functions are invoked using theme() rather than being directly called. Also, they are faster compared to the template (.tpl) file implementations.

/**
 * Uses a function called "theme_THEMEHOOK" to create the HTML.
 *
 * This is just to avoid a template file.
 */
function theme_comment_admin_overview($variables){
  // Isolate the form definition form the $variables array
 	$form = $variables['form'];
  // Pass the remaining form elements through drupal_render_children()
  $output = drupal_render_children($form);
  // Return a string that contains the rendered representation of the data.
  return $output;
}

For more help to use custom theme function, please contact us.