I need creating a series of menu items dynamically based on values from the database.
Here is a example of my hook_menu()
function; in this snippet, I removed the DB queries and just added an array to prove the concept:
function MYMODULE_menu() {
$years = array(2011, 2010, 2009);
foreach ($years as $year) {
$items['events/' . $year] = array(
'title' => $year . ' Events Archive',
'page callback' => 'page_archive',
'page arguments' => array(1),
'access callback' => TRUE,
'type' => MENU_NORMAL_ITEM,
'menu_name' => 'main-menu',
);
}
return $items;
}
This works and I get three menu items in the Main Menu titled 2011 Events Archive, 2010 Events Archive and 2009 Events Archive.
The problem I am having is the position of the dynamically created menu items in the Main Menu tree; they appear at the top level, but I want they appear under the parent Events menu item.
Currently, my menu tree looks something similar to the following.
- Home
- Events
- About Us
- Contact Us
- 2011 Events Archive
- 2010 Events Archive 2009 Events Archive
It should look like the following.
- Home
- Events
- 2011 Events Archive
- 2010 Events Archive
- 2009 Events Archive
- About Us
- Contact Us
I know you can move the menu items using the Menu editor UI when logged in, but as these will be automatically created by the system; they need to appear automatically under Events without human intervention.
How can this be done? Is there an easier way to do this?
hook_menu()
is another menu item defined fromhook_menu()
in any module. If "Events" is a menu item that links to a node, then there is a problem.