-1

I want to create a template file for a menu, and I am trying something like this:

function MYTHEME_menu_tree__main_menu($variables) {
   return theme('mymainmenu', $variables);
}

I edit the main menu's HTML in the mymainmenu.tpl.php, but this code is not working. Why?

apaderno
  • 28,547
  • 16
  • 75
  • 90
Alon
  • 889
  • 10
  • 16
  • 2
    You'll probably want to post your `theme_mymainmenu` and `hook_theme` functions as well as the problem is most likely in one of those – Clive Dec 07 '11 at 11:13

1 Answers1

1

I think you're taking the wrong approach here. In Drupal 7 you don't need to be calling the theme() method at all. You should instead focus on renderable arrays and calling drupal_render() which in turn calls theme() for you.

With that out of the way, lets focus on your problem at hand:

You should create a my hook_theme() implementation. This hook defines whether you are using a function or a template file for rendering your HTML. It also defines which variables to pass to the function/template. Here is a brief example of a hook_theme() implementation:

mymodule_theme($existing, $type, $theme, $path) {
  return array(
    'theme_name' => array(
       'variables' => array(
          'options' => NULL,
       )
       'template' => 'theme-name'
   );
}

In this example, when calling 'theme_name' you will also be able to pass a variable (options).

After that, create your tpl.php file and populate it with the HTML and data you want. You either create a template file or a function but not both.

Now, in the callback for your menu, you want to return a renderable array like so:

$output = array(
   '#theme' => 'theme-name',
   '#options' => $aVariable,
);
return $output;

As Clive mentioned in the comment, it would be more useful if you post all your methods referring to your problem so we may better gauge your problem.

KerrM
  • 5,139
  • 3
  • 35
  • 60
  • Thanks for the answer ! but i still dont get it, where do i place this function ? in my template.php ? do i need to create a module for this ? for example if i want i want to edit main menu, how shuold i edit it in this way ? thanks ! – Alon Dec 07 '11 at 16:07
  • Yes, you have to create a module for this and implement the hook_theme() there. The part where you output the code can also be called from within the module file. I don't quite understand what you want to do with the main menu though. Adding elements to the main menu should, if possible, be done through the Structure interface in the Administration section. – KerrM Dec 07 '11 at 16:26
  • Thanks, ill try that. I want to edit the html for example – Alon Dec 08 '11 at 07:55
  • If it's something as basic as just adding menu items to your Main Menu you should do it through the Structure interface. Good luck though. – KerrM Dec 08 '11 at 10:58