You are here

function taxonomy_menu_translated_menu_link_alter in Taxonomy menu 7

Same name and namespace in other branches
  1. 6.3 taxonomy_menu.module \taxonomy_menu_translated_menu_link_alter()
  2. 6.2 taxonomy_menu.module \taxonomy_menu_translated_menu_link_alter()
  3. 7.2 taxonomy_menu.module \taxonomy_menu_translated_menu_link_alter()

Implements hook_translated_menu_link_alter().

Translate menu links on the fly by using term translations.

File

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

Code

function taxonomy_menu_translated_menu_link_alter(&$item, $map) {
  if (module_exists('i18n_taxonomy')) {

    // In case of localized terms, use term translation for menu title.
    if ($item['module'] == 'taxonomy_menu') {
      $t = _taxonomy_menu_get_item($item['mlid']);

      // Only translate when term exist (may occur with stray menu item).
      if ($t) {

        // Only translate when translation mode is set to localize.
        if (i18n_taxonomy_vocabulary_mode($t->vid, I18N_MODE_LOCALIZE)) {

          // This is a term.
          if ($t->tid > 0) {
            $term = taxonomy_term_load($t->tid);
            $display_num = '';
            $menu_num_inc_children = variable_get(_taxonomy_menu_build_variable('display_num_incl_children', $term->vid), TRUE);
            $menu_display_descendants = variable_get(_taxonomy_menu_build_variable('display_descendants', $t->vid), FALSE);
            if ($menu_num_inc_children || $menu_display_descendants) {
              $num = taxonomy_menu_term_count_nodes($t->tid, $t->vid);
            }
            else {
              $num = _taxonomy_menu_term_count($t->tid);
            }

            // If hide menu is selected and the term count is 0 and the term
            // has no children then do not create the menu item.
            if ($num == 0 && variable_get(_taxonomy_menu_build_variable('hide_empty_terms', $t->vid), FALSE) && !_taxonomy_menu_children_has_nodes($t->tid, $t->vid)) {
              $display_num = '';
            }
            elseif (variable_get(_taxonomy_menu_build_variable('display_num', $t->vid), FALSE)) {
              $display_num = " ({$num})";
            }
            if ($item['title'] != $term->name . $display_num) {

              // Should not happen.
              watchdog('error', 'Menu and taxonomy name mismatch: @title != @name', array(
                '@title' => $item['title'],
                '@name' => $term->name . $display_num,
              ));
            }
            $term = i18n_taxonomy_localize_terms($term);
            $item['title'] = $item['link_title'] = $term->name . $display_num;
            if (variable_get(_taxonomy_menu_build_variable('term_item_description', $t->vid), FALSE)) {
              $item['options']['attributes']['title'] = $term->description;
            }
          }
          else {
            $vocab = taxonomy_vocabulary_load($t->vid);
            $item['title'] = i18n_string('taxonomy:vocabulary:' . $vocab->vid . ':name', $vocab->name);
          }
        }
      }
      else {
        watchdog('taxonomy_menu', 'Error with menu entry "%me" in menu "%mt"', array(
          '%me' => $item['title'],
          '%mt' => $item['menu_name'],
        ));
      }
    }
  }
}