Drupal: automatically add menu items when new node

2019-05-01 02:12发布

can I automatically add a menu item when I add a node to the page in Drupal?

In other words, can I associate a menu parent with a node content-type, and then automatically add the children if new nodes are added ?

thanks

9条回答
姐就是有狂的资本
2楼-- · 2019-05-01 02:23

This is a simple problem that unfortunately the Drupal community has decided it wants to make complicated. Forget about all the hacky solutions with rules and hooks. There are two modules, depending on whether you're on Drupal 6 or Drupal 7, that solve the problem very elegantly. I advise against actually creating menu entries. Instead the two modules below dynamically render the nodes in the menu, so that your menu editor doesn't get filled with thousands of nodes. Then, for example, if you decide you want all the blog posts to be moved from [Our Blog] to [About Us]->[News] it's just a mater of changing one setting. No updating thousands of nodes.

D6 Menu Trails

D7 Menu Position

查看更多
Emotional °昔
3楼-- · 2019-05-01 02:30

Here is case where you can do this.... A node campaign creating menu item 'CAMPAIGN 001' when it is created. Using default_menu_link Now another content type, 'Sub Campaign' creating a node, using campaign as EntityRef so its menu item should be under the Menu Item of campaign created earlier.

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') {

  }
}

Then a custom function to create menu item

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

To get the mlid of parent node. Campaign node...

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

For this you need menu_node

查看更多
聊天终结者
4楼-- · 2019-05-01 02:38

Menu Views is an interesting module for Drupal 7 to automatically generate menu links. It allows you to use the power of Views to create menu links 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, therefore I have marked the other modules bold instead of providing hyperlinks)

查看更多
We Are One
5楼-- · 2019-05-01 02:40

I would also go for a menu_link_save() call. Together with the Rules module, you can set up an action whenever a new node is saved, to create an appropriate menu item automatically.

You might want to have a look at the tutorial I wrote some time ago, which deals with programatically creating menu items using menu_link_save() and Rules: http://jan.tomka.name/blog/programmatically-creating-menu-items-drupal

查看更多
干净又极端
6楼-- · 2019-05-01 02:40

It looks like there's a Drupal module that does this: Auto Menu. Some more details about this module (from its project page):

The Auto Menu module automatically generates menu entries on node creation/edition. Parent menu item can be specified on a per content type basis.

This module acts when the menu section of a node is left empty only. So, users can still organize menus manually. Moreover, default setting for content types is to not create menu items automatically.

查看更多
\"骚年 ilove
7楼-- · 2019-05-01 02:46

You can do it with Rules on Drupal 7. This module: http://drupal.org/project/menu_rules adds some actions to rules. One of them is to create a menu item for a node. You select: Event: Create a node | Update a node Condition: Content type is "your content type" Action: Update a menu item for node (there is a checkbox to create the menu item if it doesnt exist)

查看更多
登录 后发表回答