By DrupalFrank on
Is there a simple example anywhere that demonstrates creating a new menu from Drupal 7 module code?
I'd like there to be a new menu with a fixed set of options when a user loads the module I'm developing.
So far my efforts with the menu API have not produced happy results. (Had success creating a theme with custom regions, module creates blocks for them, etc, but no luck creating a menu from API or finding a Drupal 7 example on how to do it.)
Comments
Here is the code from the
Here is the code from the menu module in menu.admin.inc
which could be adapted to your needs.
Good so far!
Awesome tip nevets! My custom menu is now appearing after I added this function to my module based on the detail in your posting...
Now I'll move into the more complex looking items portion of the menu creation process. I'll want the items to look like tabs. Will post questions if I get stuck, and post solution if I find it.
Use hook_menu
hook_menu
See also the Examples module for developers which is mentioned in Creating modules - a tutorial: Drupal 7.x.
Examples tip
Thanks Lars Kramer for the Examples module tip. I installed the menu_example and am looking it it now. (Already went through the tutorials on Drupal.org, but somehow missed that.)
Two different things
Just a note that using menu_save and hook_menu are two different things. The first is creating a menu through the menu module that exists in the database, just like a menu that was created by hand in the GUI.
If you wanted to (for some reason) write a script to create 20 menus full of test content that could then later be edited through the menu gui, this would be what you wanted to use.
Using hook_menu is for defining menu structures in code... normally this is for building the url structure and routing that a module needs. This is the right way to go if you are building admin pages, or a callback structure.