Creating a custom form page in Drupal 6 with a module

Dev

Creating a custom form page in Drupal 6 with a module

Webforms are great when you want to make a quick and useful form in Drupal. However, sometimes we may want a little more control over that form or need to run some custom PHP code when the form is submitted. Keep in mind, we can always use hook_form_alter() to modify Webforms and mold them to our liking, but if you want 100% control over all of the elements, submit functionality, and inner workings, a full custom module may be the way to go.

What We Want

  • A custom form using a custom module
  • The form to be on its own page within our Drupal theme
  • The ability to control all elements and submit functionality

What We Need

  • Drupal 6
  • Custom PHP code

What We Do

Start off by creating the custom module, which includes the .info and .module files. Name it whatever you want, preferably something intuitive and relevant to your functionality. The hooks we will be using are hook_perm(), hook_menu(), hook_form(), and hook_form_submit().

The first function in our module file will be hook_perm(). This will basically give you control over which roles may access this form. You will use some code like this:

function myform_perm() {   
  return array('access myform content');
}

Next we will need to register the path for our form page. To do this we use hook_menu(). The code below will set up our form to display at the url “www.mysite.com/myform” and register it as a link in the Navigation menu. It will also set up the callbacks and arguments necessary to get the form on the page and hook up the permissions we set earlier.

function myform_menu() {     
  $items = array();
  $items['myform'] = array(
    'title' => t('My Form'),
    'page callback' => 'drupal_get_form',
    'page arguments' => array('myform_form'),
    'access arguments' => array('access myform content'),
    'type' => MENU_NORMAL_ITEM,
  );
  return $items;
}

Now we need to actually create our form. To do this we use hook_form() like below. We will just set up a simple input message box and a submit button:

function myform_form() { 
  $form['message'] = array(
    '#type' => 'textarea',
    '#title' => t('Message'),
    '#required' => TRUE,
  );
  $form['submit'] = array(
    '#type' => 'submit',
    '#value' => t('Send')
  );
  return $form;
}

The last thing we need to do as far is coding is now our hook_form_submit()! Inside this function, we can set up any sort of custom coding we’d like.

function myform_form_submit($form, &$form_state) {   
  // Here is where your submit functionality should go.
}

Finally, the custom module is complete. Now all you have to do is activate the module from the admin menu and enjoy the new form page!

Using this method gives you just about all the control you could want or need over your form page. You can do many things with your form page from within the hook_form_submit(), whether that’s taking some input and storing it in your database or sending out an email message you input to certain users or just about anything else.

More About the Author

Justin Lemmon

Analytics Consultant
Adding a CSV Export button to Drupal 6 Views displays Views is something that a lot of Drupal developers (myself included) likely could not do without. It provides a great way for us to ...
Creating a custom form page in Drupal 6 with a module Webforms are great when you want to make a quick and useful form in Drupal. However, sometimes we may want a little more control over ...

See more from this author →

Subscribe to our newsletter

  • I understand that InterWorks will use the data provided for the purpose of communication and the administration my request. InterWorks will never disclose or sell any personal data except where required to do so by law. Finally, I understand that future communications related topics and events may be sent from InterWorks, but I can opt-out at any time.
  • This field is for validation purposes and should be left unchanged.

InterWorks uses cookies to allow us to better understand how the site is used. By continuing to use this site, you consent to this policy. Review Policy OK

×

Interworks GmbH
Ratinger Straße 9
40213 Düsseldorf
Germany
Geschäftsführer: Mel Stephenson

Kontaktaufnahme: markus@interworks.eu
Telefon: +49 (0)211 5408 5301

Amtsgericht Düsseldorf HRB 79752
UstldNr: DE 313 353 072