drupal - 创建自定义搜索表单 drupal

标签 drupal search forms

我想在 block 中的 drupal 主题上添加自定义搜索选项。它将是一个带有文本框和几个复选框的表单。表单在提交时要做的就是..根据复选框状态生成搜索 URL。

http://localhost/restaurant/search/node/type:restuarant category:34,38 %keyword%

关键字将是搜索框中的文本,类别将根据复选框状态添加。我知道在普通的 php 站点中执行此操作,但不知道如何在我的 drupal 主题中实现此操作。

我检查了表单 api,我了解了如何在模块中创建表单...并通过像

这样的 url 访问它

http://localhost/restaurant/my_module/form

但没有得到任何关于如何将其放入模板中的 block 中的线索。

最佳答案

实现hook_block() ,使用 $form['#submit'] 在表单中设置自定义提交处理程序,然后在自定义提交处理程序中,将 $form_state['redirect'] 设置为您的自定义 URL。示例:

function mymodule_block($op = 'list', $delta = 0, $edit = array()) {
  $block = array();

  switch ($op) {
    case 'list':
      $block[0]['info'] = t('Custom search form');
      break;
    case 'view':
      switch ($delta) {
        case 0:
          $block['subject'] = t('Custom search');
          $block['content'] = drupal_get_form('mymodule_custom_search_form');
          break;
      }
      break;
  }

  return $block;
}

function mymodule_custom_search_form($form_state) {
  $form = array();

  $form['keyword'] = array(
    '#type' => 'textfield',
    '#title' => t('Keyword'),
    '#required' => TRUE,
  );
  $form['category'] = array(
    '#type' => 'textfield',
    '#title' => t('Categories'),
    '#required' => TRUE,
  );
  $form['type'] = array(
    '#type' => 'textfield',
    '#title' => t('Type'),
    '#required' => TRUE,
  );
  $form['submit'] = array(
    '#type' => 'submit',
    '#value' => t('Search'),
  );

  $form['#submit'] = array('mymodule_custom_search_form_submit');

  return $form;
}

function mymodule_custom_search_form_submit($form, &$form_state) {
  $redirect_url = 'search/node/';
  $redirect_url .= 'type:' . $form_state['values']['type'];
  $redirect_url .= ' category:' . $form_state['values']['category'];
  $redirect_url .= ' %' . $form_state['values']['keyword'] . '%';

  $form_state['redirect'] = $redirect_url;
}

关于drupal - 创建自定义搜索表单 drupal,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3368377/

相关文章:

ruby-on-rails - 在 Ruby on Rails 上从多个字段搜索项目名称

javascript - 在 Javascript 中检索表单的 "name"属性,而名称为 "name"的输入存在

javascript - 在检查值时提交带有链接的表单

php - 根据选择框的值更改图像大小

c# - LINQ 查询以从多个列表中按条件过滤项目

php - Bash 脚本为分布在多个版本的服务器上的多个 Drupal 站点更新管理员用户密码

c# - 从列表框中搜索和删除项目

html - 在 Bootstrap 3 中嵌套在水平表单中的内联表单

Drupal 7 中的 JSON 提要上下文

json - Drupal 创建一个输出 JSON 的页面