You are here

taxonomy_menu.module in Taxonomy menu 6.3

It Generates menu links for all selected taxonomy terms

File

taxonomy_menu.module
View source
<?php

/**
 * @file
 * It Generates menu links for all selected taxonomy terms
 */

//include the database layer
require_once drupal_get_path('module', 'taxonomy_menu') . '/taxonomy_menu.database.inc';

/**
 * Implementation of hook_perm().
 */
function taxonomy_menu_perm() {
  return array(
    'taxonomy menu admin',
  );
}

/**
 * Implementation of hook_menu().
 */
function taxonomy_menu_menu() {
  $base = 'admin/build/taxonomy_menu';
  $admin_file = 'taxonomy_menu.admin.inc';
  $items[$base] = array(
    'title' => 'Taxonomy Menu',
    'type' => MENU_NORMAL_ITEM,
    'access arguments' => array(
      'taxonomy menu admin',
    ),
    'page callback' => 'taxonomy_menu_group_list',
    'file' => $admin_file,
  );
  $items["{$base}/group"] = array(
    'title' => 'Menu Groups',
    'type' => MENU_DEFAULT_LOCAL_TASK,
  );
  $items["{$base}/termset"] = array(
    'title' => 'Term Sets',
    'type' => MENU_LOCAL_TASK,
    'page callback' => 'taxonomy_menu_term_sets',
    'weight' => 3,
    'access arguments' => array(
      'taxonomy menu admin',
    ),
    'file' => $admin_file,
  );
  $items["{$base}/group/list"] = array(
    'title' => 'Menu Groups',
    'type' => MENU_DEFAULT_LOCAL_TASK,
    'weight' => 0,
  );
  $items["{$base}/group/add"] = array(
    'title' => 'Add Menu Group',
    'type' => MENU_LOCAL_TASK,
    'page callback' => 'drupal_get_form',
    'page arguments' => array(
      'taxonomy_menu_group_form',
    ),
    'weight' => 1,
    'access arguments' => array(
      'taxonomy menu admin',
    ),
    'file' => $admin_file,
  );
  $items["{$base}/group/%/edit"] = array(
    'type' => MENU_CALLBACK,
    'title' => 'Taxonomy Menu Group',
    'access arguments' => array(
      'taxonomy menu admin',
    ),
    'page callback' => 'drupal_get_form',
    'page arguments' => array(
      'taxonomy_menu_group_form',
      4,
    ),
    'file' => $admin_file,
  );
  $items["{$base}/group/%/remove"] = array(
    'type' => MENU_CALLBACK,
    'title' => 'Remove Term Set',
    'access arguments' => array(
      'taxonomy menu admin',
    ),
    'page callback' => 'taxonomy_menu_group_remove_term_set',
    'page arguments' => array(
      4,
    ),
    'file' => $admin_file,
  );
  $items["{$base}/group/%/delete"] = array(
    'type' => MENU_CALLBACK,
    'title' => t('Delete Menu Group'),
    'weight' => 10,
    'page arguments' => array(
      'taxonomy_menu_group_delete_confirm',
      4,
    ),
    'page callback' => 'drupal_get_form',
    'file' => $admin_file,
    'access arguments' => array(
      'taxonomy menu admin',
    ),
  );
  $items["{$base}/termset/list"] = array(
    'title' => 'Term Sets',
    'type' => MENU_DEFAULT_LOCAL_TASK,
    'weight' => 0,
  );
  $items["{$base}/termset/add"] = array(
    'title' => 'Add Term Set',
    'type' => MENU_LOCAL_TASK,
    'page callback' => 'taxonomy_menu_term_set_add',
    'file' => $admin_file,
    'weight' => 5,
    'access arguments' => array(
      'taxonomy menu admin',
    ),
  );
  $items["{$base}/termset/%/edit"] = array(
    'title' => 'Edit Term Set',
    'type' => MENU_CALLBACK,
    'page arguments' => array(
      4,
    ),
    'page callback' => 'taxonomy_menu_term_set_add',
    'access arguments' => array(
      'taxonomy menu admin',
    ),
    'file' => $admin_file,
  );
  $items["{$base}/termset/%/delete"] = array(
    'title' => 'Delete Term Set',
    'type' => MENU_CALLBACK,
    'page callback' => 'drupal_get_form',
    'page arguments' => array(
      'taxonomy_menu_term_set_delete_confirm',
      4,
    ),
    'access arguments' => array(
      'taxonomy menu admin',
    ),
    'file' => $admin_file,
  );
  $items['taxonomy_menu/ahah/tids'] = array(
    'type' => MENU_NORMAL_ITEM,
    'access arguments' => array(
      'taxonomy menu admin',
    ),
    'file' => $admin_file,
    'page callback' => 'taxonomy_menu_ahah_tids',
  );
  $items['taxonomy_menu/ahah/path'] = array(
    'type' => MENU_NORMAL_ITEM,
    'access arguments' => array(
      'taxonomy menu admin',
    ),
    'file' => $admin_file,
    'page callback' => 'taxonomy_menu_ahah_path',
  );
  $items['taxonomy_menu/ahah/term_sets'] = array(
    'type' => MENU_NORMAL_ITEM,
    'access arguments' => array(
      'taxonomy menu admin',
    ),
    'file' => $admin_file,
    'page callback' => 'taxonomy_menu_ahah_term_sets',
  );
  $items['taxonomy_menu/ahah/term_sets_remove'] = array(
    'type' => MENU_NORMAL_ITEM,
    'access arguments' => array(
      'taxonomy menu admin',
    ),
    'file' => $admin_file,
    'page callback' => 'taxonomy_menu_ahah_term_set_remove',
  );
  return $items;
}

