Tuesday, October 30, 2012

Adding menu items to an drupal 7 install profile

using the .install file ( located here: <websitedir>/profiles/<profilename>/<profilename>.install )

You can add menu items by extending the array like in the standard install profile:

adding this will put the block main-menu in the menu_bar region:

array(
      'module' => 'system',
      'delta' => 'main-menu',
      'theme' => $default_theme,
      'status' => 1,
      'weight' => 0,
      'region' => 'menu_bar',
      'pages' => '',
      'cache' => -1,
    ),
The trick was here setting the module to 'system' instead of 'menu' otherwise errors will show up after install. 
You can define the theme you want to apply this for or don't set it to apply on all themes. Since this is a system default location you should be fine not setting the theme.

Then let the following query run ( just like the standard.install file )


 $query = db_insert('block')->fields(array('module', 'delta', 'theme', 'status', 'weight', 'region', 'pages', 'cache'));
  foreach ($values as $record) {
    $query->values($record);
  }
  $query->execute();
Then creation the menu items like so:


// create menu
  $item = array(
    'link_title' => st('Home'),
    'link_path' => '<front>',
    'menu_name' => 'main-menu',
  );
 menu_link_save($item);


Optional you could do a menu_rebuild but I didn't see a difference while installing.
maybe this is only for modules so the menu gets 'cleared' after enabling a module.
since the whole site is just build it might not be necessary doing this in a profile .install file


Update the menu router information.
menu_rebuild();