0

I've created a custom filter with a checkbox exposed field in my module.

class my_module_helper_handler_filter_timetracking_children extends views_handler_filter {

  /*...*/

  function value_form(&$form, &$form_state) {
    $form['value'] = array(
      '#type' => 'checkbox',
      '#title' => t('Show subtasks'),
      '#default_value' => isset($this->value) ? $this->value : NULL,
    );
    return $form;
  }

  /*...*/

}

When by default in Views UI settings checkbox is disabled, everything works fine; when it's enabled, it doesn't work. It doesn't respect the default value and the $_GET parameter.

I can't understand this strange behavior of single on/off checkboxes in exposed filters in a view.

I also tried using '#attributes' => array('checked'=>'checked'), but the filter didn't work correctly.

Might somebody can show his code how to create custom exposed filter with single checkbox? Or give usefull link

  • There is no need to return the form here since its passed by reference and I'm able to understand your question fully. could you please elaborate. – Mathankumar Dec 18 '12 at 6:50
  • Yes you are right about return. I created exposed filter with "checkbox". Than I add my filter in view, expose it and set it as checked by default. And the view doesn't filter any thing with my checkbox. When I set is as disabled by default everything works fine. Or when I use "select" view forks fine too. – milkovsky Dec 18 '12 at 18:06
1

Views doesn't handle the default checkbox state because when you load your view page for the first time (or if the user clicks the Reset button), the URL doesn't have any parameter in it, so that the $_GET parameter is useless. Here is a complete form definition that will handle the checkbox, whether the URL has parameters or not:

class my_module_helper_handler_filter_timetracking_children extends views_handler_filter {

  /*...*/

  function value_form(&$form, &$form_state) {

    $params = my_module_get_params();

    // First time the view is loaded (or user click the Reset button), URL doesn't have parameters
    if(!$params) {
      $value = 1; // checkbox checked by default
    }
    // If URL has parameters
    else {
      // If our filter is present in the URL as parameter and its value is '1'
      if (isset($params['timetracking_children']) && $params['timetracking_children'] == 1) {
        $value = 1;
      }
      else {
        $value = 0;
      }
    }

    $form['value'] = array(
      '#type' => 'checkbox',
      '#title' => t('Show subtasks'),
      '#default_value' => isset($this->value) ? $this->value : NULL,
      '#value' => $value,
    );
    return $form;
  }

  /*...*/

}

function my_module_get_params() {
  global $base_url;
  $query = array();

  $url = $base_url.request_uri();
  $url = parse_url($url);

  if(isset($url['query'])) {
    parse_str($url['query'],$query);
  }

  return $query;
}
|improve this answer|||||

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.