/**
 * PATH API FUNCTIONS
 */

/**
 * Create the path to use in the menu item
 *
 * @return array
 *  path selections
 */
function taxonomy_menu_get_paths() {
  return module_invoke_all('taxonomy_menu_path');
}

/**
 * Create the path for the vid/tid combination.
 *
 * @param $vid
 * @param $tid
 * @return string
 *  path
 */
function taxonomy_menu_create_path($vid, $tid) {

  //get the path function for this vocabulary
  $function = variable_get('taxonomy_menu_path_' . $vid, 'taxonomy_menu_path_default');

  //run the function
  return $function($vid, $tid);
}

/**
 * hook_taxonomy_menu_path.  Invoked from _taxonomy_menu_get_paths.
 *
 * @return array
 *  function name => Display Title
 *  a list of the path options.
 */
function taxonomy_menu_taxonomy_menu_path() {
  $output = array(
    'taxonomy_menu_path_default' => t('Default'),
  );
  return $output;
}

/**
 * Callback for hook_taxonomy_menu_path
 */
function taxonomy_menu_path_default($vid, $tid) {

  //if tid = 0 then we are creating the vocab menu item format will be taxonomy/term/$tid+$tid+$tid....
  if ($tid == 0) {

    //get all of the terms for the vocab
    $vtids = _taxonomy_menu_get_terms($vid);
    $end = implode(' ', $vtids);
    $path = "taxonomy/term/{$end}";
  }
  else {
    $path = taxonomy_term_path(taxonomy_get_term($tid));
    if (variable_get('taxonomy_menu_display_descendants_' . $vid, FALSE)) {

      //Use 'all' at the end of the path
      if (variable_get('taxonomy_menu_end_all_' . $vid, FALSE)) {
        $path .= '/all';
      }
      else {

        //we wait to run this instead of durning the if above

        //because we only wan to run it once.
        $terms = taxonomy_get_tree($vid, $tid);
        foreach ($terms as $term) {
          $tids[] = $term->tid;
        }
        if ($tids) {
          $end = implode(' ', $tids);
          $path .= ' ' . $end;
        }
      }
    }
  }
  return $path;
}

/**
 * Used to create a form array of taxonomy menu options
 * invokes hook_taxonomy_menu_options().
 *
 * @param $op
 *  type of option
 *  path/group/item/global
 *
 * @return $form array
 */
function taxonomy_menu_get_options($op, $type_key) {
  $options = module_invoke_all('taxonomy_menu_options');

  //cycle through field
  foreach ($options as $field_name => $field_elements) {
    if ($op == $field_elements['option_type']) {

      // If the option type if path, then check the path option
      if ($op == 'PATH') {

        // set the default path type
        $type_key = $type_key ? $type_key : 'taxonomy_menu_path_default';
        if ($field_elements['taxonomy_menu_path'] == $type_key) {
          _taxonomy_menu_get_options($options, $field_name, $op, $type_key);
        }
        else {
          unset($options[$field_name]);
        }
      }
      else {
        if ($op != 'PATH') {
          _taxonomy_menu_get_options($options, $field_name, $op, $type_key);
        }
        else {
          unset($options[$field_name]);
        }
      }
    }
    else {

      //unset the options that are not part of the selected op
      unset($options[$field_name]);
    }
  }

  //set the option feildset values
  $options['#type'] = 'fieldset';
  $options['#title'] = t('Options');
  $options['#collapsible'] = TRUE;
  $options['#tree'] = TRUE;
  return $options;
}

