You are here

function _taxonomy_menu_save in Taxonomy menu 7

Same name and namespace in other branches
  1. 6.2 taxonomy_menu.module \_taxonomy_menu_save()

Adds/Updates a taxonomy menu item.

We use a custom data array $item as a parameter, instead of using a standard taxonomy $term object. This is because this function is also called from hook_taxonomy(), which doesn't have a $term object. Rather have one consistent method of passing the data.

Parameters

$item: array with the following key/value pairs: 'tid' => the term id (if 0 then adding the vocab as an item) 'name' => the term's name 'description' => term description, used as to build the title attribute 'weight' => term weight (This will be overriden by the order created from taxonomy_get_tree which respects the correct wight) 'vid' => the vocabulary's id 'ptid' => the term's parent's term id 'menu_name' => the menu that the link item will be inserted into 'mlid' => if this is filled in then the mlid will be updated

1 call to _taxonomy_menu_save()
taxonomy_menu_handler in ./taxonomy_menu.module
Taxonomy Menu Handler: Creates a menu item for each taxonomy term.

File

./taxonomy_menu.module, line 649
Adds links to taxonomy terms into a menu.

Code

function _taxonomy_menu_save($item) {
  if (empty($item)) {
    return;
  }
  $insert = TRUE;
  $flatten_menu = variable_get(_taxonomy_menu_build_variable('flat', $item['vid']));

  // Child items should appear around the parent/root, so set their weight
  // equal to the root term's weight.
  if ($flatten_menu) {
    $item['weight'] = $item['root_term_weight'];
  }

  // create the path.
  $path = taxonomy_menu_create_path($item['vid'], $item['tid']);

  // Get the parent mlid: this is either:
  // - the parent tid's mlid
  // - the vocab menu item's mlid
  // - the menu parent setting for this vocab
  $plid = _taxonomy_menu_get_mlid($item['ptid'], $item['vid']);
  if (!$plid || $flatten_menu) {
    $plid = variable_get(_taxonomy_menu_build_variable('vocab_parent', $item['vid']), NULL);
  }

  // Make sure the path has less then 256 characters.
  if (drupal_strlen($path) > 256) {
    preg_match('/(.{256}.*?)\\b/', $path, $matches);
    $path = rtrim($matches[1]);
  }
  $link = array(
    'link_title' => $item['name'],
    'menu_name' => $item['menu_name'],
    'plid' => $plid,
    'weight' => $item['weight'],
    'module' => 'taxonomy_menu',
    'expanded' => variable_get(_taxonomy_menu_build_variable('expanded', $item['vid']), TRUE),
    'link_path' => $path,
  );

  // Be sure to load the original menu link to preserve non-standard properties.
  if (isset($item['mlid']) && !empty($item['mlid']) && ($original_link = menu_link_load($item['mlid']))) {
    $link = array_merge($original_link, $link);
  }
  else {
    $link['options'] = array(
      'attributes' => array(
        'title' => trim($item['description']) ? $item['description'] : $item['name'],
      ),
    );
  }

  // Add setup the query paramater in the URL correctly.
  if (strpos($path, '?') !== FALSE) {
    $split = explode('?', $path);
    if (strpos($split[1], '?') !== FALSE) {

      // the query split didn't work, too many question marks
      // error?
    }
    else {
      parse_str($split[1], $link['options']['query']);
      $link['link_path'] = $split[0];
    }
  }

  // If passed a mlid then add it.
  if (isset($item['mlid']) && $item['mlid']) {
    $link['mlid'] = $item['mlid'];
    $insert = FALSE;
    $menu_link = menu_link_load($item['mlid']);
    $is_hidden = !empty($menu_link['hidden']) ? $menu_link['hidden'] : 0;
  }
  else {
    $is_hidden = 0;
  }

  // @todo i18nmenu needs to be cleaned up to allow translation from other menu modules.
  if (module_exists('i18n_menu')) {
    $link['options']['alter'] = TRUE;
    $link['language'] = $item['language'];
    $link['customized'] = 1;
  }

  // Set the has_children property.
  // If tid=0 then adding a vocab item and had children.
  // If the term has any children then set it to true.
  if ($item['tid'] == 0) {
    $link['has_children'] = 1;
  }
  else {
    $children = taxonomy_get_children($item['tid']);
    if (!empty($children)) {
      $link['has_children'] = 1;
    }
  }

  // If remove is true then set hidden to 1.
  $link['hidden'] = isset($item['remove']) && $item['remove'] ? 1 : $is_hidden;

  // Save the menu item.
  if ($mlid = menu_link_save($link)) {

    // if inserting a new menu item then insert a record into the table.
    if ($insert) {
      _taxonomy_menu_insert_menu_item($mlid, $item['tid'], $item['vid']);
    }
    return $mlid;
  }
  else {
    drupal_set_message(t('Could not save the menu link for the taxonomy menu.'), 'error');
    return FALSE;
  }
}