You are here

function _taxonomy_menu_save in Taxonomy menu 6.2

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

Add/Update 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
HANDLING

File

./taxonomy_menu.module, line 496
It Generates menu links for all selected taxonomy terms

Code

function _taxonomy_menu_save($item) {

  // If this is an existing link pull the current link araray
  if ($item['mlid']) {
    $insert = FALSE;
    $link = menu_link_load($item['mlid']);
  }
  else {
    $insert = TRUE;
    $link = array();
  }

  // create the path.
  // use url to create he inital path
  // we need to remove the first '/' so menu_link_save will work correctly
  $path = taxonomy_menu_create_path($item['vid'], $item['tid']);

  // Make sure the path has less then 256 characters
  // @TODO This basicly breaks these menu items. There has to be a better way.
  if (strlen($path) > 256) {
    preg_match('/(.{256}.*?)\\b/', $path, $matches);
    $path = rtrim($matches[1]);
  }

  // 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 {
      $link['options']['query'] = $split[1];
      $link['link_path'] = $split[0];
    }
  }

  // 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) {
    $plid = variable_get('taxonomy_menu_vocab_parent_' . $item['vid'], NULL);
  }

  // Add updates
  $link['link_title'] = $item['name'];
  $link['menu_name'] = $item['menu_name'];
  $link['plid'] = $plid;
  $link['weight'] = $item['weight'] - 50;
  $link['module'] = 'taxonomy_menu';
  $link['expanded'] = variable_get('taxonomy_menu_expanded_' . $item['vid'], TRUE);
  $link['link_path'] = $path;
  $link['hidden'] = 0;

  // @todo i18nmenu need to be cleaned up to allow translation from other menu module
  if (module_exists('i18nmenu')) {
    $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
  if (!empty($item['remove'])) {
    $link['hidden'] = 1;
  }
  if (!variable_get('taxonomy_menu_blank_title_' . $item['vid'], FALSE)) {
    $link['options']['attributes']['title'] = strlen(trim($item['description'])) > 0 ? strip_tags(html_entity_decode($item['description'], ENT_COMPAT, "UTF-8")) : strip_tags(html_entity_decode($item['name'], ENT_COMPAT, "UTF-8"));
  }

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