/**
 * Helper Functoin for taxonomy_menu_get_options
 */
function _taxonomy_menu_get_options(&$options, $field_name, $op, $type_key) {

  //if the variable is set then use that, if the default key is set then use that, otherwise use false
  $options[$field_name]['#default_value'] = taxonomy_menu_get_option($field_name, $op, !empty($options[$field_name]['default']) ? $options[$field_name]['default'] : FALSE, $type_key);

  //set the type to checkbox if it is empty
  if (empty($options[$field_name]['#type'])) {
    $options[$field_name]['#type'] = 'checkbox';
  }

  //remove the default and option type values from the array so we don't pass it to the form
  unset($options[$field_name]['default']);
  unset($options[$field_name]['option_type']);
  unset($options[$field_name]['taxonomy_menu_path']);
}

/**
 * Implementation of hook_taxonomy_menu_options().
 *
 * @return array
 *  Uses the value to set the variable taxonomy_menu_<value>_$vid
 *  $options[value]
 *   default - optional.  this is what will be used if the varialbe is not set.  if empty then FALSE is used
 *   #title - required.
 *   option_type - GROUP/TERM_SET/PATH
 *   taxonomy_menu_path - if path is the option_type then this is required and is callback function
 *   any other form element
 */
function taxonomy_menu_taxonomy_menu_options() {
  $options['display_num'] = array(
    '#title' => t('Display Number of Nodes'),
    '#description' => t('Display the number of Items per taxonomy Terms.  Will not show up for vocabulary menu items.'),
    'default' => FALSE,
    'option_type' => 'TERM_SET',
  );
  $options['hide_empty_terms'] = array(
    '#title' => t('Hide Empty Terms'),
    '#description' => t('Hide terms with no nodes attached to them.'),
    'default' => FALSE,
    'option_type' => 'GROUP',
  );
  $options['voc_item'] = array(
    '#title' => t('Item for Vocabulary'),
    '#description' => t("Shall the vocabulary have it's own item."),
    'default' => TRUE,
    'option_type' => 'TERM_SET',
  );
  $options['expanded'] = array(
    '#title' => t('Auto Expand Menu Item'),
    '#description' => t('Sets the expand setting to TRUE'),
    'default' => TRUE,
    'option_type' => 'GROUP',
  );
  $options['display_descendants'] = array(
    '#title' => t('Display Descendants'),
    'default' => FALSE,
    'option_type' => 'TERM_SET',
  );
  $options['end_all'] = array(
    '#title' => t("Use 'all' at the end of URL"),
    'default' => TRUE,
    '#description' => t('This changes tid+tid+tid to "All" in term when Dispaly Decendants is selected. <br />
    Only used if Menu Path Type is "Default path". <br />
    Works with default taxonomy page.'),
    'option_type' => 'PATH',
    'taxonomy_menu_path' => 'taxonomy_menu_path_default',
  );
  return $options;
}

/**
 * Implementation of hook_translated_menu_link_alter().
 *
 * Translate menu links on the fly by using term translations.
 *
 */
function taxonomy_menu_translated_menu_link_alter(&$item, $map) {
  if (module_exists('i18ntaxonomy')) {

    // in case of localized terms, use term translation for menu title
    if ($item['module'] == 'taxonomy_menu') {

      // TODO: check vocabulary translation mode before tryring to translate: but is this really usefull ?
      //  if (i18ntaxonomy_vocabulary($vid) == I18N_TAXONOMY_LOCALIZE) {
      $t = _taxonomy_menu_get_item($item['mlid']);
      if ($t['tid'] > 0) {

        // this is a term
        $term = taxonomy_get_term($t['tid']);
        $display_num = '';
        $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_hide_empty_terms_' . $t['vid'], FALSE) && _taxonomy_menu_children_has_nodes($t['tid'], $t['vid'])) {
          $display_num = '';
        }
        else {
          if (variable_get('taxonomy_menu_display_num_' . $t['vid'], FALSE)) {

            // if number > 0 and display decendants, then count all of the children
            if (variable_get('taxonomy_menu_display_descendants_' . $t['vid'], FALSE)) {
              $num = taxonomy_term_count_nodes($t['tid']);
            }
            $display_num = " ({$num})";
          }
        }
        if ($item['title'] != $term->name . $display_num) {

          // Should not happen
          drupal_set_message(t('Menu and taxonomy name mismatch : @title != @name', array(
            '@title' => $item['title'],
            '@name' => $term->name . $display_num,
          )), 'error');
        }
        $item['title'] = tt('taxonomy:term:' . $term->tid . ':name', $term->name) . $display_num;
      }
      else {

        // is a vocabulary
        $vocab = taxonomy_vocabulary_load($t['vid']);
        $item['title'] = tt('taxonomy:vocabulary:' . $vocab->vid . ':name', $vocab->name);
      }
    }
  }
}

