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

时间:2011-06-22 18:33:39

标签: drupal search drupal-6

我正在改变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(),并在提交表单后,到达了相应的网址(http://www.mydomain.com/search/mymodule),但没有传递任何数据(没有关键字)。< / p>

编辑#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]);
  }
}

2 个答案:

答案 0 :(得分: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');
  }
}

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

答案 1 :(得分:0)

我相信你想更新$ 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') - 让我们称之为搜索#1,搜索框上的搜索框(搜索#2)和第三搜索框/搜索/ node /无论结果页面(搜索#3)。

现在,我刚刚看到的是更新$ form ['#action']适用于Search#2,但在其他两种情况下不起作用。因此,解决方案不尽如人意,让我们尝试不同的方法。

让我们添加我们自己的#submit函数来形成问题:

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';
}

瞧,现在所有3个案件都有效!