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(&$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(&$form, &$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