2

Is it possible to remove the default value an exposed filter (implemented by the Better exposed filter module) in a view? I would like to have users choose a value.

I tried using this in the template file, but nothing changed.

function custom_theme_form_views_exposed_form_alter(&$form, &$form_state) {
  // dpm($form);
  if ($form['#id'] == 'views-exposed-form-betterexposed-page-1') {
    foreach(element_children($form) as $key => $value) {
      if (isset($form[$key]['#type']) && $form[$key]['#type'] == 'select') {
        array_shift($form[$key]['#options']);
      }
    }
  }
}

The HTML markup is now the following.

<form action="/be1" method="get" id="views-exposed-form-betterexposed-page-1" accept-charset="UTF-8" class="jquery-once-1-processed"><div><div class="views-exposed-form">
  <div class="views-exposed-widgets clearfix">
          <div id="edit-status-wrapper" class="views-exposed-widget views-widget-filter-status">
                  <label for="edit-status">
            Published          </label>
                        <div class="views-widget">
          <div class="form-item form-type-radios form-item-status">
 <div id="edit-status" class="form-radios bef-select-as-radios"><div class="form-item form-type-radio form-item-status">
 <input class="bef-select-as-radios form-radio" type="radio" id="edit-status-1" name="status" value="1" checked="checked">  <label class="option" for="edit-status-1">Yes </label>

</div>
<div class="form-item form-type-radio form-item-status">
 <input class="bef-select-as-radios form-radio" type="radio" id="edit-status-0" name="status" value="0">  <label class="option" for="edit-status-0">No </label>

</div>
</div>
<div class="description"></div></div>
</div>
        </div>
              </div>

                    <div class="views-exposed-widget views-submit-button">
      <input type="submit" id="edit-submit-betterexposed" name="" value="Apply" class="form-submit">    </div>
      </div>
</div>
</div></form>

I also tried the following code.

function custom_theme_form_views_exposed_form_alter(&$form, &$form_state) {
foreach(element_children($form) as $key => $value) {
    if (isset($form[$value]['#type']) && $form[$value]['#type'] == 'select') {

      $options = $form[$value]['#options'];
      $newOptions = array();

      for ($i = 0, $optionsCount = count($options); $i < $optionsCount; $i++) {
        $optionsKey = array_keys($options[$i]->option);
        $optionsValue = array_values($options[$i]->option);

        $newOptions[$optionsKey[0]] = $optionsValue[0];
      }

      unset($form[$value]['#multiple']);

      $form[$value]['#type'] = 'radios';
      $form[$value]['#options'] = $newOptions;
      $form[$value]['#theme'] = 'select_as_radios';
    }
  }
}

I now get the following notice and warning messages.

Notice: Trying to get property of non-object in custom_theme_form_views_exposed_form_alter() Warning: array_keys() expects parameter 1 to be array, null given in custom_theme_form_views_exposed_form_alter()

1

May be something like this (with hook_form_FORM_ID_alter function):

function MYMODULE_form_views_exposed_form_alter(&$form, &$form_state) {
  // dpm($form);
  if ($form['#id'] == 'MY_EX_FORM_ID') {
    foreach(element_children($form) as $key => $value) {
      if (isset($form[$key]['#type']) && $form[$key]['#type'] == 'select') {
        array_shift($form[$key]['#options']);
      }
    }
  }
}

EDIT:

Ok, it seems that better exposed filter doesn't allow to remove default value from radio if settings are default (with default settings, filter -> is one of, BEF Settings -> checkboxes/radio buttons). I realized that in my project radios have been made from checkboxes type (with alter function), that's why in my example it's working fine..

here is a working example:

FIlter Settings

in hook_form_form_id_alter function:

foreach(element_children($form) as $key => $value) {
    if (isset($form[$value]['#type']) && $form[$value]['#type'] == 'select') {

      $options = $form[$value]['#options'];
      $newOptions = array();

      for ($i = 0, $optionsCount = count($options); $i < $optionsCount; $i++) {
        $optionsKey = array_keys($options[$i]->option);
        $optionsValue = array_values($options[$i]->option);

        $newOptions[$optionsKey[0]] = $optionsValue[0];
      }

      unset($form[$value]['#multiple']);

      $form[$value]['#type'] = 'radios';
      $form[$value]['#options'] = $newOptions;
      $form[$value]['#theme'] = 'select_as_radios';
    }
  }
| improve this answer | |
1

I came across this issue myself, and found a solution. It's not ideal, but it works.

Rather than setting your filter value to be required, allow it to add an - Any - option; in hook_form_alter(), remove the All option, and set the #value and #default_value of your form to a falsy value (e.g 0, NULL).

Removing the - Any - option will cause the form to not have a valid default, and instead have no Radio button selected. Unfortunately, in the form validation handler, it will reach a point where the form is determined to be invalid and display an error to the user.

Setting #value and #default_value to something that PHP interprets as false, will avoid reaching this error condition, and you will be left with your radio button options, but with none selected by default.

You could use code similar to the following one.

function YOUR_MODULE_form_views_exposed_form_alter(&$form, $form_state) {
  if(isset($form['field_YOUR_FIELD'])){
    // Set the value and default value to a falsy value, so that when the form is validated in _form_validate,
    // it doesn't throw an error due to the missing option, which we remove ('All').
    // This is done in order to have the radio buttons UNSET by default, which isn't possible with views
    // and Better Exposed Filters. We instead allow the All value to appear, and then remove it.
    if($form_state['input']['field_YOUR_FIELD'] == 'All'){
      $form['field_YOUR_FIELD']['#value'] = NULL;
    }
    $form['field_YOUR_FIELD']['#default_value'] = NULL;
    unset($form['field_YOUR_FIELD']['#options']['All']);
  }
}
| 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.