My Profile - Maintenance Forms and Handlers

In our previous post we constructed a maintenance table. That allows our administrators to either add a new alias, modify a current alias, or delete a current alias.

In this post we will detail the edit and delete current alias form and routines.

We will implement hook_form() to display a form that either modifies or deletes a current user alias.

We also implement a submit handler to perform the actual modify or delete. Drupal makes heavy use of function and module names to identify and invoke. By simply adding a function name that suffixes our form name with "_submit", Drupal will automatically call that function when the user submits the form.

The code below provides us with the details:

function modify_my_profile_form(&$form_state,$id,$name,$op) {
    $mp = new my_profile();
    if (count($form_state['post']) == 0) {
         $dbMp = new dbMy_profile();
         $mp = $dbMp->get($id);
    } else {
    $submit_value = '';
    switch($op) {
        case 'modify' :
            $submit_value = t('Save Changes');
            drupal_set_title(t("Edit My Profile Alias"));
        case 'delete' :
            $submit_value = t('Confirm Delete');
            drupal_set_title(t("Delete My Profile Alias"));
    $weight = 0;
    $form['#redirect'] = 'my_profile/maintenance';
    $title = 'Alias settings for '.$name;
    $form['contact'] = array(
        '#type' => 'fieldset',
        '#title' => t($title),
        '#weight' => 5,
        '#collapsible' => FALSE,
        '#collapsed' => FALSE,
    $form['contact']['alias'] = array(
        '#required' => TRUE,
    $form['id'] = array(
    $form['uid'] = array(
     $form['contact']['cancel']= array(
        '#value'=> t('Cancel'),
    $form['contact']['submit']= array(
        '#value'=> $submit_value,
    return $form;
function modify_my_profile_form_submit($form, &$form_state) {
    switch($form_state['values']['op']) {
        case 'Cancel' :
            drupal_set_message('Operation Cancelled');
        case 'Save Changes' :
            $dbResult =  updateAlias(&$form_state);
            if ($dbResult) {
                  '  Updated Successfully');
            } else {
                   'Unexpected Exception: Unable to update ' .
        case 'Confirm Delete' :
           $uid = $form_state['values']['uid'];
           $dbResult = deleteAlias($uid);
           if ($dbResult) {
                drupal_set_message('My Profile Alias '.
                           ' Deleted Sucessfully');
           } else {
                    'Unexpected Exception: Unable to Delete '.
  * Utility method updates my profile 
function updateAlias(&$form_state) {
    $dbProfile = new dbMy_profile();
    $myProfile = new my_profile();
    $dbResult = $dbProfile->update($myProfile);
    return  $dbResult;


By default Drupal forms post back to themselves. If you've ever created a form in html and not set the "action" method of your form that's exactly what happens.

So the first time the form is called, we go to database and get the current record image. In the case of a modify, the second time the form is called we want to use the modified image of the record (what the user enter in the text field).

We set the form's redirect attribute so that the maintenance screen is displayed upon a submit. The premise is the administrator normally does bulk work (works on multiple records). That's why we redirect back to our table.

Note! This routine could be improved by doing 2 things. One set the required attribute on the alias field to false. Next, add a validate handler which says "if you are doing a modify, then the alias must have a value". Currently, it's possible that the user blanks out the alias value then clicks "Cancel". The default required validation is kicked off and the user gets a confusing message.

We'll keep the routine as is, just for brevity sake

The following screen shot show us a

  • Delete form
  • Delete canceled
  • Edit form
  • Edit completed

In our next post part 7, we have a much more interesting set of issues. Part 7 will detail the administrator's add form and handlers. We want to display list of user's who don't have an alias assigned. We'll use a little Ajax trick to render our add form