You are here

function taxonomy_menu_menu_link_prepare in Taxonomy menu 8

Same name and namespace in other branches
  1. 7.2 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 142
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()->id) ? $term
    ->language()->id : Language::LANGCODE_NOT_SPECIFIED;
  $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
    ->bundle(), FALSE);
  $hide_term = taxonomy_menu_variable_get('hide_empty_terms', $term
    ->bundle(), FALSE);
  if ($hide_term || $display_count) {
    $nodes_count = taxonomy_menu_term_count_nodes($term
      ->id(), $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_existing_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
    ->bundle(), 0);

  // Has children.
  $has_children = taxonomy_term_load_children($term
    ->id(), $term
    ->bundle());
  $menu_link['has_children'] = empty($has_children) ? 0 : 1;

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

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

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

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

  // Path.
  $link_path = taxonomy_menu_path_get($term);
  $menu_link['link_path'] = \Drupal::service('path.alias_manager.cached')
    ->getSystemPath($link_path, $langcode);
  return $menu_link;
}