/**
 * THEME FUNCTIONS
 */

/**
 * Implementation of hook_theme()
 */
function taxonomy_menu_theme() {
  return array(
    'taxonomy_menu_term_set_table' => array(
      'arguments' => array(
        'form',
      ),
    ),
    'taxonomy_menu_group_term_set_add' => array(
      'arguments' => array(
        'form',
      ),
    ),
  );
}

/**
 * Theme function for the list of term sets
 */
function theme_taxonomy_menu_term_set_table($form) {
  $rows = array();
  $output = '';
  $headers = array(
    t('Term Set Name'),
    t('Vocabulary Name'),
    t('Parent Term Set'),
  );
  if ($form['mgid']['#value'] && isset($form['list'])) {
    foreach (element_children($form['list']) as $key) {
      $row = array();
      $row[] = array(
        'data' => drupal_render($form['list'][$key]['name']),
      );
      $row[] = array(
        'data' => drupal_render($form['list'][$key]['vocab']),
      );
      $row[] = array(
        'data' => drupal_render($form['list'][$key]['parent']),
      );
      $row[] = array(
        'data' => drupal_render($form['list'][$key]['remove']),
      );

      //$row[] = array('data' => l(t('Remove'), 'admin/build/taxonomy_menu/group/'.

      //$form['mgid']['#value'] .'/remove/'. $key));
      $rows[] = $row;
    }
  }
  else {
    $rows[] = array(
      t('Please save the menu group before associating a term sets'),
    );
  }
  $output = theme('table', $headers, $rows);
  $output .= drupal_render($form['term_set_add']);
  return $output;
}

/**
 * Theme function for term set associate from on menu group form
 */
function theme_taxonomy_menu_group_term_set_add($form) {
  $output = '';
  if ($form['term_set']['#options']) {
    $output .= '<div class="container-inline">';
    $output .= drupal_render($form['term_set']);
    $output .= drupal_render($form['term_set_parent']);
    $output .= drupal_render($form['term_set_more']);
    $output .= '</div>';
  }
  else {
    $output = t('There are no term sets to associate with this menu group.');
  }
  return $output;
}

Functions

Namesort descending Description
taxonomy_menu_create_path Create the path for the vid/tid combination.
taxonomy_menu_get_options Used to create a form array of taxonomy menu options invokes hook_taxonomy_menu_options().
taxonomy_menu_get_paths Create the path to use in the menu item
taxonomy_menu_menu Implementation of hook_menu().
taxonomy_menu_path_default Callback for hook_taxonomy_menu_path
taxonomy_menu_perm Implementation of hook_perm().
taxonomy_menu_taxonomy_menu_options Implementation of hook_taxonomy_menu_options().
taxonomy_menu_taxonomy_menu_path hook_taxonomy_menu_path. Invoked from _taxonomy_menu_get_paths.
taxonomy_menu_theme Implementation of hook_theme()
taxonomy_menu_translated_menu_link_alter Implementation of hook_translated_menu_link_alter().
theme_taxonomy_menu_group_term_set_add Theme function for term set associate from on menu group form
theme_taxonomy_menu_term_set_table Theme function for the list of term sets
_taxonomy_menu_get_options Helper Functoin for taxonomy_menu_get_options