Login

My Profile - Modiying the standard My Account/User Profile

In this post we'll modify the standard "My Account" user/profile menu and edit forms. We'll code the routines that allow users to edit their own "my profile alias".

In out previous post we discussed the fact that an administrator can turn this feature on or off. In other words, our code needs to check to see what the current system settings are before we allow a user to edit their alias.

First lets add our menu item. Note, we are appending the standard path "user". The following item is added to our hook_menu() impementation:

 $items['user/%/alias'] = array(
        'title' => 'Your Alias',
        'load arguments'=>array(3),
        'page callback' => 'drupal_get_form',
        'page arguments'=>array('my_profile_user_form',1),
        'access arguments' => array('access content'),
        'type' => MENU_LOCAL_TASK|MENU_CALLBACK,
    );

Note "MENU_LOCAL_TASK" instructs Drupal to create a "tab" for our menu item and form.

Next let's implement hook_form():

function my_profile_user_form(&$form_state,$uid) {
 
   if (!variable_get('my_profile_user',1)) {
       drupal_set_message("Registration Is Turned Off");
       return;
    }
    $mp = new my_profile();
    $submit_operation = '';
    if (count($form_state['post']) == 0) {
         $dbMp = new dbMy_profile();
         $mp = $dbMp->getByUid($uid);
    } else {
         $mp->setAlias($form_state['post']['alias']);
         $mp->setId($form_state['post']['id']);
         $mp->setUid($form_state['post']['uid']);
    }
 
    if ( is_null($mp->getId()) || $mp->getId()=='' ){
       $submit_operation = 'Add';
    } else {
       $submit_operation = 'Update';
    }
 
    $weight = 0;
 
    $form['contact'] = array(
        '#type' => 'fieldset',
        '#title' => t('My Profile Alias Setting'),
        '#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'=> is_null($mp->getId()) ? '' : $mp->getId(),
     );
 
    $form['uid'] = array(
      '#type'=>'hidden',
      '#value'=>$uid);
 
     $form['contact']['submit']= array(
        '#prefix'=>'<table><tr><td>',
        '#suffix'=>'</td>',
        '#type'=>'submit',
        '#theme' => 'my_profile_button',
        '#value'=> $submit_operation,
        '#weight'=>$weight);
 
 
     $form['contact']['delete']= array(
        '#prefix'=>'<td>',
        '#suffix'=>'</td></tr></table>',
        '#type'=>'submit',
        '#value'=> t('Delete'),
        '#weight'=>$weight);
 
 
    return $form;
 }

And now our submit handler:

function my_profile_user_form_submit($form, &$form_state) {
    switch($form_state['values']['op']) {
        case 'Add' :
            $mp = addAlias($form_state['values']['alias'],$form_state['values']['uid']);
 
            drupal_set_message('Added Alias Successfully');
        break;
        case 'Update' :
            $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 '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;
    }
}

Explanation:

We are going to have some folks who never entered a "my profile alias" when they first created their account. Therefore, for those folks, we need to add a new record. Thus at the top of the form creation, we check the database for a my_profile record for the current user. If we don't find a record we know our database operation is going to be an add. If we do find a record, we display it and we also know our database operation is going to be a update.

We also supply a delete button. Each button has a "name" of "op". Thus, the submit handler checks to see if the user clicked the "update" button or "delete" button. If the user pressed the delete button we know our database operation is a delete operation