I want to create a new menu programmatically add for him some elements and the activate it in some content region as a block.

How can I do this programmatically?

I know how to add for an existing menu new elements, but I don't know how to create a new menu.

2 Answers 2


You save the menu using menu_save:

$menu = array(
  'menu_name' => '',
  'title' => '',
  'description' => '',

I'm not sure if there is an api function to activate a block, but it can be done, by inserting a row in the blocks table. You need to know the module, delta, theme and region. For menus, the menu module will create one with the delta being the menu_name.

So with the above you could do something like this:

$menu_block = array(
  'module' => 'menu',
  'delta' => $menu['menu_name'],
  'theme' => '', // Either get the active theme or you can do it for all themes
  'region' => 'content', // Where you want to place it, theme dependant
  'status' => 1,

So while you can do it, you probably want to make this theme specific, or do this in an install profile, where you know which theme should be the active one etc.

  • the menu creation is OK, but the activation is not working as you said. Maybe I need to save somehow $menu_block array ?!
    – EKK
    Apr 2, 2011 at 11:00
  • I have tried like this $block = module_invoke('my-block-name', 'block_view', 0); print $block['content']; but is not working...
    – EKK
    Apr 2, 2011 at 14:26
  • @Ek you need to save the menu_block array to the blocks table using drupal_write_record
    – googletorp
    Apr 2, 2011 at 17:24
  • 1
    Thanks, now is working. However drupal_write_record('block', $menu_block); need that in $menu_block array to be set also 'pages' => '', otherwise it display an error.
    – EKK
    Apr 3, 2011 at 5:58

I had a similar question a few days ago. You can make the menu as you normally would in a module using hook_menu and then create the new menu using the hook_install

 $items['devel/cache/clear'] = array(
  'title' => 'Empty cache',
  'page callback' => 'devel_cache_clear',
  'description' => 'Clear the CSS cache and all database cache tables which store page, node, theme and variable caches.',
  'access arguments' => array('access devel information'),
  'menu_name' => 'devel',

Custom module with its own 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.