What hook function (is _form_alter correct?) do I use to alter the input submitted by the user into a views exposed filter form? I'm developing a views exposed filter form for searching for items in given geographic locations. 

I want to restrict this to a particular country so that cities with the same name in other countries aren't shown in the View results. For example only London, UK not London, Ontario; Portsmouth UK not Portsmouth New Hampshire, USA. 

(Note please don't close this topic as being too local - my question would apply to anyone who wants to do this whatever country they are in.)

The simplest approach I want to consider is suffix the user input with the country. For my example this would be Newcastle becomes Newcastle, UK.  Given that the site is focused on users in particular country, for user experience (UX) usability reasons I do not want the user to have to enter the country name at the end.

I had tried to add a function to do the suffixing by adding it as a callback in the form[#submit] array in _form_alter and implement the function in my custom module. But this did not alter my submitted input as required. Here is my code snippets:

function mymodule_form_alter(&amp;$form, $form_state, $form_id) {

// keep the standard handler
$temp = $form['#submit'][0];

// put my handler in first
$form['#submit'][0] = '_mymodule_custom_handler_submit';

// restore the standard handler to appear in the array after mine
$form['#submit'][2] = $temp;
}

function _mymodule_custom_handler_submit(&amp;$form, &amp;$form_state) {
    $form['field_geofield_distance']['origin']['#value'] .= ", UK";
}


When I try this I find (using  dpm ) that the submitted input is not altered before it gets sent to the View for it to create the results - so I deduce that this code is not running before that, but instead after that.

Can you advise on what I might be doing wrong here?

Also, in the $form['#submit'] array, are the callback functions "chained" together - i.e. in that the first function in the array gets called, it modifies the form submitted input and this is then passed to the next callback. If this were the case I would have expected my code to work.

Is _form_alter the right hook to use for this problem?

Update

Now using debugger (Komodo IDE) connected to my Vagrant VM-based local development environment that's running the site to investigate a solution:



Thanks.

Update 2:

I don't think this form handler approach is going to work, the screenshot below shows a callstack - see bottom right, showing that the views execute display function (presumably the view itself) runs before my code to modify the input
First if you want to add submit handler as first in form_alter do

//prepend the handler to array
array_unshift($form['#submit'], '_mymodule_custom_handler_submit');


So you dont overwrite any other callback by some other module.

Second, check that the form input elements dont have overriding submit handlers ("
submit action on the buttons")

Third, try to assign the value with form_set_value

$oldValue = $form_state['values']['field_geofield_distance']['origin'];
$value = array('origin' =&gt; array('#value' =&gt; $oldValue. ', UK')
form_set_value($form['field_geofield_distance'], $value, $form_state);


Syntax above is not correct, but without knowing the structure of the field its impossible to know what kind of array it requires. For example Link module fields can be modified with following.

    // Set first link of the field pointing to google, most likely 
    //unsets all the following ones
  $link = array(
    0 =&gt; array(
      'title' =&gt; 'Google',
      'url' =&gt; 'google.com'
    )
  );
  form_set_value($form['field_link'], $link)), $form_state);


Last but not least, use additional form validation callback instead submit handler, that way its sure that the values get passed to all "targeted(see second)" submit handlers.

Long story short. When form is being built, add input validator to it. Validator will get called after user sends the form and there you can modify values before they get submitted to submit handlers.

Summary interpretating view flow

While the view is being built first time it calls the form (which runs through standard drupal hooks like form_alter) and gets the values from it just in case it has some defaults or accepts 0 filters. 

After that it runs the queries, renders the form and the result. When finished you get the view including all the elements without user input. Like clean plate (mayby some contextual filters and stuff is applied but thats irrelevant?).

Next when the user sends the form with custom input, drupal (field widgets/handlers) transforms input to values and store them in $form_state and passes them to validators. Here the custom validator can modify the user input values in $form_state and attach them with form_set_value.

If the validation passes correctly $form_state values(modified) get passed to submit handlers. Most likely views will start building itself from them. Heres more exact flow(pdf)