You are here

function hs_taxonomy_views_form_alter in Hierarchical Select 5.3

Same name and namespace in other branches
  1. 6.3 modules/hs_taxonomy_views.module \hs_taxonomy_views_form_alter()

Implementation of hook_form_alter().

File

modules/hs_taxonomy_views.module, line 38
Implementation of the Hierarchical Select API for the Taxonomy module's Views exposed filters.

Code

function hs_taxonomy_views_form_alter($form_id, &$form) {

  // Change the exposed filters of Views. Only affects hierarchical vocabulary
  // filters.
  if (in_array($form_id, array(
    'views_filters',
    'views_filterblock',
  ))) {
    $hs_exposed_filters_found = 0;

    // Find the ids and vocabulary ids of the exposed filters.
    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("taxonomy_hierarchical_select_{$vid}", 0)) {
          $hs_exposed_filters_found++;
          $vocabulary = taxonomy_get_vocabulary($vid);
          $view = $form['view']['#value'];

          // Make it use a hierarchical select.
          require_once drupal_get_path('module', 'hierarchical_select') . '/includes/common.inc';
          unset($form["filter{$id}"]['#options']);
          unset($form["filter{$id}"]['#theme']);
          unset($form["filter{$id}"]['#size']);
          $form["filter{$id}"]['#type'] = 'hierarchical_select';
          $defaults_override = array(
            'module' => 'hs_taxonomy_views',
            'params' => array(
              'optional' => (bool) $view->exposed_filter[$id]['optional'],
              'vid' => $vid,
              'exclude_tid' => NULL,
              'root_term' => NULL,
            ),
            // When the **ALL** option is selected, nothing else should be.
            'exclusive_lineages' => array(
              '**ALL**',
            ),
            // This is a GET form, so also render the flat select.
            'render_flat_select' => 1,
          );
          hierarchical_select_common_config_apply($form["filter{$id}"], "taxonomy-views-{$view->name}-{$vid}", $defaults_override);

          // Inherit #required from the exposed filter settings.
          $form["filter{$id}"]['#required'] = !(bool) $view->exposed_filter[$id]['optional'];

          // Put the altered exposed filters in a separate table row.
          hierarchical_select_common_views_exposed_filters_reposition();
        }
      }
    }
    if ($hs_exposed_filters_found > 0) {

      // Views will remove the form_id in views_filters_process(), but we need
      // it for Hierarchical Select to work, so put it back.
      $form['copy_of_form_id'] = $form['form_id'] + array(
        '#parents' => array(
          'form_id',
        ),
      );
    }
  }

  // Alter the edit view form: add a link to the Hierarchical Select
  // configuration when appropriate and to mark which settings are now managed
  // by the Hierarchical Select configuration.
  if ($form_id == 'views_edit_view') {
    foreach ($form['exposed_filter'] as $filter_id => $filter) {
      if (is_numeric($filter_id)) {
        $id = $form['exposed_filter'][$filter_id]['id']['#default_value'];
        if (preg_match("/term_node_(\\d+)\\.tid/", $id, $matches)) {
          $vid = $matches[1];
          if (variable_get("taxonomy_hierarchical_select_{$vid}", 0)) {
            $view = $form['#parameters'][1];
            $link = l(t('Configure Hierarchical Select'), "admin/build/views/{$view->name}/hs_config/{$vid}");
            $form['exposed_filter'][$filter_id]['name']['#value'] .= '<br />' . $link;

            // Alter the form to support the current Hierarchical Select
            // config.
            require_once drupal_get_path('module', 'hierarchical_select') . '/includes/common.inc';
            $config_id = "taxonomy-views-{$view->name}-{$vid}";
            $config = hierarchical_select_common_config_get($config_id);
            $text = t('This setting is now managed by the<br />Hierarchical Select configuration!');

            // Exposed filter's "Force single" setting.
            $form['exposed_filter'][$filter_id]['single']['#description'] = $text;

            // Additional settings when save_lineage is enabled.
            if ($config['save_lineage']) {

              // Filter's "Operator" setting.
              $form['filter'][$filter_id]['operator']['#description'] = $text;

              // Exposed filter's "Lock Operator" setting.
              $form['exposed_filter'][$filter_id]['operator']['#description'] = $text;
            }
          }
        }
      }
    }
  }
}