Hello Form - a Hello World With a Form

As Drupal is designed to be a fantastic platform for community based websites it stands to reason that one of the first things a Drupal developer will need to develop is a form. Here then is a very simple Hello World with a form. This just asks the user to select from one of 3 greetings with a radio button, and echos the greeting back to them. No fancy DB calls, just a little form with a teensy bit of processing.

As usual, easy to install. I built these 3 files all in one folder, uploaded them all to a directory on my server as "sites/all/modules/hello_form". Then to turn this moudle on I went into Home >> Adminnister >> Site building >> Modules, and clicked the little check box to enable this.

As has been the case, my code indenting doesn't seem to be showing up in the blog, so I'll have to look into that at some point.

README.txt


HELLO_FORM MODULE
-------------

hello_form.module is a "Hello World" form module for Drupal,
and is meant to be the simplest possible module with a form
that I could build.

I used "Pro Drupal Development" by Van Dyk and Westagate
as well as the Drupal form APIs as references.
(http://api.drupal.org/api/file/includes/form.inc/6)

see this run at: http://www.drupnewb.com/?q=hello_form

to install put files in a "hello_form" directory under
/sites/all/modules,
and turn on in admin at Administer >> Site Building >> Modules

Author:
Larry Truett

hello_form.info


; $Id$
name = Hello Form
description = "a hello world Drupal module with a form."

hello_form.module

<?php
// $Id$

/**
 * @file
 * a hello world Drupal module with a form
 * 
 * the simplest possible module with a form I could create
 */

/**
 * Implementation of hook_menu().
 */
function hello_form_menu($may_cache) {
  $items = array();

  if ($may_cache) {
  } else {
    $items[] = array(
      'path' => 'hello_form',
      'title' => t('Hello Form'),
      'description' => t('Hello Form.'),
      'callback' => 'hello_form_page',
      'access' => TRUE,
      'weight' => 10
    );
  }
  return $items;
}

/**
 * Implemenation of page.
 */
function hello_form_page() {
  return drupal_get_form('hello_form_form');
}

/**
 * The form.
 */
function hello_form_form() {
  $form['greeting'] = array(
    '#type' => 'fieldset',
    '#title' => t('Select a Greeting')
  );

  $form['greeting']['theGreeting'] = array(
    '#type' => 'radios',
    '#options' => array(
      t('Hello'),
      t('Hi'),
      t('Whats Up')
    )
  );
  
  $form['greeting']['submit'] = array(
    '#type' => 'submit',
    '#value' => t('Greet Me!')
  );
  
  return $form;
}

/**
 * Called when the form is submit.
 */
function hello_form_form_submit($form_id, $form_values) {
  $greeting_no = $form_values['theGreeting'];
  switch ($greeting_no) {
    case '0': $greeting = 'Hello'; break;
    case '1': $greeting = 'Hi'; break;
    case '2': $greeting = 'Whats Up'; break;
  }
  drupal_set_message (t($greeting.' DrupNewb!'));
}

/**
 * Implementation of help.
 */
function hello_form_help($section) {
  switch ($section) {
    case 'admin/help#hello_form':
      return 'hello form help';
  }
}

(note: the above module didn't show proper indenting with the <code> tag, but does with the <pre> tag.)