You are here

function views_fields_on_off_form::exposed_form in Views Fields On/Off 7

Render our chunk of the exposed handler form when selecting.

Overrides views_handler::exposed_form

File

includes/views/views_fields_on_off_form.inc, line 20
Definition of views_fields_on_off_form.

Class

views_fields_on_off_form
Provides a handler that adds the form for Fields On/Off.

Code

function exposed_form(&$form, &$form_state) {
  $fields = $this->options['fields'];
  $options = array();
  $checked = array();
  $all_fields = $this
    ->get_field_labels_no_relationship_names($this->view->display_handler);

  // Grab the fields_on_off values that have been submitted already.
  $params = drupal_get_query_parameters();
  $on_off_submitted = array_key_exists('fields_on_off_hidden_submitted', $params);
  $checked_fields = array_key_exists('fields_on_off', $params) ? $params['fields_on_off'] : array();

  // Now loop through the fields defined in the view.
  foreach ($fields as $field) {
    if ($field) {
      $id = $field;
      if (array_key_exists($id, $all_fields)) {
        $label = $all_fields[$id];
        $options[$id] = $label;

        // If the field is included on the querystring...
        $check_me = !count($checked_fields) && !$on_off_submitted || array_key_exists($id, $checked_fields);
        if ($check_me) {

          // Check it because it has already been selected
          $checked[$id] = TRUE;
        }
      }
    }
  }

  // Form API to build the checkboxes.
  $form['fields_on_off'] = array(
    '#type' => 'checkboxes',
    '#title' => t('Show Fields'),
    '#description' => t('Select the fields you want to display'),
    '#options' => $options,
    '#value' => $options,
  );
  $form["fields_on_off_hidden_submitted"] = array(
    '#type' => 'hidden',
    '#default_value' => 1,
  );

  // Have to use $form_state['input'] because setting the default values on
  // the form field itself doesn't work.
  // Because of how Views handles the exposed filters,
  // this is how we set our values in the form.
  $form_state['input']['fields_on_off'] = $checked;
}