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'),

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");
    $mp = new my_profile();
    $submit_operation = '';
    if (count($form_state['post']) == 0) {
         $dbMp = new dbMy_profile();
         $mp = $dbMp->getByUid($uid);
    } else {
    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(
        '#required' => TRUE,
    $form['id'] = array(
      '#value'=> is_null($mp->getId()) ? '' : $mp->getId(),
    $form['uid'] = array(
     $form['contact']['submit']= array(
        '#theme' => 'my_profile_button',
        '#value'=> $submit_operation,
     $form['contact']['delete']= array(
        '#value'=> t('Delete'),
    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');
        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 ' .
        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 '.


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