You are here

function menu_import_save_menu in Menu Export/Import 7

Import menu items.

Parameters

$menu: An associative array containing the menu structure.

$options: An associative array of import options.

  • link_to_content: look for existing nodes and link to them
  • create_content: create new content (also if link_to_content not set)
  • remove_menu_items: removes current menu items
  • node_type: node type
  • node_body: node body
  • node_author: node author
  • node_status: node status

Return value

Array of different statistics accumulated during the import.

3 calls to menu_import_save_menu()
menu_import_file in ./menu_import.module
Import menu from text file.
menu_import_form_step2_submit in includes/admin.inc
Step 2 form submit handler.
menu_import_string in ./menu_import.module
Import menu from text variable.

File

includes/import.inc, line 418
Import functions for menu_import module.

Code

function menu_import_save_menu($menu, $options) {
  $menu_items_deleted_cnt = $unknown_links_cnt = $external_links_cnt = 0;
  $nodes_matched_cnt = $nodes_new_cnt = $failed_cnt = 0;

  // Delete existing menu items.
  $menu_name = $menu[0]['menu_name'];
  if ($options['remove_menu_items']) {
    $menu_items_deleted_cnt = count(menu_load_links($menu_name));
    menu_delete_links($menu_name);
  }
  $menu[0]['mlid'] = 0;
  foreach ($menu as $item) {
    if (!isset($item['children'])) {
      continue;
    }
    foreach ($item['children'] as $index) {
      $menuitem = $menu[$index];
      $menuitem['plid'] = $menu[$menuitem['parent']]['mlid'];
      $menuitem['menu_name'] = $menu_name;

      // Do not create nodes for external links.
      if ($menuitem['external']) {
        $external_links_cnt++;
      }
      elseif ($menuitem['link_path'] && empty($menuitem['node_view'])) {
        $unknown_links_cnt++;
      }
      else {

        // Node exists.
        if ($menuitem['nid']) {

          // Try to link to existing content first.
          if ($options['link_to_content']) {

            // Delete any existing menu items from the current menu.
            if (!$menu_items_deleted_cnt) {
              menu_import_delete_node_menuitem($menuitem);
            }
            $nodes_matched_cnt++;
          }
          elseif ($options['create_content']) {
            $options['node_title'] = $menuitem['link_title'];
            $nid = menu_import_create_node($options);
            $menuitem['nid'] = $nid;
            $nodes_new_cnt++;
            $menuitem['link_path'] = 'node/' . $menuitem['nid'];
          }
        }
        else {

          // Create new link and node.
          if ($options['create_content']) {
            $options['node_title'] = $menuitem['link_title'];
            if ($options['node_alias'] && !empty($menuitem['path']) && arg($menuitem['path'], 0) != 'node') {
              $options['path_alias'] = $menuitem['path'];
            }
            $nid = menu_import_create_node($options);
            $menuitem['nid'] = $nid;
            $nodes_new_cnt++;
            $menuitem['link_path'] = 'node/' . $menuitem['nid'];
          }
          else {
            $unknown_links_cnt++;
            $menuitem['link_path'] = '<front>';
          }
        }
      }

      // Ensure we link to at least front page.
      if (empty($menuitem['link_path'])) {
        $menuitem['link_path'] = '<front>';
      }

      // Save description if available.
      if (isset($menuitem['description'])) {
        $menuitem['options']['attributes']['title'] = $menuitem['description'];
      }

      // Allow other modules to alter the imported data.
      drupal_alter('menu_import', $menuitem);

      // Save menuitem and set mlid.
      $mlid = menu_link_save($menuitem);
      if (!$mlid) {
        $failed_cnt++;
      }
      $menu[$index]['mlid'] = $mlid;
    }
  }
  return array(
    'external_links' => $external_links_cnt,
    'unknown_links' => $unknown_links_cnt,
    'matched_nodes' => $nodes_matched_cnt,
    'new_nodes' => $nodes_new_cnt,
    'deleted_menu_items' => $menu_items_deleted_cnt,
    'failed' => $failed_cnt,
  );
}