Drupal: automatically add menu items when adding new nodes

Is it possible to automatically add a menu item when I add a node to a page in Drupal?

In other words, is it possible to associate a parent menu item with a node content type and then automatically add children if new nodes are added?

thank

+5
source share
9 answers

Yes.

I'm sure there is a module for something like that, but you can also create your own.

There are two ways to do this.

hook_menu() . , node, hook_nodeapi().  . , .

hook_nodeapi() menu_link_save().

Edit

hook_menu , , , .

, node, , , .

:

function example_menu() {
  $result = db_query('select * from node where ...'); // put in your own select items and where clause
  $menu = array();
  while ($row = db_fetch_object($result)) {
    $menu['my_path/' . $row->nid;] = array(
      // See hook menu docs for what to put here.
    );
  }
  return $menu;
}
+1

Drupal 7. : http://drupal.org/project/menu_rules . - node. : : node | a node : - " " : node ( , , )

+4

, , . API .

+2

Auto Menu module - Drupal 6 dev, , , , menu_link_save(), .

0

menu_link_save(). Rules , node , .

, , , menu_link_save() : http://jan.tomka.name/blog/programmatically-creating-menu-items-drupal

0

, Drupal, : . ( ):

Auto Menu node /. .

, node . , . , , .

0

.... A node , "CAMPAIGN 001" . default_menu_link , "-", node, EntityRef, , .

function mymodule_node_insert($node) {
  if ($node->type == 'sub-campaign') {
    if (isset($node->field_reference_campaign['und'][0]['target_id'])) {
      $campaign_node_id = $node->field_photo_album_campaign['und'][0]['target_id'];
      $campaign_loaded = node_load($campaign_node_id);
      // Get menu link id for the campaign node.
      $campaign_node_id_mlid = custom_node_mlid($campaign_node_id);
      $campaign_loaded_title = strtolower(str_replace(' ', "-", $campaign_loaded->title));
      $campaign_loaded_title_link_path = 'campaign/' . $campaign_loaded_title . '/photo-albums';
      //I will query if it exist or not, if not then will create a sub menu item.
      $link_exist = db_query("SELECT * FROM {menu_links} WHERE link_path = :link_path", array(':link_path' => $campaign_loaded_title_link_path))->fetchField();
      dsm($link_exist);
      if (!$link_exist) {
        // Create menu item under campaign.
        custom_create_menu_item($campaign_loaded_title_link_path, 'photo-albums', $campaign_node_id_mlid);
        //watchdog('glue_site - Menu Item', 'Link Created');
      }
      else {
        //dsm('Link Exist.');
        watchdog('glue_site - Menu Item', 'Link Already Exist');
      }
    }
  }
  if ($node->type == 'campaign') {

  }
}

function custom_create_menu_item($campaign_loaded_title_link_path, $type, $plid) {
  switch ($type) {
    case 'photo-albums':
      $item = array(
        'link_path' => $campaign_loaded_title_link_path,
        // If changing the title here, change it in template.php as well.
        'link_title' => 'Sub Campaign',
        'menu_name' => 'menu-campaign-menu', // Menu machine name, for example: main-menu
        'weight' => 0,
        'plid' => $plid, // Parent menu item, 0 if menu item is on top level
        'module' => 'menu',
        'router_path' => 'campaign/%/sub-campaign',
        'customized' => '1',
      );
      menu_link_save($item);
      menu_cache_clear_all();
      watchdog('glue_site - Menu Item', 'Link Created');
      break;
  }
} 

mlid node. node...

function custom_node_mlid($nid) {
  // Require menu node module.
  $arr = menu_node_get_links($nid);
  $mlid = array_keys($arr);
  return $mlid[0];
}

menu_node

0

, , Drupal , . . , , Drupal 6 Drupal 7, . . , . , , , , [ ] [ ] β†’ [], . .

D6

D7

0

Menu Views is an interesting module for Drupal 7 for automatically creating menu links. It allows you to use the power of Views to create links to menus and can be used out of the box in combination with modules such as Superfish and Nice menus .

(PS: my reputation is not high enough to provide more than two links, so I highlighted the other modules in bold instead of providing hyperlinks)

-1
source

All Articles