commiting uncommited changes on live site
[weblabels.fsf.org.git] / crm.fsf.org / 20131203 / files / sites / all / modules-old / views / help / alter-exposed-filter.html
CommitLineData
7f254ad8
AE
1Modifying 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.
2
3As 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:
4
5<pre>
6&lt;?php
7if (empty($form_state['view']->exposed_input[$identifier])) .
8 { $form_state['input'][$identifier] = $default_value; }
9?&gt;
10</pre>
11
12where $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.
13
14If 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.
15
16To 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():
17
18<pre>
19&lt;?php
20function MODULENAME_form_views_exposed_form_alter(&$form, $form_state) {
21 if(strpos($form['#id'], 'volunteer-directory') !== FALSE) {
22 $city_tid = geoip_redirect_get_tid();
23 if(is_numeric($city_tid) && $city_tid != 7660) {
24 if (empty($form_state['view']->exposed_input['tid'])) {
25 $form_state['input']['tid'] = $city_tid;
26 }
27 }
28 }
29}
30?&gt;
31</pre>