0

i'm updating role using hook user update like this :

function my_module_user_update(&$edit, $account, $category) {  
  //print_r($account->field_my_field['und'][0]['value']);exit;
  if($account->field_my_field['und'][0]['value'] == 1 ) { 
    $uid = $account->uid;
    $role_name = 'Spanish';
    if ($role = user_role_load_by_name($role_name)) {
      user_multiple_role_edit(array($uid), 'add_role', $role->rid);
      //print "1";print_r($role);exit;
    }
  }
  elseif($account->field_my_field['und'][0]['value'] == 0 ) {
    $uid = $account->uid;
    $role_name = 'Spanish';
    if ($role = user_role_load_by_name($role_name)) {
      user_multiple_role_edit(array($uid), 'remove_role', $role->rid);
     //print "2";print_r($role);exit;
    }
  }
}

but it is not working : user's role is not updating my hook_update_user is correctly called, i clear cache after all change in script , i have syslog and database logging enabled to see the error log, but there is no error

When i doing like this (with just one condition), it's working , i have no problem : user's role is correctly updated accoding the condition

function my_module_user_update(&$edit, $account, $category) {  

     if($account->field_my_field['und'][0]['value'] == 1 ) { 
       $uid = $account->uid;
       $role_name = 'Spanish';
       if ($role = user_role_load_by_name($role_name)) {
            user_multiple_role_edit(array($uid), 'add_role', $role->rid);
          //print "1";print_r($role);exit;
       }
    }

}

any ideas ?

3
  • Is your hook_user_update getting called? If not then, after adding a new hook in module you must clear cache. If module is enabled and you have cleared cache also, then you should check in "Recent log messages", see if any error related to this is triggered. Jul 14, 2014 at 12:26
  • Please give more information about your problem. Why you say is not working? Does Drupal complains in any way? You don't see the desired effect? Also, are you sure your hook implementation is called?
    – sanzante
    Jul 14, 2014 at 12:27
  • Hello and welcome. We are not a free debug service. Don't expect us to put your code in our environments to even see what happens with it. If it fails, describe both expected and experienced results, at the very least. Showing at least a bit of own effort to solve it yourself would be good, too.
    – Mołot
    Jul 14, 2014 at 12:35

1 Answer 1

2

hook_user_update : Modules may use this hook to update their user data in a custom storage after a user account has been updated.

So that hook is call only when you edit the user account. other things is disable you module which implement this hook.

Morever if you want to edit the role every time when user is created/edited use hook_user_presave.

1
  • thank you for your post getting it working like that : fonction my_module_user_presave(&$edit, $account, $category) { $role_name = 'Spanish'; $role = user_role_load_by_name($role_name); $rid = $role->rid; if($edit['field_my_field']['und'][0]['value'] == 1){ $edit['roles'][$rid] = 1; }else{ $edit['roles'][$rid] = 0; } }
    – stomerfull
    Jul 14, 2014 at 13:48

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