Drupal Module Best Practice: Form Data

picture

Drupal Module Best Practice: Form Data

Drupal Module Best Practice: Form Data

When coding modules there are certain best practices you should follow. These practices will keep you module secure, extensible, translatable, and clean. In this post we'll look at creating forms that are filled out with data from the DB.

One of the most common tasks in module development is creating an add/edit form. They are used in core and contributed modules all over the place. When they are in the edit state there is data that has to be passed into to them for default values. All too often though there is a part of this process that gets overlooked by developers: storing the data object as a form value.

Here is some code from a custom module I was recently working on.

<?php
function mymod_zip_form($form$zip NULL)  {
  if (!
$zip) {
    
$zip 'add';
  }
  
$region NULL;
  if (
is_numeric($zip)) {
    
$sql "SELECT * FROM {mymod_regions} WHERE zip = %d";
    
$res db_query($sql$zip);
    
$region db_fetch_object($res);
  }
  
$form['zip'] = array(
    
'#type' => 'textfield',
    
'#title' => t('Zip Code'),
    
'#default_value' => $region->zip,
    
'#size' =>  10,
  );
  
$form['city'] = array(
    
'#type' => 'textfield',
    
'#title' => t('City'),
    
'#default_value' => $region->city,
    
'#size' =>  30,
  );
  
$form['county'] = array(
    
'#type' => 'textfield',
    
'#title' => t('County'),
    
'#default_value' => $region->county,
    
'#size' =>  30,
  );
  
$form['rid'] = array(
    
'#type' => 'textfield',
    
'#title' => t('Region Code'),
    
'#default_value' => $region->rid,
    
'#size' =>  5,
  ); 
  
$form['submit'] = array('#type' => 'submit''#value' => t('Add'));
  return 
$form;
}
?>

You'll notice this form does two things before it returns the form: Checks if a zip code has been passed, and gets the data for that zip if it exists. Something that is missing from the current code though is storing the data object as a form element.

<?php
  $form
['region'] = array(
    
'#type' => 'value',
    
'#value' => $region,
  );
?>

Why do we need to do this? Because Drupal exposes all forms to hook_form_alter. If I was to come along and want to write a module that alters this form and adds extra data fields or whatnot, I need to be able to access the data object related to that form. Now if you don't do this does it make your form not work? No. Will it make it impossible for other developers to hook into your form? Not always. But it is considered the proper way to build out your forms. It will make life for others easier.

Even if you are just writing a custom module that you don't think anyone else will ever see, it's a good idea to follow best practices and coding standards. First off it keeps you doing things the right way. Secondly you never know who will come behind you to work on the code. It's like the old video rental saying "Be Kind, Rewind": make life easier for those who will use it after you.

Happy Coding!

Add Your Comment

The content of this field is kept private and will not be shown publicly.
Image CAPTCHA
Enter the characters shown in the image.

Search Site

Latest Posts

picture
One of Drupal longtime woes has been it’s GUI. It has...
picture
Long time users of Drupal will be familiar with Fields...
picture
I use SSH on an hourly basis and am often logging into...
picture
One of the most common tasks in module development is...

Get In Touch

Feel free to contact me about how I can help with your next Web project.
Telephone: 910-808-1717
E-mail: info@adamagregory.com