You are here

function taxonomy_menu_menu_link_prepare in Taxonomy menu 7.2

Same name and namespace in other branches
  1. 8 taxonomy_menu.module \taxonomy_menu_menu_link_prepare()

Prepares a taxonomy item to be saved as a menu link.

A menu item has the following properties:

  • link_path: (required)
  • link_title: (required)
  • router_path: (required)
  • menu_name: (optional)
  • weight: (optional)
  • expanded: (optional)
  • options: (optional)
  • mlid: (optional)
  • plid: (optional)

Parameters

$term: A taxonomy term used to save a respective menu item.

$menu_name: The machine name of the menu in which the menu link should be saved.

Return value

A menu link built upon a taxonomy term, to be saved in the menu.

1 call to taxonomy_menu_menu_link_prepare()
taxonomy_menu_menu_link_save in ./taxonomy_menu.module
Saves a menu link in a menu, based on a taxonomy term.

File

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

Code

function taxonomy_menu_menu_link_prepare($term, $menu_name) {
  static $weight = 0;
  $langcode = isset($term->language) ? $term->language : LANGUAGE_NONE;
  $recursive_count = FALSE;

  // Count nodes attached to a taxonomy term if the settings require it.
  // TODO Make the recursivity of node count optional.
  $display_count = taxonomy_menu_variable_get('display_num', $term->vid, FALSE);
  $hide_term = taxonomy_menu_variable_get('hide_empty_terms', $term->vid, FALSE);
  if ($hide_term || $display_count) {
    $nodes_count = taxonomy_menu_term_count_nodes($term->tid, $recursive_count);
    $is_hidden = $hide_term && (!$nodes_count || $nodes_count == 0) ? 1 : 0;
  }

  // Load or create a menu link corresponding the taxonomy term being processed.
  $menu_link = taxonomy_menu_menu_link_load($term, $langcode);

  // Menu to be attached to.
  $menu_link['menu_name'] = $menu_name;

  // Expanded.
  $menu_link['expanded'] = taxonomy_menu_variable_get('expanded', $term->vid, 0);

  // Has children.
  $has_children = taxonomy_get_children($term->tid, $term->vid);
  $menu_link['has_children'] = empty($has_children) ? 0 : 1;

  // Flatten.
  $flatten_menu = taxonomy_menu_variable_get('flat', $term->vid, 0);
  if ($flatten_menu) {
    $menu_link['weight'] = $weight++;
    $menu_link['has_children'] = 0;
    $menu_link['plid'] = taxonomy_menu_variable_get('vocab_parent', $term->vid, NULL);
    $menu_link['expanded'] = 0;
  }
  else {
    $menu_link['weight'] = $term->weight;
    $menu_link['plid'] = taxonomy_menu_term_get_plid($term, $langcode);
  }

  // Empty terms.
  $menu_link['hidden'] = isset($is_hidden) ? $is_hidden : $menu_link['hidden'];

  // Menu link title.
  $menu_link['link_title'] = $term->name;
  if ($display_count && $nodes_count > 0) {
    $menu_link['link_title'] .= " (" . $nodes_count . ")";
  }

  // HTML title attribute.
  if (taxonomy_menu_variable_get('display_title_attr', $term->vid, TRUE)) {
    $term_description = taxonomy_menu_variable_get('term_item_description', $term->vid, 0);
  }
  $menu_link['options']['attributes']['title'] = isset($term_description) && $term_description == 1 ? trim($term->description) : '';

  // Path.
  $link_path = taxonomy_menu_path_get($term);
  $menu_link['link_path'] = drupal_get_normal_path($link_path, $langcode);
  return $menu_link;
}