I am still working on the demo content sub-module for Organic Groups. I'd like to add a menu item during the installation process. How do I do that?

Specifically, I've created a view page called "groups" and I'd like a menu link in the main menu to "groups."

Accepted answer

My initial goal was to create a menu item that linked to a view that I had configured and that was being created on installation of the module, by including the view config file. 

To do this, I just needed to configure the menu link in the view before exporting the config file and the link is created automatically.

You can create a view simply by including the config file in the module config directory.

To create a link to a existing node, one can look at the standard install profile in core for some excellent sample code. 

// Create a Home link in the main menu.
  $item = array(
    'link_title' => st('Home'),
    'link_path' => '<front>',
    'menu_name' => 'main-menu',
    'weight' => -1,
  );
  menu_link_save($item);

  // Create a login link in the account menu.
  $item = array(
    'link_title' => st('Login'),
    'link_path' => 'user/login',
    'menu_name' => 'user-menu',
    'weight' => -1, 
  );
  menu_link_save($item);

  // Update the menu router information.
  menu_rebuild();

 

Most helpful answers

Found the solution: Needed to add the 'customized' key to the $item array, with a value of 1.

$item = array(
    'link_path' => 'media/photos',
    'link_title' => t('Photos'), 
    'customized' => 1,
  );
  menu_link_save($item);

Comments

My initial goal was to create a menu item that linked to a view that I had configured and that was being created on installation of the module, by including the view config file. 

To do this, I just needed to configure the menu link in the view before exporting the config file and the link is created automatically.

You can create a view simply by including the config file in the module config directory.

To create a link to a existing node, one can look at the standard install profile in core for some excellent sample code. 

// Create a Home link in the main menu.
  $item = array(
    'link_title' => st('Home'),
    'link_path' => '<front>',
    'menu_name' => 'main-menu',
    'weight' => -1,
  );
  menu_link_save($item);

  // Create a login link in the account menu.
  $item = array(
    'link_title' => st('Login'),
    'link_path' => 'user/login',
    'menu_name' => 'user-menu',
    'weight' => -1, 
  );
  menu_link_save($item);

  // Update the menu router information.
  menu_rebuild();

 

Just using this old post of mine to create a new menu link.

Today, I programmatically created a "Contact Us" webform as part of a recipe and wanted to create a link to the webform.

Using the code I posted above, with 'link_path" => 'contact-us' did not work.

It seems, I need the NID and to create the link as 'link_path => 'node/' . NID'

I was able to get it working with this code:

    $webform->save();
    $nid = $webform->nid;

    $link = array(
      'link_title' => st('Contact Us'),
      'link_path' => 'node/' . $nid,
      'menu_name' => 'main-menu',
      'weight' => 5,
    );
    menu_link_save($link);
robertgarrigos's picture

I'm trying to do this also with a layout and some views, with no luck. I create the views and include them in the config directory of the module, as well as the layout. Then create the menu links within in the hook_install function. The proper entries are created in the menu_links and menu_router tables. However, whenever I run menu_rebuild(), the entries in the menu_links disappear, but not the ones in the menu_router. What's going on?

robertgarrigos's picture

Found the solution: Needed to add the 'customized' key to the $item array, with a value of 1.

$item = array(
    'link_path' => 'media/photos',
    'link_title' => t('Photos'), 
    'customized' => 1,
  );
  menu_link_save($item);