gpt4 book ai didi

drupal - 更改搜索表单操作不适用于自定义搜索模块,Drupal 6

转载 作者:行者123 更新时间:2023-12-05 00:03:43 25 4
gpt4 key购买 nike

我正在更改 Drupal 站点上基本搜索表单的表单操作的值。这是我添加到我们的 hook_form_alter() 中的行在我们的自定义搜索模块中实现:

$form_id['#action'] = 'http://mydomain.com/search/customsearchmodule/';

当我查看表单的来源时,操作与我在上一行中设置的完全一样。问题是,提交表单后,显示的是默认搜索结果页面,而不是自定义搜索结果页面。

如何设置显示自定义搜索结果页面,而不是默认页面?

编辑

这是我的 hook_form_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_form_alter() 的实现并且,在提交表单后,到达了适当的 URL ( http://www.mydomain.com/search/mymodule),只是没有传递任何数据(没有关键字)。

编辑#2

我正在研究 Drupal API 和核心搜索模块,只是想看看它是如何工作的。这是核心 search_box_form_submit()来自我们的 /modules/search/search.module的功能:
    /**
* 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_form_submit()执行。该函数应命名为 mymodule_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]);
}
}

最佳答案

尝试更改表单调用的提交函数,而不是更改表单操作的 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然后您的模块可以相应地处理关键字。

关于drupal - 更改搜索表单操作不适用于自定义搜索模块,Drupal 6,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6444769/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com