2

I've created a custom module to add several submission buttons at the bottom of the form. One submit button should redirect users to their Profile2 profile. The other will send them to the user administration section.

The Profile2 page has a submit button to send them to the user account edit page, or to the user administration section. I was able to do this with rules, but the site admin didn't like that there weren't buttons.

So I've created a custom module to handle this, but the problem is that the data is not saved when one of the custom submission handlers is called.

//redirects to account and from profile are working but submissions not saving

function MY_MODULE_redirect_form_alter(&$form, &$form_state, $form_id) {
  if ($form_id == 'user_profile_form') {
    if (arg(3) == 'coach'){
        $form['actions']['profile'] = array(
        '#type' => 'submit',
        '#submit' => array('MY_MODULE_redirect_account'),
        '#value' => t('Save and go to Account'));
        $form['actions']['submit'] = array(
        '#type' => 'submit',
        '#submit' => array('MY_MODULE_redirect_admin'),
        '#value' => t('Save and go to Administration'));
    }
    else {
    $form['actions']['profile'] = array(
      '#type' => 'submit',
      '#submit' => array('MY_MODULE_redirect_submit'),
      '#value' => t('Save and go to Profile'));
    $form['actions']['submit'] = array(
      '#type' => 'submit',
      '#submit' => array('MY_MODULE_redirect_admin'),
      '#value' => t('Save and go to Administration'));
    }
  }
}



function MY_MODULE_redirect_submit(&$form, &$form_state) {
    $user = $form_state['user'];
    $form_state['redirect'] = 'user/' . $user->uid . '/edit/coach' ;
    drupal_set_message('The user account has been saved');
}

function MY_MODULE_redirect_admin(&$form, &$form_state) {
    $user = $form_state['user'];
    $form_state['redirect'] = 'admin/MY_MODULE-user';
    drupal_set_message('Your submission is complete');
}

function MY_MODULE_redirect_account(&$form, &$form_state) {
  $user = $form_state['user'];
  $form_state['redirect'] = 'user/' . $user->uid . '/edit';
  drupal_set_message('The coach profile has been saved');
}
5

You are altering user_profile_form. By doing this

$form['actions']['profile'] = array(
      '#type' => 'submit',
      '#submit' => array('MY_MODULE_redirect_submit'),
      '#value' => t('Save and go to Profile'));

you have dissconnected default submit handler which save the data.

// Add the final user profile form submit handler.
  $form['#submit'][] = 'user_profile_form_submit';

So you need to append your submit handler with default submit handler

  • I'm unable to get this to work even with the appending the default handler. I get a Fatal error: Call to undefined function user_profile_form_submit(). I'm going to continue to investigate, I may have to build out my own custom user_save() function in the end. – LWal Dec 14 '12 at 21:53
  • The code that I have written, is just an idea. How to append your submit handler with default submit. you would need to find out the default submit handler by printing form array. – Ankit Chauhan Dec 17 '12 at 5:23
  • Thanks I was able to figure out how to handle the submission thanks to your help. I posted a longer answer here as well. – LWal Dec 17 '12 at 19:01
  • cool....finally you got it. – Ankit Chauhan Dec 18 '12 at 5:17
0

I was able to figure it out thanks to Ankit's recommendation. I had to pass $form and $form_state to the default submit handlers user_profile_form_submit($form, $form_state) for the user account and profile2_form_submit_handler($form, $form_state) for profile2. I also had to separate out the standard submission to save the proper form fields.

Here's the code that I used

//redirects to account and from profile are working but submissions not saving

function MY_MODULE_redirect_form_alter(&$form, &$form_state, $form_id) {
  if ($form_id == 'user_profile_form') {
    if (arg(3) == 'coach'){
        //handes profile2 submissions
        $form['actions']['profile'] = array(
        '#type' => 'submit',
        '#submit' => array('MY_MODULE_redirect_account'),
        '#value' => t('Save and go to Account'));
        $form['actions']['submit'] = array(
        '#type' => 'submit',
        '#submit' => array('MY_MODULE_redirect_profile_admin'),
        '#value' => t('Save and go to Administration'));
    }
    else {
    //handles user account submissions
    $form['actions']['profile'] = array(
      '#type' => 'submit',
      '#submit' => array('MY_MODULE_redirect_submit'),
      '#value' => t('Save and go to Profile'));
    $form['actions']['submit'] = array(
      '#type' => 'submit',
      '#submit' => array('MY_MODULE_redirect_user_admin'),
      '#value' => t('Save and go to Administration'));
    }
  }
}

//redirect from profile2 to custom URL

function MY_MODULE_redirect_profile_admin(&$form, &$form_state) {
    $user = $form_state['user'];
    profile2_form_submit_handler($form, $form_state);
    $form_state['redirect'] = 'admin/MY_CUSTOM_REDIRECT';
    drupal_set_message('Your submission is complete');
}

//redirect from profile2 to user account

function MY_MODULE_redirect_account(&$form, &$form_state) {
  $user = $form_state['user'];
  profile2_form_submit_handler($form, $form_state);
  $form_state['redirect'] = 'user/' . $user->uid . '/edit';
  drupal_set_message('The coach profile has been saved');

//redirect from account to profile2

function MY_MODULE_redirect_submit(&$form, &$form_state) {
    $user = $form_state['user'];
    user_profile_form_submit($form, $form_state);
    $form_state['redirect'] = 'user/' . $user->uid . '/edit/coach' ;
    drupal_set_message('The user account has been saved');
}

//redirect from account to custom URL

function MY_MODULE_redirect_user_admin(&$form, &$form_state) {
    $user = $form_state['user'];
    user_profile_form_submit($form, $form_state);
    $form_state['redirect'] = 'admin/MY_CUSTOM_REDIRECT';
    drupal_set_message('Your submission is complete');
}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.