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

发布于 2024-11-16 23:19:08 字数 3163 浏览 0 评论 0原文

我正在更改 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< /code>),仅没有任何数据已传递(无关键字)。

编辑 #2

我正在 Drupal API 和核心搜索模块中进行深入研究,只是为了看看它是如何工作的。这是 /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]);
    }

请注意定义 $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]);
  }
}

I am altering the value of the form action for the basic search form on a Drupal site. Here is the line I added to our hook_form_alter() implementation in our custom search module:

$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:

/**
* 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']); 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).

Edit #2

I am digging around in the Drupal API and in the core search module just to see how it works. This is the core search_box_form_submit() function from our /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]);
    }

Note the line where $form_state['redirect'] is defined. If I comment this line out, then the search form will land on the search/mymodule/ page, although without any search keywords passed.

Edit - Working Code

The final solution is an altered version of nmc's answer. I had to alter his mymodule_form_submit() implementation. The function should be named mymodule_submit() and the code within the function needed to be altered as well:

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

深海蓝天 2024-11-23 23:19:08

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

Try changing the submit function which the form calls, instead of changing the URL of the form's action:

/**
* 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.

醉殇 2024-11-23 23:19:08

我相信您想更新 $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,/search 页面上有一个搜索框 (Search #2),第三个搜索框位于 /search /node/whatever 结果页面(搜索#3)。

现在,我刚刚看到的是,更新 $form['#action'] 在搜索 #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 种情况下工作了!

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

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. Therefore, solution not satisfactory, let's try a different approach then.

Let's add our own #submit function to form in question:

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!

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文