开发者

Create a custom search form drupal

开发者 https://www.devze.com 2023-01-09 18:07 出处:网络
I want to add a custom search option on my drupal theme in a block. It will be a form with a text box and few checkboxes. All开发者_JS百科 that the form has to do while submitting is.. generate a sear

I want to add a custom search option on my drupal theme in a block. It will be a form with a text box and few checkboxes. All开发者_JS百科 that the form has to do while submitting is.. generate a search url based on the checkbox state.

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

The keyword will be the text in the search box and category will be added according to the checkbox state. I know to do this in an ordinary php site but have no idea how to implement this in my drupal theme.

I Checked the form api, I understood about creating a form in a module... and accessing it via a url like

http://localhost/restaurant/my_module/form

But didn't get any clue on how i could put it in a block in my template.


Implement hook_block(), set up a custom submit handler in your form using $form['#submit'], and in your custom submit handler, set $form_state['redirect'] to your custom URL. Example:

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

精彩评论

暂无评论...
验证码 换一张
取 消