You are here

public function views_plugin_exposed_form::exposed_form_alter in Views (for Drupal 7) 7.3

Same name and namespace in other branches
  1. 6.3 plugins/views_plugin_exposed_form.inc \views_plugin_exposed_form::exposed_form_alter()

File

plugins/views_plugin_exposed_form.inc, line 234
Definition of views_plugin_exposed_form.

Class

views_plugin_exposed_form
The base plugin to handle exposed filter forms.

Code

public function exposed_form_alter(&$form, &$form_state) {
  if (!empty($this->options['reset_button'])) {
    $form['reset'] = array(
      '#value' => $this->options['reset_button_label'],
      '#type' => 'submit',
    );
  }
  $form['submit']['#value'] = $this->options['submit_button'];

  // Check if there is exposed sorts for this view
  $exposed_sorts = array();
  foreach ($this->view->sort as $id => $handler) {
    if ($handler
      ->can_expose() && $handler
      ->is_exposed()) {
      $exposed_sorts[$id] = check_plain($handler->options['expose']['label']);
    }
  }
  if (count($exposed_sorts)) {
    $form['sort_by'] = array(
      '#type' => 'select',
      '#options' => $exposed_sorts,
      '#title' => $this->options['exposed_sorts_label'],
    );
    $sort_order = array(
      'ASC' => $this->options['sort_asc_label'],
      'DESC' => $this->options['sort_desc_label'],
    );
    if (isset($form_state['input']['sort_by']) && isset($this->view->sort[$form_state['input']['sort_by']])) {
      $default_sort_order = $this->view->sort[$form_state['input']['sort_by']]->options['order'];
    }
    else {
      $first_sort = reset($this->view->sort);
      $default_sort_order = $first_sort->options['order'];
    }
    if (!isset($form_state['input']['sort_by'])) {
      $keys = array_keys($exposed_sorts);
      $form_state['input']['sort_by'] = array_shift($keys);
    }
    if ($this->options['expose_sort_order']) {
      $form['sort_order'] = array(
        '#type' => 'select',
        '#options' => $sort_order,
        '#title' => t('Order', array(), array(
          'context' => 'Sort order',
        )),
        '#default_value' => $default_sort_order,
      );
    }
    $form['submit']['#weight'] = 10;
    if (isset($form['reset'])) {
      $form['reset']['#weight'] = 10;
    }
  }
  $pager = $this->view->display_handler
    ->get_plugin('pager');
  if ($pager) {
    $pager
      ->exposed_form_alter($form, $form_state);
    $form_state['pager_plugin'] = $pager;
  }

  // Apply autosubmit values.
  if (!empty($this->options['autosubmit'])) {
    $form = array_merge_recursive($form, array(
      '#attributes' => array(
        'class' => array(
          'ctools-auto-submit-full-form',
        ),
      ),
    ));
    $form['submit']['#attributes']['class'][] = 'ctools-use-ajax';
    $form['submit']['#attributes']['class'][] = 'ctools-auto-submit-click';
    $form['#attached']['js'][] = drupal_get_path('module', 'ctools') . '/js/auto-submit.js';
    if (!empty($this->options['autosubmit_hide'])) {
      $form['submit']['#attributes']['class'][] = 'js-hide';
    }
  }
}