My Profile - Proof of Concept Module

This is another installment of our series on beginning module development for Drupal

We'll build a "proof of concept" module called "My Profile" which demonstrates the following:

  • How to customize the User Registration form (add an optional field).
  • How to create a CRUD maintenance table (Add, Modify, Delete).
  • How to use Ajax with Drupal's Form API.
  • How to implement a "Cancel" button of Drupal form which includes a required field.
  • Create a Administrator's screen to manage our custom type.
  • Customize the "My Account" dialog (User Profile Form), so that user's can add, update or delete their own custom profile field.
  • A simple object pattern for PHP and Drupal.

Our module name is "My Profile".

Customizing the Drupal Account Registration Dialog (form).

Drupal provides "hook" functions which allow modules to override default Drupal behavior. In our example, we want to override the default Account Registration form, so we implement hook_form_alter

function my_profile_form_alter(&$form, &$form_state, $form_id) {
    switch($form_id) {
        case 'user_register' :
               $form['my_fs'] = array(
                    '#type'=> 'fieldset',
                    '#title'=>t('Your Special Alias'),
               $form['my_fs']['alias']= array(
                    '#title'=> t('My Alias'),
                    '#description'=>t('A short name 
                         used to disguise your identity'),

Explanation: Drupal will call this function each time a form is being built. We identify the registration form by it's "form_id". The following is the html source of a default Drupal account registration form:

<input id="edit-user-register" type="hidden" value="user_register" name="form_id"/>

The following is a screen shot of what the customized registration form looks like:

Customized User Registration Form (Click to Enlarge Image Display)