Tell me more ×
Drupal Answers is a question and answer site for Drupal developers and administrators. It's 100% free, no registration required.

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.

share|improve this question

2 Answers

up vote 2 down vote accepted

You save the menu using menu_save:

$menu = array(
  'menu_name' => '',
  'title' => '',
  'description' => '',
);
menu_save($menu);

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.

share|improve this answer
the menu creation is OK, but the activation is not working as you said. Maybe I need to save somehow $menu_block array ?! – Ek Kosmos Apr 2 '11 at 11:00
I have tried like this $block = module_invoke('my-block-name', 'block_view', 0); print $block['content']; but is not working... – Ek Kosmos Apr 2 '11 at 14:26
@Ek you need to save the menu_block array to the blocks table using drupal_write_record – googletorp Apr 2 '11 at 17:24
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. – Ek Kosmos Apr 3 '11 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?

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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