You are here

function _taxonomy_menu_create_item in Taxonomy menu 7

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

Creates the initial $item array.

Parameters

$args: array with the following key/value pairs: 'term': Taxonomy term object, if updating a term. 'menu_name': menu that the item is set to apply to. 'vid': Vocabuary id, if editing vocab item. 'mlid': Menu link id.

$node: The node object.

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

File

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

Code

function _taxonomy_menu_create_item($args = array(), $node) {

  // If tid = 0, then we are creating a vocab item.
  if (isset($args['tid']) && isset($args['vid']) && $args['tid'] == 0 && variable_get(_taxonomy_menu_build_variable('voc_item', $args['vid']), 0)) {
    $vocab = taxonomy_vocabulary_load($args['vid']);
    $item = array(
      'tid' => 0,
      'name' => $vocab->name,
      'description' => variable_get(_taxonomy_menu_build_variable('voc_item_description', $args['vid']), 0) ? $vocab->description : '',
      'weight' => $vocab->weight,
      'vid' => $args['vid'],
      'ptid' => 0,
      'root_term_weight' => $vocab->weight,
      'menu_name' => $args['menu_name'],
      'language' => $vocab->language,
    );
  }
  else {

    // If tid <> 0 then we are creating a term item.
    $term = $args['term'];

    // Sometimes $term->parents is not set so we find it.
    if (empty($term->parents)) {
      $term->parents = _taxonomy_menu_get_parents($term->tid);
      if (empty($term->parents)) {

        // Even without parents, create one with $ptid = 0.
        $term->parents = array(
          0 => '0',
        );
      }
    }

    // Find the weight of the root taxonomy term; we'll need it in case we want
    // a flat taxonomy menu.
    if (is_object($term)) {
      $term_parents = taxonomy_get_parents_all($term->tid);
      $root_term_weight = $term_parents ? $term_parents[count($term_parents) - 1]->weight : 0;
    }
    else {
      $root_term_weight = 0;
    }
    foreach ($term->parents as $parent) {
      $ptid = $parent;

      // Turn the term into the correct $item array form.
      $item = array(
        'term' => $term,
        'tid' => $term->tid,
        'name' => $term->name,
        'description' => variable_get(_taxonomy_menu_build_variable('term_item_description', $term->vid), 0) ? $term->description : '',
        'weight' => !empty($term->weight) ? $term->weight : 0,
        'vid' => $term->vid,
        'ptid' => $ptid,
        'root_term_weight' => $root_term_weight,
        'menu_name' => $args['menu_name'],
        'language' => isset($term->language) ? $term->language : ($node ? $node->language : $GLOBALS['language']->language),
      );
      if (isset($args['mlid'])) {
        $item['mlid'] = $args['mlid'];
      }

      // Mutiple parents are not supported yet. Without the break, the item is
      // inserted multiple under one parent, instead of once under each parent.
      break;
    }
  }
  return $item;
}