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 incustom_theme_form_views_exposed_form_alter()