You are here

taxonomy.inc in Hierarchical Select 5

Same filename and directory in other branches
  1. 5.2 modules/taxonomy.inc

File

modules/taxonomy.inc
View source
<?php

//----------------------------------------------------------------------------

// Hierarchical Select hooks.

/**
 * Implementation of hook_hierarchical_select_form_alter().
 */
function taxonomy_hierarchical_select_form_alter($form_id, &$form) {

  // Add per-vocabulary settings for Hierarchical Select.
  if ($form_id == 'taxonomy_form_vocabulary') {
    $vid = $form['vid']['#value'];
    $first_part = array_slice($form, 0, 9);
    $second_part = array_slice($form, 10);
    $form = $first_part;
    $form['hierarchical_select'] = array(
      '#tree' => TRUE,
      '#type' => 'fieldset',
      '#title' => t('Hierarchical Select settings'),
    );
    $form['hierarchical_select']['hierarchical_select_status'] = array(
      '#type' => 'checkbox',
      '#title' => t('Use the Hierarchical Select form element for this vocabulary'),
      '#default_value' => variable_get("hierarchical_select_status_{$vid}", FALSE),
    );
    $form['hierarchical_select']['hierarchical_select_lineage'] = array(
      '#type' => 'select',
      '#title' => t('Lineage'),
      '#options' => array(
        TRUE => t('Save term lineage'),
        FALSE => t('Save only the deepest term'),
      ),
      '#default_value' => variable_get("hierarchical_select_lineage_{$vid}", FALSE),
      '#description' => t('Saving the term lineage means saving the entire the term itself and
        all its ancestors. This is recommended if the vocabulary has the
        multiple parents options enabled.'),
    );
    $form['#submit']['taxonomy_form_vocabulary_submit'] = array();

    // Make sure the original submit handler is still called.
    $form['#submit']['hierarchical_select_taxonomy_form_vocabulary_submit'] = array();
    $form += $second_part;
  }

  // Change the exposed filters of Views. Only affects hierarchical vocabulary
  // filters.
  if ($form_id == 'views_filters') {

    // Find the ids and vocabulary ids of the exposed filters.
    $filters = array();
    foreach ($form['view']['#value']->exposed_filter as $id => $filter) {
      if (preg_match("/term_node_(\\d+)\\.tid/", $filter['field'], $matches)) {
        $vid = $matches[1];

        // Only apply Hierarchical Select if it's enabled for this vocabulary.
        if (variable_get("hierarchical_select_status_{$vid}", FALSE)) {
          $vocabulary = taxonomy_get_vocabulary($vid);

          // Hierarchical Select only makes sense if there's a hierarchy.
          if ($vocabulary->hierarchy > 0) {

            // Remove the "operator" select form item.
            unset($form["op{$id}"]);

            // Enforce the "Force single" option.
            $form['view']['#value']->exposed_filter[$id]['single'] = TRUE;
            $form['#parameters']->exposed_filter[$id]['single'] = TRUE;

            // Make it use a hierarchical select.
            $form["filter{$id}"]['#type'] = 'hierarchical_select';
            $form["filter{$id}"]['#hierarchical_select_settings'] = array(
              'lineage' => variable_get("hierarchical_select_lineage_{$vid}", FALSE),
              'module' => 'taxonomy',
              'params' => array(
                'vid' => $vid,
              ),
            );

            // Put the altered exposed filters in a separate table row.
            _hierarchical_select_views_exposed_filters_reposition();
          }
        }
      }
    }
  }

  // Change the term selection of nodes. Only affects hierarchical
  // vocabularies.
  if (isset($form['type']) && $form['type']['#value'] . '_node_form' == $form_id && is_array($form['taxonomy'])) {
    foreach ($form['taxonomy'] as $vid => $form_item) {

      // Only apply Hierarchical Select if it's enabled for this vocabulary.
      if (variable_get("hierarchical_select_status_{$vid}", FALSE)) {
        $vocabulary = taxonomy_get_vocabulary($vid);

        // Hierarchical Select only makes sense if there's a hierarchy.
        if ($vocabulary->hierarchy > 0) {
          $form['taxonomy'][$vid]['#type'] = 'hierarchical_select';
          $form['taxonomy'][$vid]['#hierarchical_select_settings'] = array(
            'lineage' => variable_get("hierarchical_select_lineage_{$vid}", FALSE),
            'module' => 'taxonomy',
            'params' => array(
              'vid' => $vid,
            ),
          );
        }
      }
    }
  }
}

/**
 * Implementation of hook_hierarchical_select_params().
 */
function taxonomy_hierarchical_select_params() {
  $params = array(
    'vid',
  );
  return $params;
}

/**
 * Implementation of hook_hierarchical_select_root_level().
 */
function taxonomy_hierarchical_select_root_level($params) {
  $terms = taxonomy_get_tree($params['vid'], 0, -1, 1);
  return _taxonomy_hierarchical_select_terms_to_options($terms);
}

/**
 * Implementation of hook_hierarchical_select_children().
 */
function taxonomy_hierarchical_select_children($parent, $params) {
  $terms = taxonomy_get_children($parent, $params['vid']);
  return _taxonomy_hierarchical_select_terms_to_options($terms);
}

/**
 * Implementation of hook_hierarchical_select_lineage().
 */
function taxonomy_hierarchical_select_lineage($item) {
  if (!is_numeric($item) || $item == -1) {
    $lineage = array();
  }
  else {
    $terms = array_reverse(taxonomy_get_parents_all($item));
    foreach ($terms as $term) {
      $lineage[] = $term->tid;
    }
  }
  return $lineage;
}

/**
 * Implementation of hook_hierarchical_select_valid_item().
 */
function taxonomy_hierarchical_select_valid_item($item, $params) {
  $term = taxonomy_get_term($item);
  return $term->vid == $params['vid'];
}

//----------------------------------------------------------------------------

// FAPI callbacks.

/**
 * Additional submit handler for the taxonomy_form_vocabulary form.
 */
function hierarchical_select_taxonomy_form_vocabulary_submit($form_id, $form_values) {
  $vid = $form_values['vid'];
  foreach ($form_values['hierarchical_select'] as $setting => $value) {
    variable_set("{$setting}_{$vid}", $value);
  }
}

//----------------------------------------------------------------------------

// Private functions.

/**
 * Helper function to transform an array of terms into an associative array of
 * options, for use in a select form item.
 *
 * @param $terms
 *  An array of term objects.
 * @return
 *  An associative array of options, keys are tids, values are term names.
 */
function _taxonomy_hierarchical_select_terms_to_options($terms) {
  $options = array();
  foreach ($terms as $key => $term) {
    $options[$term->tid] = $term->name;
  }
  return $options;
}

Functions

Namesort descending Description
hierarchical_select_taxonomy_form_vocabulary_submit Additional submit handler for the taxonomy_form_vocabulary form.
taxonomy_hierarchical_select_children Implementation of hook_hierarchical_select_children().
taxonomy_hierarchical_select_form_alter Implementation of hook_hierarchical_select_form_alter().
taxonomy_hierarchical_select_lineage Implementation of hook_hierarchical_select_lineage().
taxonomy_hierarchical_select_params Implementation of hook_hierarchical_select_params().
taxonomy_hierarchical_select_root_level Implementation of hook_hierarchical_select_root_level().
taxonomy_hierarchical_select_valid_item Implementation of hook_hierarchical_select_valid_item().
_taxonomy_hierarchical_select_terms_to_options Helper function to transform an array of terms into an associative array of options, for use in a select form item.