更改搜索表单操作不适用于自定义搜索模块Drupal 6
我正在修改Drupal站点上基本搜索表单的表单操作的值。下面是我添加到自定义搜索模块中的更改搜索表单操作不适用于自定义搜索模块Drupal 6,drupal,search,drupal-6,Drupal,Search,Drupal 6,我正在修改Drupal站点上基本搜索表单的表单操作的值。下面是我添加到自定义搜索模块中的hook\u form\u alter()实现中的一行: $form_id['#action'] = 'http://mydomain.com/search/customsearchmodule/'; 当我查看表单的源代码时,操作与我在上一行中设置的完全相同。问题是,提交表单后,将显示默认搜索结果页面,而不是自定义搜索结果页面 如何将自定义搜索结果页面设置为显示,而不是默认页面 编辑 下面是我的hook\u
hook\u form\u alter()
实现中的一行:
$form_id['#action'] = 'http://mydomain.com/search/customsearchmodule/';
当我查看表单的源代码时,操作与我在上一行中设置的完全相同。问题是,提交表单后,将显示默认搜索结果页面,而不是自定义搜索结果页面
如何将自定义搜索结果页面设置为显示,而不是默认页面
编辑
下面是我的hook\u form\u alter()
实现的一个示例:
/**
* Implementation of hook_form_alter()
*/
function mymodule_form_alter(&$form, &$form_state, $form_id) {
if($form_id == 'search_block_form') {
$form['#action'] = 'http://www.mydomain.com/search/mymodule/';
}
}
另外:
我添加了unset($form['#submit'])
到我的hook\u form\u alter()
实现,并在提交表单后到达相应的URL(http://www.mydomain.com/search/mymodule
),仅不传递任何数据(无关键字)
编辑#2
我在DrupalAPI和核心搜索模块中进行深入研究,只是想看看它是如何工作的。这是我们的/modules/search/search.module
的核心search\u box\u form\u submit()
功能:
/**
* Process a block search form submission.
*/
function search_box_form_submit($form, &$form_state) {
// The search form relies on control of the redirect destination for its
// functionality, so we override any static destination set in the request,
// for example by drupal_access_denied() or drupal_not_found()
// (see http://drupal.org/node/292565).
if (isset($_REQUEST['destination'])) {
unset($_REQUEST['destination']);
}
if (isset($_REQUEST['edit']['destination'])) {
unset($_REQUEST['edit']['destination']);
}
$form_id = $form['form_id']['#value'];
$form_state['redirect'] = 'search/node/'. trim($form_state['values'][$form_id]);
}
注意定义了$form_state['redirect']的行。如果我注释掉这一行,那么搜索表单将登陆search/mymodule/
页面,尽管没有传递任何搜索关键字
编辑-工作代码
最终的解决方案是对nmc答案的修改版本。我不得不改变他的mymodule\u form\u submit()
实现。函数应命名为mymodule\u submit()
,函数中的代码也需要更改:
function mymodule_submit($form, &$form_state) {
// The search form relies on control of the redirect destination for its
// functionality, so we override any static destination set in the request,
// for example by drupal_access_denied() or drupal_not_found()
// (see http://drupal.org/node/292565).
if (isset($_GET['destination'])) {
unset($_GET['destination']);
}
// Check to see if the form was submitted empty.
// If it is empty, display an error message.
// (This method is used instead of setting #required to TRUE for this field
// because that results in a confusing error message. It would say a plain
// "field is required" because the search keywords field has no title.
// The error message would also complain about a missing #title field.)
if ($form_state['values']['search_block_form'] == '')
{
form_set_error('keys', t('Please enter some keywords.'));
}
else
{
$form_id = $form['form_id']['#value'];
$form_state['redirect'] = 'search/mymodule/'. trim($form_state['values'][$form_id]);
}
}
我相信您想更新$form的#action元素,而不是$form#id
function MYMODULE_form_alter(&$form, &$form_state, $form_id) {
if ($form_id == 'search_form') {
$form['#action'] = 'MYMODULE_search_url';
}
}
工作非常好,带我到我自己的页面,不显示标准的搜索结果
编辑:
好的,我现在玩得更多了一点,实际上我上面写的在33,(3)%;)尝试使用Garland主题,我在左侧边栏中有一个搜索框($form#id='search#theme#form')-我们称之为search#1,在/search页面上有一个搜索框(search#2),在/search/node/where结果页面上有第三个搜索框(search#3)
现在,我刚刚看到的是更新$form['#action']在Search#2的情况下有效,但在另外两种情况下无效。因此,解决方案并不令人满意,那么让我们尝试另一种方法
让我们将自己的#提交函数添加到相关表单中:
function MYMODULE_form_alter(&$form, &$form_state, $form_id) {
if ($form_id == 'search_form' || $form_id == 'search_theme_form') {
$form['#submit'][] = 'MYMODULE_submit';
}
}
function MYMODULE_submit($form, &$form_state) {
$form_state['redirect'] = 'MYMODULE_path';
}
瞧,这三种情况都有效了 尝试更改表单调用的提交函数,而不是更改表单操作的URL:
/**
* Implementation of hook_form_alter()
*/
function mymodule_form_alter(&$form, &$form_state, $form_id) {
if($form_id == 'search_block_form') {
$form['#submit'][] = 'mymodule_form_submit';
}
}
/**
* Process a block search form submission.
*/
function mymodule_form_submit($form, &$form_state) {
// The search form relies on control of the redirect destination for its
// functionality, so we override any static destination set in the request,
// for example by drupal_access_denied() or drupal_not_found()
// (see http://drupal.org/node/292565).
if (isset($_GET['destination'])) {
unset($_GET['destination']);
}
// Check to see if the form was submitted empty.
// If it is empty, display an error message.
// (This method is used instead of setting #required to TRUE for this field
// because that results in a confusing error message. It would say a plain
// "field is required" because the search keywords field has no title.
// The error message would also complain about a missing #title field.)
if ($form_state['values']['search_block_form'] == '') {
form_set_error('keys', t('Please enter some keywords.'));
}
$form_id = $form['form_id']['#value'];
$form_state['redirect'] = 'search/mymodule/'. trim($form_state['values'][$form_id]);
}
else {
form_set_error(NULL, t('Search is currently disabled.'), 'error');
}
}
这会将搜索重定向到
http://www.mydomain.com/search/mymodule/keywords
您的模块随后可以相应地处理关键字。您是在为实际搜索执行自定义代码,还是只想拥有一个自定义搜索结果页面?我在模块中实现了hook_search()
,用于自定义搜索。所以,是的,它不仅仅是一个自定义搜索结果页面。不确定,这听起来很奇怪。不确定您是指搜索而不是搜索,但我确信这只是一个输入错误,不是问题的根源。你可以随时发出“哎呀”的声音,这只是个打字错误。我的意思是搜索,而不是搜索。现在修好。感谢drupal stackexchange的URL,我将试一试。如果您选择覆盖其所有工作,为什么要使用此核心表单?您是否考虑过创建自己的搜索框?如果没有,为什么不呢?是的,它会更改表单的操作值,但在提交后会将我发送给So,您的解决方案是将表单定向到自定义模块结果页面,但URL中没有搜索关键字。我现在正在查看它,看看是否还可以找出其他任何东西。只需看看最初在搜索模块中是如何完成的:$form_id=$form['form_id']['#value']$表单_state['redirect']='search/node/'。修剪($form_state['values'][$form_id]);我不得不修改你的代码,但成功了!查看对我的问题的最后一次编辑,查看修改后的代码。@letseatfood:太棒了!感谢您与社区分享更正。