alter-exposed-filter.html
Same filename in other branches
File
View source
Modifying default values of a views exposed form is tricky, because FAPI was not designed to work with GET forms. One consequence is that it often can't tell if textfields (there are others) were submitted or not. As a consequence, it *always* thinks the value was submitted, even if it was not. To fix that, Views modifies $form_state['input'][$identifier] with the default value if $form_state['input'][$identifier] was not set. In order to modify the default value in an alter, you need to do this: <pre> <?php if (empty($form_state['view']->exposed_input[$identifier])) { $form_state['input'][$identifier] = $default_value; } ?> </pre> where $identifier is the particular filter for which you want to change the default value, and $default_value is the new default value you want to set. If you use a hook_form_FORM_ID_alter or hook_form_alter, you can modify exposed filters on the fly based on information that is external to Views. For example, I modified the exposed filter of a form to set a taxonomy term ID based on the user's GeoIP. To do this, I used the following function, where geoip_redirect_get_tid() loads the relevant term id based on the user's current ip_address(): <pre> <?php function MODULENAME_form_views_exposed_form_alter(&$form, $form_state) { if (strpos($form['#id'], 'volunteer-directory') !== FALSE) { $city_tid = geoip_redirect_get_tid(); if (is_numeric($city_tid) && $city_tid != 7660) { if (empty($form_state['view']->exposed_input['tid'])) { $form_state['input']['tid'] = $city_tid; } } } } ?> </pre>