开发者

Create a VERY simple form in Drupal

开发者 https://www.devze.com 2023-01-11 22:53 出处:网络
All I need to do is have a form that 开发者_JAVA技巧does this: User enters postcode in text box Upon submit user is redirected to mysite.com/[user postcode]

All I need to do is have a form that 开发者_JAVA技巧does this:

  1. User enters postcode in text box
  2. Upon submit user is redirected to mysite.com/[user postcode]

That's it! I know validation etc. would be desirable as well, but I just need to get this working for now. I don't mind if it's harcoded or utilises the Drupal form API (actually I'd prefer the former!).

I know this is dead simple, but unfortunately I'm coming from a front-end background and have a bit to learn about this sort of thing :(

Cheers!


This is pretty easy with the Form API and a custom module. You'll build a form using the Form API and add a submit handler that changes the redirect for the form to whatever you'd like. Finally, you'll need to create a way to access the form (either by creating a menu item or by creating a block).

Here's an example that implements a form like you want: you'll want to peruse the Form API reference to see all the options you have when building a form. It also provides two ways to access the form:

  1. Using hook_menu() to provide a page for the form at http://example.com/test
  2. Using hook_block() to provide a block containing the form that you can add and move around on the block administration page.

Example code:

// Form builder. Form ID = function name
function test_form($form_state) {

  $form['postcode'] = array(
    '#type' => 'textfield',
    '#title' => t('Postcode'),
    '#size' => 10,
    '#required' => TRUE,
  );
  $form['submit'] = array(
    '#type' => 'submit',
    '#value' => t('Go'),
  );

  return $form;
}

// Form submit handler. Default handler is formid_submit()
function test_form_submit($form, &$form_state) {
  // Redirect the user to http://example.com/test/<Postcode> upon submit
  $form_state['redirect'] = 'test/' . check_plain($form_state['values']['postcode']);
}

// Implementation of hook_menu(): used to create a page for the form
function test_menu() {

  // Create a menu item for http://example.com/test that displays the form
  $items['test'] = array(
    'title' => 'Postcode form',
    'page callback' => 'drupal_get_form',
    'page arguments' => array('test_form'),
    'access arguments' => array('access content'),
    'type' => MENU_NORMAL_ITEM,
  );

  return $items;
}

// Implementation of hook_block(): used to create a movable block for the form
function test_block($op = 'list', $delta = 0, $edit = array()) {
  switch ($op) {
    case 'list': // Show block info on Site Building -> Blocks
      $block['postcode']['info'] = t('Postcode form');
      break;
    case 'view':
      switch ($delta) {
        case 'postcode':
          $block['subject'] = t('Postcode');
          $block['content'] = drupal_get_form('test_form');
          break;
      }
      break;
  }

  return $block;
}

More info:

  • Creating modules - a tutorial: Drupal 6.x
  • Form API Quickstart Guide
  • Form API Reference
  • hook_menu() API reference
  • hook_block() API reference
  • Example of hook_block in Drupal 6


The Drupal Form API -is- dead simple and its something you need to learn as a developer eventually. Might as well jump in and do it through the API since its not too hard, what you are trying to do.


Creating forms in Drupal is fairly easy once you get the hang of it. I would recommend reading the following link. http://drupal.org/node/751826 It gives a good overview of how to create the form.

In the _submit hook, you can then redirect to the appropriate page by setting $form_state['redirect'].

This is assuming of course that you already have the hang of creating custom modules. If you need more info on that, go here.

0

精彩评论

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