简体   繁体   English

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

[英]Altering search form action not working for custom search module, Drupal 6

I am altering the value of the form action for the basic search form on a Drupal site. 我正在改变Drupal站点上基本搜索表单的表单操作的值。 Here is the line I added to our hook_form_alter() implementation in our custom search module: 这是我在自定义搜索模块中添加到hook_form_alter()实现的行:

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

When I view the source for the form, the action is exactly as I have set it in the above line. 当我查看表单的源时,操作就像我在上面的行中设置它一样。 The problem is, after the form has been submitted, the default search results page is displayed, not the custom search results page. 问题是,在提交表单后,将显示默认搜索结果页面,而不是自定义搜索结果页面。

How do I set the custom search results page to display, rather than the default one? 如何设置要显示的自定义搜索结果页面,而不是默认值?

Edit 编辑

Here is an example of my hook_form_alter() implementation: 这是我的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/';
  }
}

Also: 也:

I added unset($form['#submit']); 我添加了unset($form['#submit']); to my implementation of hook_form_alter() and, after submitting the form, arrived at the appropriate URL ( http://www.mydomain.com/search/mymodule ), only without any data passed (no keywords). 到我的hook_form_alter()实现,并在提交表单后,到达相应的URL( http://www.mydomain.com/search/mymodule ),只传递任何数据(没有关键字)。

Edit #2 编辑#2

I am digging around in the Drupal API and in the core search module just to see how it works. 我正在挖掘Drupal API和核心搜索模块,看看它是如何工作的。 This is the core search_box_form_submit() function from our /modules/search/search.module : 这是我们的/modules/search/search.module的核心search_box_form_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]);
    }

Note the line where $form_state['redirect'] is defined. 请注意定义$ form_state ['redirect']的行。 If I comment this line out, then the search form will land on the search/mymodule/ page, although without any search keywords passed. 如果我注释掉这一行,那么搜索表单将落在search/mymodule/ page上,尽管没有传递任何搜索关键字。

Edit - Working Code 编辑 - 工作代码

The final solution is an altered version of nmc's answer. 最终解决方案是nmc答案的改进版本。 I had to alter his mymodule_form_submit() implementation. 我不得不改变他的mymodule_form_submit()实现。 The function should be named mymodule_submit() and the code within the function needed to be altered as well: 该函数应命名为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]);
  }
}

Try changing the submit function which the form calls, instead of changing the URL of the form's action: 尝试更改表单调用的提交函数,而不是更改表单操作的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');
  }
}

This should redirect the search to http://www.mydomain.com/search/mymodule/keywords which your module can then process the keywords accordingly. 这应该将搜索重定向到http://www.mydomain.com/search/mymodule/keywords ,然后您的模块可以相应地处理关键字。

I believe you wanted to update #action element of $form, not $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';
  }
}

works perfectly fine, takes me to my own page and does not display standard search results there. 工作得很好,带我到我自己的页面,并没有在那里显示标准的搜索结果。

Edit: 编辑:

Ok, I have played with it a little bit more now, and actually what I have written above works in 33,(3)%. 好吧,我现在已经玩了一点,实际上我上面写的是33,(3)%。 ;) Trying it with Garland theme, I have one search box in left sidebar ($form_id = 'search_theme_form') - let's call it Search #1, one search box on /search page (Search #2) and third one on /search/node/whatever results page (Search #3). ;)尝试使用Garland主题,我在左侧边栏中有一个搜索框($ form_id ='search_theme_form') - 让我们称之为搜索#1,搜索框上的搜索框(搜索#2)和第三搜索框/搜索/ node /无论结果页面(搜索#3)。

Now, what I've just seen is that updating $form['#action'] works in case of Search #2, but do not work in two other cases. 现在,我刚刚看到的是更新$ form ['#action']适用于Search#2,但在其他两种情况下不起作用。 Therefore, solution not satisfactory, let's try a different approach then. 因此,解决方案不尽如人意,让我们尝试不同的方法。

Let's add our own #submit function to form in question: 让我们添加我们自己的#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';
}

And voila, works in all 3 cases now! 瞧,现在在所有3个案例中都有效!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM