You are here

taxonomy_menu_custom_paths.module in Taxonomy menu 7.2

Same filename and directory in other branches
  1. 8 taxonomy_menu_custom_paths/taxonomy_menu_custom_paths.module

taxonomy_menu_custom_paths module

File

taxonomy_menu_custom_paths/taxonomy_menu_custom_paths.module
View source
<?php

/**
 * @file
 * taxonomy_menu_custom_paths module
 */

/**
 * Implements hook_taxonomy_menu_vocabulary_settings().
 */
function taxonomy_menu_custom_paths_taxonomy_menu_vocabulary_settings() {
  $defaults = array(
    'custom_path_base' => '',
    'custom_path_depth' => '',
  );
  return $defaults;
}

/**
 * Implements hook_form_FORM_ID_alter().
 *
 * Adds Taxonomy menu custom path settings on a per-vocabulary basis.
 *
 * @see taxonomy_menu_custom_paths_vocabulary_validate()
 */
function taxonomy_menu_custom_paths_form_taxonomy_form_vocabulary_alter(&$form, &$form_state) {
  $vid = isset($form['vid']) && $form['vid']['#value'] ? $form['vid']['#value'] : 0;
  $form['taxonomy_menu']['options_custom_path'] = array(
    '#type' => 'fieldset',
    '#title' => t('Custom path options'),
    '#collapsible' => TRUE,
    '#collapsed' => TRUE,
    '#description' => t("<strong>Warning:</strong> These settings uses custom paths that are not registered in Drupal by default. You need to register them <strong>before</strong> you create the taxonomy menu. For example, you could use a view in order to do that."),
    '#states' => array(
      'visible' => array(
        ':input[name="taxonomy_menu[path]"]' => array(
          'value' => 'taxonomy_menu_path_custom',
        ),
      ),
    ),
  );
  $form['taxonomy_menu']['options_custom_path']['custom_path_base'] = array(
    '#title' => t('Base path'),
    '#weight' => 0,
    '#type' => 'textfield',
    '#default_value' => taxonomy_menu_variable_get('custom_path_base', $vid, ''),
    '#description' => t("Path 'base_path/%' must be available, where the argument is a Term ID (tid)."),
  );
  $form['taxonomy_menu']['options_custom_path']['custom_path_depth'] = array(
    '#title' => t('Depth modifier'),
    '#weight' => 1,
    '#description' => t("Path 'base_path/%/%' must be available, where the arguments are respectively a Term ID (tid) and a Depth Modifier."),
    '#default_value' => taxonomy_menu_variable_get('custom_path_depth', $vid, ''),
    '#type' => 'textfield',
  );

  // Add a custom validation function.
  array_unshift($form['#validate'], 'taxonomy_menu_custom_paths_vocabulary_validate');
}

/**
 * Validation handler for settings of custom path in the taxonomy vocabulary.
 *
 * We make sure that a base path is provided and paths are registered.
 *
 * @see taxonomy_form_vocabulary()
 */
function taxonomy_menu_custom_paths_vocabulary_validate($form, $form_state) {
  $options = $form_state['values']['taxonomy_menu'];
  $base = $options['options_custom_path']['custom_path_base'];
  $depth = $options['options_custom_path']['custom_path_depth'];

  // Don't allow base path value to be empty when a custom path is selected.
  // Check that the required path are registered in Drupal before processing.
  if ($options['path'] == 'taxonomy_menu_path_custom') {
    if (empty($base)) {
      form_set_error('custom_path_base', 'A base path must be provided when using a custom path.');
    }
    elseif (empty($depth)) {
      $path = drupal_get_normal_path($base . '/%');
      if (!_taxonomy_menu_valid_path($path)) {
        form_set_error('custom_path_base', 'The path ' . $path . ' is not available in Drupal. This is required to use custom paths.');
      }
    }
    else {
      $path = drupal_get_normal_path($base . '/%/%');
      if (!_taxonomy_menu_valid_path($path)) {
        form_set_error('custom_path_depth', 'The path ' . $path . ' is not available in Drupal. This is required to use custom paths.');
      }
    }
  }
}

/**
 * Implements hook_taxonomy_menu_path().
 */
function taxonomy_menu_custom_paths_taxonomy_menu_path() {
  $output = array(
    'taxonomy_menu_path_custom' => t('Custom (<base_path>/%)'),
  );
  return $output;
}

/**
 * Callback for hook_taxonomy_menu_path.
 */
function taxonomy_menu_path_custom($term) {
  $path = '';
  $tids = array();
  $base_path = taxonomy_menu_variable_get('custom_path_base', $term->vid, '');
  $depth = taxonomy_menu_variable_get('custom_path_depth', $term->vid, '');

  // When tid equals 0, we are dealing with a vocabulary item. We want the path
  // to be a mulitple term path.
  if ($term->tid == 0) {
    $tids = _taxonomy_menu_get_tids($term->vid);
  }
  else {
    $tids[] = $term->tid;
    $terms = taxonomy_get_tree($term->vid, $term->tid);
    foreach ($terms as $term) {
      $tids[] = $term->tid;
    }
  }

  // Build the path.
  if ($tids) {
    $path = $base_path . '/' . implode('+', $tids);
  }
  else {
    $path = $base_path . '/' . $term->tid;
  }
  if ($depth != '') {
    $path .= '/' . $depth;
  }
  return $path;
}

Functions

Namesort descending Description
taxonomy_menu_custom_paths_form_taxonomy_form_vocabulary_alter Implements hook_form_FORM_ID_alter().
taxonomy_menu_custom_paths_taxonomy_menu_path Implements hook_taxonomy_menu_path().
taxonomy_menu_custom_paths_taxonomy_menu_vocabulary_settings Implements hook_taxonomy_menu_vocabulary_settings().
taxonomy_menu_custom_paths_vocabulary_validate Validation handler for settings of custom path in the taxonomy vocabulary.
taxonomy_menu_path_custom Callback for hook_taxonomy_menu_path.