Hi All
I installed the Search Files module and am trying to make the core search display the results of the 'Search Files' module by default instead of the results of 'Contents' (node_search).
I am able to implement this by modifying the core search.module file:
vi search.module
/**
* Process a block search form submission.
*/
function search_box_form_submit($form, &$form_state) {
$form_id = $form['form_id']['#value'];
// $form_state['redirect'] = 'search/node/'. trim($form_state['values'][$form_id]);
// modified search/node/ to search/search_files/
$form_state['redirect'] = 'search/search_files/'. trim($form_state['values'][$form_id]);
}
But obviously modifying the core is not recommended.
So I tried writing a new module that has this file.
search_new.module
----
function search_all_form_search_form_alter(&$form, &$form_state) {
// $form_id = $form['form_id']['#value'];
// echo " form state in form_alter " . $form_state['values'][$form_id];
// the form_state is NULL, i.e. the values are not passed over to this function.
$form['#submit'][] = 'search_all_special_submit';
}
function search_all_special_submit($form, &$form_state) {
$form_id = $form['form_id']['#value'];
// $form_state['redirect'] = 'search/node/'. trim($form_state['values'][$form_id]);
// echo " form state in special: " . $form_state['values'][$form_id];
// the form state is NULL! i.e., nothing is passed over to this function.
$form_state['redirect'] = 'search/search_files/'. trim($form_state['values'][$form_id]);
}
----
This module is supposed to do a search_form_alter hook to add the #submit property to the search_form and then make the form use my own function 'search_all_special_submit' instead of
the core search_form_submit function.
The hook works and it does use my own submit function.
But the problem is the form_state is NULL. That is, no values are passed over to either the
search_all_form_search_form_alter or search_all_special_submit functions.
I also tried using #after_build in search_all_special_submit to go to another of my function which then adds the #submit property -- but again form_state is NULL.
I even tried adding #submit into the core search.module's search_form function but again the form_state is NULL in my own submit function "search_all_special_submit'.
It seems that the form_state data is not available in anywhere else except the default form_submit.
It is not available to form_alter hook, #submit and #after_build functions.
Thank you.
Comments
Comment #1
cmgui commentedCorrection, should be $form_state instead of form_status
Comment #2
cmgui commented$form_state instead of form_status
Comment #3
cmgui commentedI found that the form_state is actually from this function in the core search.module:
function search_box(&$form_state, $form_id)
The problem is somehow I am not able to do a mymodule_form_search_box_alter in my module to change the #submit to point to my own submit function. The search_box function doesn't seem to be considered as a form function by Drupal?
Any ideas how to hook onto this search_box function?
The form that calls search_box is search_forms (with form_id = search_theme_form)
but search_them_form also does not pass over the $form_state data to my functions,
just like search_form function.
That is, hook_form_search_theme_form_alter does not receive the $form_state data.
Thank you.
Comment #4
ainigma32 commentedIf you just want to override the search_box_form this will do:
Please post back how that works out for you.
- Arie
Comment #5
cmgui commentedHi Arie
Thank you for the reply.
It doesn't seem to work.
I tried search_all_form_search_block_form_alter and search_all_form_search_box_form_alter (actually there is no search_box_form) and search_all_form_search_theme_form_alter.
search_all_form_search_block_form_alter function wasn't executed at all, meaning it didn't hook on to search box when I hit the Search button.
search_all_form_search_box_form_alter didn't work because there is no such form as search_box_form.
search_all_form_search_theme_form_alter function did execute when I hit the Search button but the $form_state passed to it was NULL. $form got passed over to search_all_form_search_theme_form_alter but obviously $form_state was not.
Yes, I had realized the $form['#submit'] has to be replaced.
Thank you.
gui
Comment #6
ainigma32 commentedIf you look at the source of the page with the search form in it. What is the value of the form_id field?
In Garland it should look something like:
<input type="hidden" value="search_block_form" id="edit-search-block-form" name="form_id"/>That corresponds to search_all_form_search_block_form_alter
If your form is named differently you should change the function name.
I tested this locally and it works on my end.
- Arie
Comment #7
cmgui commentedHi Arie
It is search_theme_form.
Thank you
Gui
Comment #8
cmgui commentedHi Arie
Thank you. I got it to work now.
I use this
function search_all_form_search_theme_form_alter(&$form, &$form_state) {
$form['#submit'] = array('search_all_special_submit');
}
function search_all_special_submit($form, &$form_state) {
$form_id = $form['form_id']['#value'];
$form_state['redirect'] = 'search/search_files/'. trim($form_state['values'][$form_id]);
}
--
I made two mistakes previously.
1) $form['#submit'][] = 'search_all_special_submit'
instead of
$form['#submit'] = array('search_all_special_submit')
2) to test, I do this in search_all_form_search_theme_form_alter
$form_id = $form['form_id']['#value'];
echo $form_id;
echo $form_state['values'][$form_id];
and the second echo is blank because $form_state does not have 'values' key in this function.
It is 'post' instead. That is, have to echo $form_state['post'][$form_id];
But in search_all_special_submit function, it is 'values'.
Thank you once again.
Comment #9
cmgui commented