-1

I am having trouble trying to remove a role after user account submit.

I have used the following code:

function vdb_form_alter(&$form, $form_state,  $form_id) {
  global $user;


}
  if(($form_id === 'user_profile_form')and (isset($form['account']['pass'])) and (in_array('Hybridauth', array_values($user->roles))) ) {
      $form['#submit'][] = 'my_module_hook_user_update';


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

Resulting in the following errors:

Warning: Missing argument 3 for vdb_hook_user_update(), called in \includes\form.inc on line 1471 and defined in vdb_hook_user_update() (line 46 Notice: Trying to get property of non-object in vdb_hook_user_update() (line 47 Warning: array_search() expects parameter 2 to be array, null given in vdb_hook_user_update() (line 47 Warning: Attempt to modify property of non-object in vdb_hook_user_update() line 50

I have tried using hook_user_presave however using this hook hybridauth module gets stuck.

  • 1
    hook_user_presave() is the correct hook to use; data has already been submitted to the database by the time hook_user_update() is invoked – Clive Jan 8 '15 at 10:44
  • I have tried hook_user_presave however once a user is created by hybridauth the function fires before the user has filled his user account. Which i dont want. Is there another hook i can use? – Byron Jan 8 '15 at 11:01
1

The errors in your code are two:

  • vdb_hook_user_update() is not the correct name for an implementation of hook_user_update()

  • vdb_hook_user_update() is being used as form submission handler

As consequence of the second point is that you are declaring the function with the wrong arguments, since a form submission handler gets two arguments: $form and &$form_state.

As Clive pointed out, you should use hook_user_presave(), which is invoked before the user account is saved in the database. Remove the code adding the form submission handler, and implement hook_user_presave() using code similar to the following one.

function vd_user_presave(&$edit, $account, $category) {
  if (!empty($account->uid) && ($key = array_search('Hybridauth', $account->roles)) !== FALSE) {
    unset($account->roles[$key]);
  }
}
0

It is not need to set a submit handler, by default when you user update hook documentation Modules may use this hook to update their user data in a custom storage after a user account has been updated., so you don't need set a submit handler in form_alter ,as Clive mentioned in comment you can also try hook user presave,

and So

function mymodule_user_presave($edit, $account, $category) {
  $role_position = array_search('Hybridauth', $account->roles);
  // Use !== or === here since $role_position may be zero or FALSE
  if ($role_position !== FALSE) {
    unset($account->roles[$role_position]);
  }
}
  • My usecase is as following. 1.User is created by hybridauth module 2.Upon user creation it will receive a role 'hybridauth' 3.user fills his account and submits. 4.User role 'hybridauth' should be removed. When i use user_pre_save the role that is given creating the user, is taken away in that same process, before viewing the user account. The role should still be there when user sees his user account. – Byron Jan 8 '15 at 11:14
  • No succes. I removed all code out of my custom module except <?php function vdb_user_presave($edit, $account, $category) { $role_position = array_search('Registered with mail', $account->roles); // Use !== or === here since $role_position may be zero or FALSE if ($role_position !== FALSE) { unset($account->roles[$role_position]); } } ?> However this code is not removing my role upon saving the user page. – Byron Jan 8 '15 at 13:32
0

Have you tried unsetting the edit parameter which should also have roles in it and your function my_module_hook_user_update($edit, $account, $category) should have &$edit.

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.