Login

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 {
         $mp->setAlias($form_state['post']['alias']);
         $mp->setId($form_state['post']['id']);
         $mp->setUid($form_state['post']['uid']);
    }
 
    $submit_value = '';
    switch($op) {
        case 'modify' :
            $submit_value = t('Save Changes');
            drupal_set_title(t("Edit My Profile Alias"));
        break;
 
        case 'delete' :
            $submit_value = t('Confirm Delete');
            drupal_set_title(t("Delete My Profile Alias"));
        break;
    }
 
    $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(
        '#type'=>'textfield',
        '#title'=>t('Alias'),
        '#required' => TRUE,
        '#default_value'=>$mp->getAlias(),
        '#maxlength'=>80,
        '#size'=>60,
        '#weight'=>$weight++);
 
    $form['id'] = array(
      '#type'=>'hidden',
      '#value'=>$mp->getId());
 
    $form['uid'] = array(
      '#type'=>'hidden',
      '#value'=>$mp->getUid());
 
     $form['contact']['cancel']= array(
        '#prefix'=>'<table><tr><td>',
        '#suffix'=>'</td>',
        '#type'=>'submit',
        '#value'=> t('Cancel'),
        '#weight'=>$weight);
 
    $form['contact']['submit']= array(
        '#prefix'=>'<td>',
        '#suffix'=>'</td></tr></table>',
        '#type'=>'submit',
        '#value'=> $submit_value,
        '#weight'=>$weight);
 
    return $form;
 }
 
function modify_my_profile_form_submit($form, &$form_state) {
    switch($form_state['values']['op']) {
        case 'Cancel' :
            drupal_set_message('Operation Cancelled');
        break;
        case 'Save Changes' :
            $dbResult =  updateAlias(&$form_state);
            if ($dbResult) {
                drupal_set_message(
                   $form_state['values']['alias'].
                  '  Updated Successfully');
            } else {
                drupal_set_message(
                   'Unexpected Exception: Unable to update ' .
                        $form_state['values']['alias']);
            }
        break;
        case 'Confirm Delete' :
           $uid = $form_state['values']['uid'];
           $dbResult = deleteAlias($uid);
           if ($dbResult) {
                drupal_set_message('My Profile Alias '.
                        $form_state['values']['alias'].
                           ' Deleted Sucessfully');
           } else {
                drupal_set_message(
                    'Unexpected Exception: Unable to Delete '.
                        $form_state['values']['alias']);
           }
        break;
    }
}
 
/* 
  * Utility method updates my profile 
  */
function updateAlias(&$form_state) {
    $dbProfile = new dbMy_profile();
    $myProfile = new my_profile();
    $myProfile->setAlias($form_state['values']['alias']);
    $myProfile->setId($form_state['values']['id']);
    $myProfile->setUid($form_state['values']['uid']);
    $dbResult = $dbProfile->update($myProfile);
    return  $dbResult;
}

Explanation:

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