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

Mik*_*ore 4 search drupal drupal-6

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

$form_id['#action'] = 'http://mydomain.com/search/customsearchmodule/';
Run Code Online (Sandbox Code Playgroud)

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

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

编辑

以下是我的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/';
  }
}
Run Code Online (Sandbox Code Playgroud)

也:

我添加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]);
    }
Run Code Online (Sandbox Code Playgroud)

请注意定义$ 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]);
  }
}
Run Code Online (Sandbox Code Playgroud)

nmc*_*nmc 7

尝试更改表单调用的提交函数,而不是更改表单操作的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');
  }
}
Run Code Online (Sandbox Code Playgroud)

这应该重定向搜索,http://www.mydomain.com/search/mymodule/keywords然后您的模块可以相应地处理关键字.