You are here

public function _menu_editor_SaveEngine::saveItem in Menu Editor 6.3

File

./menu_editor.admin.inc, line 505

Class

_menu_editor_SaveEngine

Code

public function saveItem(&$item, $item_key) {
  $item['customized'] = 1;

  // check the link path
  $link_path =& $item['link_path'];

  // set the parent link id
  if ($item['plid'] && !is_numeric($item['plid'])) {
    if (isset($this->_mlids["mlid-{$item['plid']}"])) {
      $item['plid'] = $this->_mlids["mlid-{$item['plid']}"];
    }
    else {
      unset($item['plid']);
    }
  }

  // placeholders
  $link_path_placeholder = NULL;
  if (isset($this->_placeholders[$link_path])) {
    $link_path_placeholder = $this->_placeholders[$link_path];
    if (is_numeric($item['mlid'])) {

      // existing menu item
      $link_path = _menu_editor_get_path_for_placeholder($link_path_placeholder, $item);
      $link_path_placeholder = NULL;
    }
    else {

      // new menu item
      // use a dummy link path,
      // until we know the correct mlid.
      $link_path = '<front>';
    }
  }

  // clean the link path
  if (isset($link_path)) {
    $link_path = drupal_get_normal_path($link_path);
    if (!menu_path_is_external($link_path)) {
      $parsed_link = parse_url($link_path);
      if (isset($parsed_link['query'])) {
        $item['options']['query'] = $parsed_link['query'];
      }
      else {
        unset($item['options']['query']);
      }
      if (isset($parsed_link['fragment'])) {
        $item['options']['fragment'] = $parsed_link['fragment'];
      }
      else {
        unset($item['options']['fragment']);
      }
      if ($link_path != $parsed_link['path']) {
        $link_path = $parsed_link['path'];
      }
    }
    if (!trim($link_path) || !menu_valid_path($item)) {

      // invalid link path, discard this item
      continue;
    }
  }

  // update the item, or create it if
  $mlid = menu_link_save($item);
  if (is_numeric($mlid)) {

    // remember as a plid for child items
    $this->_mlids[$item_key] = $mlid;
    if (isset($link_path_placeholder)) {

      // overwrite the dummy link path
      $link_path = _menu_editor_get_path_for_placeholder($link_path_placeholder, $item);
      menu_link_save($item);
    }
    if (module_exists('i18nmenu')) {

      // See http://drupal.org/node/786230#comment-2911038
      $item['parent'] = $item['menu_name'] . ':' . $item['plid'];

      // Ensure we have a menu item to work with.
      _i18nmenu_update_item($item);
    }
    return $mlid;
  }
  else {
    $this->_errors[] = $item_key;
    return NULL;
  }
}