Create a VERY simple form in Drupal

All I have to do is have a form that does this:

  • User enters a zip code in a text field
  • After sending, the user is redirected to the zip code mysite.com/[user]

What is it! I know that checking, etc. It would also be desirable, but I just need to do it now. I do not mind if it is encoded or uses the Drupal API (in fact, I would prefer the first!).

I know that this is dead simple, but, unfortunately, I come from the external interface and learn a little about it :(

Hurrah!

+4
source share
3 answers

This is pretty easy with the Form API and user module . You will create the form using the forms API and add a submit handler that will change the redirection for the form, whatever you want. Finally, you need to create a way to access the form (either by creating a menu item or by creating a block).

Here is an example that implements the form as you want: you will want to familiarize yourself with the link to the form API to view all the parameters that you have when creating the form. It also provides two ways to access the form:

  • Using hook_menu() to provide the form page http://example.com/test
  • Using hook_block() , you will get a block containing a form that you can add and move on the block administration page.

Code example:

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

Additional Information:

+4
source

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

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

This assumes, of course, that you already have the ability to create custom modules. If you need more information about this, go here .

+2
source

The Drupal Form -is-dead API is simple and its what you need to learn as a developer in the end. You can also jump in and do it via the API, since it’s not too complicated what you are trying to do.

+2
source

All Articles