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();

 

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);