I have been able to use the search form in a page with a path different from the canonical one, using the following code.
/**
* Implements hook_menu().
*/
function code_testing_menu() {
drupal_static_reset('search_get_info');
$default_info = search_get_default_module_info();
$items['test/search'] = array(
'title' => 'User search test',
'page callback' => 'drupal_get_form',
'page arguments' => array('code_testing_user_search_form'),
'access callback' => 'search_is_active',
'type' => MENU_CALLBACK,
);
$items["test/search/%menu_tail"] = array(
'title' => 'User search test',
'load arguments' => array('%map', '%index'),
'page callback' => 'drupal_get_form',
'page arguments' => array('code_testing_user_search_form', 2),
'access callback' => '_search_menu_access',
'access arguments' => array('user'),
'type' => MENU_CALLBACK,
'weight' => 0,
);
return $items;
}
function code_testing_user_search_form($form, &$form_state, $keys = '') {
$info = FALSE;
$keys = trim($keys);
if (!$keys && !empty($_REQUEST['keys'])) {
$keys = trim($_REQUEST['keys']);
}
$active_module_info = search_get_info();
if (isset($active_module_info['user'])) {
$info = $active_module_info['user'];
}
if (!$info) {
form_set_error(NULL, t('Search is currently disabled.'), 'error');
return $form;
}
$action = 'test/search';
$form['#action'] = url($action);
$form_state['action'] = $action;
$form['#attributes']['class'][] = 'search-form';
$form['module'] = array(
'#type' => 'value',
'#value' => 'user',
);
$form['basic'] = array(
'#type' => 'container',
'#attributes' => array('class' => array('container-inline')),
);
$form['basic']['keys'] = array(
'#type' => 'textfield',
'#title' => t('Enter your keywords'),
'#default_value' => $keys,
'#size' => 40,
'#maxlength' => 255,
);
$form['basic']['processed_keys'] = array(
'#type' => 'value',
'#value' => '',
);
$form['basic']['submit'] = array(
'#type' => 'submit',
'#value' => t('Search'),
);
if (!empty($form_state['results'])) {
$form['search_results'] = $form_state['results'];
}
return $form;
}
function code_testing_user_search_form_submit($form, &$form_state) {
$active_module_info = search_get_info();
$conditions = NULL;
$keys = $form_state['values']['keys'];
if (!empty($active_module_info[$form_state['values']['module']])) {
$info = $active_module_info[$form_state['values']['module']];
if (isset($info['conditions_callback']) && function_exists($info['conditions_callback'])) {
$conditions = call_user_func($info['conditions_callback'], $keys);
}
if ($keys || !empty($conditions)) {
$form_state['results'] = search_data($keys, $info['module'], $conditions);
$form_state['rebuild'] = TRUE;
}
}
}
I tried writing the page callback (code_testing_user_search_view()
) for test/search, and test/search/%menu_tail basing on the search_view() code, but it didn't work. I used dpm($_POST, time());
in code_testing_user_search_view()
, and I noticed the function was called twice, after the search form was submitted: In the first invocation, $_POST
was populated with the expected values, while in the second invocation $_POST
was an empty array. The code I used for code_testing_user_search_view()
was slightly different from the code used by search_view()
because code_testing_user_search_view()
was not getting the name of the module implementing the search hooks as argument.
function code_testing_user_search_view($keys = '') {
$info = FALSE;
$redirect = FALSE;
$keys = trim($keys);
if (!$keys && !empty($_REQUEST['keys'])) {
$keys = trim($_REQUEST['keys']);
}
$active_module_info = search_get_info();
if (isset($active_module_info['user'])) {
$info = $active_module_info['user'];
}
$results = array('#markup' => '');
$conditions = NULL;
dpm($_POST);
if (isset($info['conditions_callback']) && function_exists($info['conditions_callback'])) {
$conditions = call_user_func($info['conditions_callback'], $keys);
}
if ($keys || !empty($conditions)) {
$results = search_data($keys, $info['module'], $conditions);
}
$build['search_form'] = drupal_get_form('search_form', 'test/search', $keys, 'user');
$build['search_results'] = $results;
return $build;
}
dpm($_POST)
, I noticed the page callback is called twice; when the page callback is called the second time,$_POST
is empty.