0

I like to redirect my users after they submit their user/edit page. On their user/edit submit i like to remove a certain role from them as well. The redirect hasn't been a problem however removing the role is.

In a custom module i have the following code:

  function regform_user_profile_form_submit($form, &$form_state) {
  global $user; 
 drupal_goto('user/' . $user->uid);

I am quit a beginner with php and couldn't find any could that fitted in this function properly. Using rules is not an option as it conflicts with the Oauth module.

Help appreciated, thanks.

3 Answers 3

0

I would try implementing hook_user_presave().

function custom_module_user_presave($edit, $account, $category) {
  $role_position = array_search('undesired_role', $account->roles);
  // Use !== or === here since $role_position may be zero or FALSE
  if ($role_position !== FALSE) {
    unset($account->roles[$role_position]);
  }
}
1
  • thanks for your comment. However i am having some trouble implementing your code. This is probably due to a earlier if statement which skips the new code. See my module underneath
    – Byron
    Mar 18, 2014 at 7:55
0
   <?php
function regform_form_user_profile_form_alter(&$form, $form_state,  $form_id) {
  global $user;
 // Making password required when role=In between role.
  if( ($form_id == 'user_profile_form') and (isset($form['account']['pass'])) and (in_array('In between role', array_values($user->roles))) ){
    $form['account']['pass']['#type'] = 'password';
    $form['account']['pass']['#required'] = TRUE;
    $form['account']['pass']['#type'] = 'password_confirm';
    $form['account']['pass']['#required'] = TRUE;
  }

// Using regform_user_profile_form_submit to redirect users with Oauh role
    elseif(($form_id === 'user_profile_form')and (isset($form['account']['pass'])) and (in_array('Oauth role', array_values($user->roles))) ) {
    $form['#submit'][] = 'regform_redirect_callback';

    }
}
function regform_redirect_callback($form, &$form_state) {
  $form_state['redirect'] = 'user/';


  }  

function regform_user_presave($edit, $account, $category) {
  $role_position = array_search('Oauth_role', $account->roles);
  // Use !== or === here since $role_position may be zero or FALSE
  if ($role_position !== FALSE) {
    unset($account->roles[$role_position]);

  }
}

?>
3
  • 1
    For a start, functions nested within functions will only be in scope to the code within the parent function. Take the regform_form_user_presave() and regform_user_profile_form_submit() functions out so they're global and you may have some better luck.
    – alexkb
    Mar 18, 2014 at 12:35
  • 1
    Secondly, you want to avoid doing a drupal_goto() in the regform_user_profile_form_submit() because that may stop the existing user_profile_form submit handler from executing. Instead, in your user_profile_form_alter(), try set the redirect value to your intended destination:
    – alexkb
    Mar 18, 2014 at 12:38
  • Thanks! I have updated my code. The redirect is working. However the presave function is not. Even if leave only the presave function in my custom module. The role after submit is not removed.
    – Byron
    Mar 19, 2014 at 22:17
0

You can achieve what you want without writing any custom code. Use Rules and Rules Form Support to do it.

How to:

  1. Enable the above modules.

  2. Go to Configuration -> Workflow -> Rules -> Form events and check "Enable event activation messages on forms".

  3. Activate the user/edit page by visiting it and pressing "Activate events for user_profile_form" which appears in the status messages area.

  4. You will be redirected to a confirmation page. Give a name to your custom form label (I named it "User profile form") and check "Enable button level validate/submit events". Then activate the form.

  5. Create a new rule, reacting on event "User profile form is submitted".

  6. Add any conditions you may want. For example, as a condition you could add "User has role(s)" and select the role you want to remove.

  7. Rules Forms supports many actions like "Require an element in the form" (in your case the password fields) and "Set the redirect target of the form" to change the redirect path.

  8. Last one is to add one more action to remove the user role you want ("Remove user role" action).

I hope I helped you. If you have any questions feel free to ask!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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