When defining themeable output via a hook_theme() implementation in previous versions of Drupal, if you omitted the 'function'
and 'template'
array keys the output defaulted to a theme function and a theme function name was automatically generated. For example a theme hook of username
would match up with a theme_username()
theme function.
In Drupal 8, templates are the default output method for hook_theme(), and the 'template'
key is optional if your template file name is based on the theme hook. Underscores are converted to hyphens when building the template name. If the themeable output is being generated by a theme function (not recommended) then the base theme function needs to be defined in the hook_theme() implementation in the 'function' array key. Theme functions like theme_node_search_admin()
in the example below should be refactored or converted to Twig templates, see https://drupal.org/node/1831138.
If you specify a 'function' in your hook_theme() that doesn't exist, a BadFunctionCallException will be thrown when rebuilding the theme registry.
Before:
/**
* Implements hook_theme().
*/
function node_theme() {
return array(
'node' => array(
'render element' => 'elements',
'template' => 'node',
),
'node_search_admin' => array(
'render element' => 'form',
),
'node_add_list' => array(
'variables' => array('content' => NULL),
'file' => 'node.pages.inc',
'template' => 'node-add-list',
),
);
}
After:
/**
* Implements hook_theme().
*/
function node_theme() {
return array(
'node' => array(
'render element' => 'elements',
),
'node_search_admin' => array(
'render element' => 'form',
'function' => 'theme_node_search_admin',
),
'node_add_list' => array(
'variables' => array('content' => NULL),
'file' => 'node.pages.inc',
),
);
}