You are here

function hs_content_taxonomy_views_form_alter in Hierarchical Select 5.3

Implementation of hook_form_alter().

File

modules/hs_content_taxonomy_views.module, line 41
Implementation of the Hierarchical Select API for the Content Taxonomy Views module.

Code

function hs_content_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) {
      $field_name = _hs_content_taxonomy_views_parse_fieldname_from_id($filter['id']);
      if ($field_name !== FALSE) {
        $widget_type = db_result(db_query("SELECT widget_type FROM {node_field_instance} WHERE field_name = '%s'", $field_name));

        // Only apply Hierarchical Select if it's enabled for this field.
        if ($widget_type == 'content_taxonomy_hs') {
          $hs_exposed_filters_found++;
          $field = content_fields($field_name);
          $vid = $field['vid'];

          // This is the vocabulary id, not the view id!
          $tid = $field['tid'];
          $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}"]['#size']);
          unset($form["filter{$id}"]['#options']);
          unset($form["filter{$id}"]['#theme']);
          $form["filter{$id}"]['#type'] = 'hierarchical_select';
          $defaults_override = array(
            'module' => 'hs_content_taxonomy_views',
            'params' => array(
              'optional' => (bool) $view->exposed_filter[$id]['optional'],
              'vid' => $vid,
              'tid' => $tid,
              'depth' => min($field['depth'], _hs_taxonomy_hierarchical_select_get_depth($vid)),
            ),
            // 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}"], "content-taxonomy-views-{$view->name}-{$field_name}", $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 $filter['field'] is not set, the exposed filter is being deleted.
      if (is_numeric($filter_id) && isset($filter['field'])) {
        $id = $form['exposed_filter'][$filter_id]['id']['#default_value'];
        $field_name = _hs_content_taxonomy_views_parse_fieldname_from_id($id);
        if ($field_name !== FALSE) {
          $widget_type = db_result(db_query("SELECT widget_type FROM {node_field_instance} WHERE field_name = '%s'", $field_name));
          if ($widget_type == 'content_taxonomy_hs') {
            $view = $form['#parameters'][1];
            $link = l(t('Configure Hierarchical Select'), "admin/build/views/{$view->name}/hs_config/{$field_name}");
            $form['exposed_filter'][$filter_id]['name']['#value'] .= '<br />' . $link;

            // Alter the form to support the current Hierarchical Select
            // config when …
            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;
            }
          }
        }
      }
    }
  }
}