I have a D7 menu of nearly 300 terms in a 3-level hierarchy. Rather than generate them one at a time in the UI, I would like to generate them using code. I am using the book 'Pro Drupal Development' and the Drupal API documentation for menus and the menus.module to attempt this, but am confused about how to establish the structure of the three tiers.

Creating a menu item appears to be fairly simple, implementing hook menu and creating item arrays with values assigned to 'title', 'page callback', 'access callback', and 'type'. My problem is that I must define the hierarchy and I am not clear on how that is done. How do I identify the parent in the array structure?

EDIT - additional information

The menu items, at all levels must have a callback to a view definition following the pattern View_Name/tid/display_type. The first level of the URL will be a unique label for the View being called. The next level is a term id identifying the specific term from a vocabulary that defines the relationships of the menu options. The third level determines which view page in the definition provides the view output for display. My question is addressing how to duplicate the taxonomy hierarchy in a code generated menu? How do I tell the Drupal the relationships between the links to duplicate the three tier menu system when the links are created? I can provide my customized callbacks but haven't found the correct array key-value settings to generate the menu correctly.

I have experimented using the Taxonomy_Menu module to generate the menu directly, but I cannot find a way to generate different third level URL values within the module. The module generates a menu of taxonomy links and Drupal does not expose the links for editing.

2 Answers 2


I'm not sure to understand your problem, but as you define everything by code, it should be straightforward.

You can create any menu entry you want, on any existing or newly created entry.

As for the hierarchy, you just have to define your paths like any URL path. As per the doc : If you have

function mymodule_menu() {
    $items['abc'] = array(
      'page callback' => 'mymodule_abc_view',
    return $items;

You can then define a child entry like this :

function mymodule_menu() {
    $items['abc/def'] = array(
      'page callback' => 'mymodule_abc_def_view',
    return $items;

This will give an entry at the root level 'abc' and a child entry 'abc/def'.


  • Thanks, I have added to my question to clarify how I am using the URL to link to a view. I understand that Drupal would understand the URL you suggest as parent/child, but it is also possible to do it without the URL. I have had trouble finding the correct documentation/examples to figure this out. I am new to the coding end so maybe I don't know where to look, or have missed the settings required.
    – Ashlar
    Commented Mar 2, 2012 at 16:11
  • I think you should investigate the hook_menu documentation, particularly the wildcards part. It's not easy to understand, but I think it's needed for what you're trying to achieve.
    – Countzero
    Commented Mar 2, 2012 at 16:38

Well check out my question Override menu item weight (bring it to same level as 'user' link) that has user/% path and has to be in user-menu

and use

'plid' => '0' or the mlid of the menu you want as parent

in the $items of hook_menu()

Your Answer

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

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