You are here

function taxonomy_hierarchical_select_form_alter in Hierarchical Select 5

Same name and namespace in other branches
  1. 5.2 modules/taxonomy.inc \taxonomy_hierarchical_select_form_alter()

Implementation of hook_hierarchical_select_form_alter().

File

modules/taxonomy.inc, line 9

Code

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,
            ),
          );
        }
      }
    }
  }
}