You are here

function views_filters_selective_form_views_exposed_form_alter_do in Views Hacks 6

2 calls to views_filters_selective_form_views_exposed_form_alter_do()
views_filters_selective_exposed_form_plugin::exposed_form_alter in views_filters_selective/views_filters_selective_exposed_form_plugin.inc
views_filters_selective_form_views_exposed_form_alter in views_filters_selective/views_filters_selective.module
Implementation of hook_form_FORMID_alter() for views_exposed_form.

File

views_filters_selective/views_filters_selective.module, line 47

Code

function views_filters_selective_form_views_exposed_form_alter_do(&$form, $form_state, $settings) {
  static $guard = FALSE;
  if ($guard) {
    return;
  }
  $guard = TRUE;

  // Go through each filter checking for a 'selective' setting.
  $active = 0;
  foreach ($form_state['view']->filter as $filter_id => $filter) {
    if (empty($filter->options['exposed'])) {
      continue;
    }
    $active++;

    // count active exposed filters
    if (empty($settings[$filter_id]['vfs_selective'])) {
      continue;
    }

    // Form element is designated by the element ID which is user-configurable.
    $filter_element = $form['#info']["filter-{$filter_id}"]['value'];

    // Execute a clone of the view with a few changes:
    // * no grouped fields for multiple values
    // * no distinct
    // * no paging
    // * no caching
    $view = $filter->view
      ->clone_view();
    if (empty($settings[$filter_id]['vfs_active'])) {
      $view
        ->set_exposed_input(array(
        'dummy' => TRUE,
      ));
    }
    else {
      $view
        ->set_exposed_input($filter->view->exposed_input);
    }

    // Fix case of exposed form in block for view with arguments.
    if ($filter->view->display_handler
      ->get_option('exposed_block') && !empty($filter->view->argument)) {
      static $arguments;
      if (!empty($filter->view->args)) {

        // Remember the arguments because next time we're here we'll need them.
        $arguments = $filter->view->args;
      }
      else {
        if (!empty($arguments)) {
          $view
            ->set_arguments($arguments);
        }
      }
    }
    $items = $view
      ->get_items('field', $filter->view->current_display);
    foreach ($items as $item) {
      if (!empty($item['multiple']['group'])) {
        $item['multiple'] = array(
          'group' => FALSE,
          'multiple_number' => '',
          'multiple_from' => '',
          'multiple_reversed' => FALSE,
        );
        $view
          ->set_item($filter->view->current_display, 'field', $item['field'], $item);
      }
    }
    $view->display_handler
      ->set_option('distinct', FALSE);
    $view->display_handler
      ->set_option('cache', array(
      'type' => 'none',
    ));
    $view
      ->set_display($filter->view->current_display);
    $view
      ->pre_execute();
    if (isset($_GET['items_per_page'])) {
      $items_per_page = $_GET['items_per_page'];
      unset($_GET['items_per_page']);
    }
    $view
      ->set_items_per_page(0);
    if (isset($items_per_page)) {
      $_GET['items_per_page'] = $items_per_page;
    }
    $view
      ->execute();

    // Don't continue if we're displaying a summary.
    if (!empty($view->build_info['summary'])) {
      $guard = FALSE;
      return;
    }

    // Filter the results.
    $filter_settings = explode(':', $settings[$filter_id]['vfs_field']);
    $field_id = NULL;
    if (!empty($filter_settings[1])) {

      // Filter on a field name.
      $field_id = $filter_settings[1];
      $field_alias = $view->field[$field_id]->field_alias;
      $options = array();
      foreach ($view->result as $row) {
        if (isset($row->{$field_alias})) {
          $options[] = $row->{$field_alias};
        }
      }
    }
    else {
      if ($handler = _views_filters_selective_get_handler(get_class($filter))) {
        $oids = array();
        foreach ($view->result as $result) {
          $oids[] = $result->{$view->base_field};
        }
        $oids = array_filter($oids);
        $options = empty($oids) ? array() : call_user_func($handler, $view->filter[$filter_id], $oids);
      }
      else {
        drupal_set_message(t('Could not find a selective filter handler for %filter.', array(
          '%filter' => $filter->definition['title'],
        )), 'warning');
      }
    }
    drupal_alter('views_filters_selective_options', $options, $view->filter[$filter_id], $field_id ? $view->field[$field_id] : NULL, $oids);
    if ($filter->options['expose']['optional']) {
      $options[] = 'All';
    }
    if (in_array($form[$filter_element]['#type'], array(
      'select',
      'checkboxes',
      'radios',
    ))) {
      $form[$filter_element]['#options'] = _views_filters_selective_reduce_options($form[$filter_element]['#options'], $options);
    }
    else {
      if (!empty($options)) {
        sort($options);
        $options = array_combine(array_values($options), array_values($options));
      }
      $any_label = variable_get('views_exposed_filter_any_label', 'old_any') == 'old_any' ? '<Any>' : t('- Any -');
      if (isset($options['All'])) {
        unset($options['All']);
        $options = array(
          'All' => $any_label,
        ) + (array) $options;
      }
      else {
        if (!empty($settings[$filter_id]['vfs_optional'])) {
          $options = array(
            '' => $any_label,
          ) + (array) $options;
        }
      }
      $form[$filter_element]['#type'] = 'select';
      $form[$filter_element]['#multiple'] = FALSE;
      $form[$filter_element]['#options'] = $options;
      $form[$filter_element]['#default_value'] = 'All';
      $form[$filter_element]['#validated'] = TRUE;

      // avoid invalid selection error
      unset($form[$filter_element]['#size']);
    }
    if ((empty($form[$filter_element]['#options']) || array_keys($form[$filter_element]['#options']) === array(
      'All',
    ) || array_keys($form[$filter_element]['#options']) === array(
      '',
    )) && !empty($settings[$filter_id]['vfs_hide_empty'])) {
      $form[$filter_element]['#access'] = FALSE;
      $form["{$filter_element}_op"]['#access'] = FALSE;
      unset($form['#info']["filter-{$filter_element}"]);
      $active--;
    }
  }
  if (!$active) {

    // hide whole form if all exposed filters are hidden
    $form['#access'] = FALSE;
  }
  $guard = FALSE;
}