I have a function in my template.php file but I've now created a module that manages these particular tasks so I want to move the function over.

This is what I have in my template.php file:

function MYTHEME_menu_tree__main_menu($variables){
    return '<ul class="menu my-class">' . $variables['tree'] . '</ul>';

This works file but when I move it into my custom module (mymodule.module file) if doesn't work.

I've tried this:

function MYMODULE_menu_tree__main_menu($variables){
    return '<ul class="menu my-class">' . $variables['tree'] . '</ul>';  

and this:

function MYMODULE_preprocess_menu_tree__main_menu($variables){
    return '<ul class="menu my-class">' . $variables['tree'] . '</ul>';  

but not happening.

Can anyone see where i'm going wrong? or am I way off on this and it can't be done like this?

  • You would need to use hook_theme_registry_alter() and replace the callback function for that hook; the naming convention for overrides only works for themes
    – Clive
    May 20, 2016 at 15:32
  • Thanks @Clive - I don't quite understand. Are you able to elaborate on your response. May 20, 2016 at 16:14

1 Answer 1


hook_theme_registry_alter() allows a module to step in to override registered theme callback functions. In your case, you're looking to override the menu_tree__main_menu entry and replace its callback function:

 * Implements hook_theme_registry_alter().
function MYMODULE_theme_registry_alter(&$theme_registry) {
  if ($theme_registry['menu_tree__main_menu']) {
    // @TODO: override
    // $theme_registry['menu_tree__main_menu']['function']
    // $theme_registry['menu_tree__main_menu']['type']
    // $theme_registry['menu_tree__main_menu']['theme path']
    // for your new module theme function

See the Return Value section of hook_theme() for more information on what a theme registry entry contains.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.