0

There are more than 20 user roles(Editor,approver,new admin..) in the project. Whenever the admin assign/delete the role to user then the user should be notified with email. Email should say Hey Prabhakar You have been assinged to following roles or you have been removed from following roles.

  • 1
    Hey hey, could you please enhance your question: Where would you want to get that, for what, and what have you tried so far? – leymannx Jan 2 at 17:58
-1

Implement hook_user_presave:


/**
 * Implements hook_user_presave()
 */
function yourModuleName_user_presave(&$edit, $account, $category){
  foreach ($account->roles as $role_id => $val) {
    $user_roles_existing[] = $role_id;
  }    
  foreach ($edit['roles'] as $role_id => $val) {
    // $edit['roles'] contains all roles ids as key and selected role id as val. If not selected then value is 0
    // ex. $edit['roles'] = array(5=>5, 6=>0) here role id 5 selected
    if ((int) $val > 0) {
      $user_roles_form[] = $role_id;
    }
  }    
  $user_roles_new = array_diff($user_roles_form, $user_roles_existing);
  $user_roles_removed = array_diff($user_roles_existing, $user_roles_form);     
  //roles added
  if (count($user_roles_new) >= 1) {
    /**
     * Required code...
     */
    drupal_set_message('Testing new roles count' . count($user_roles_new));
  }
  //roles removed
  if (count($user_roles_removed) >= 1) {
    /**
     * Required code...
     */
    drupal_set_message('Testing Removed roles count' . count($user_roles_removed));
  }
}

  • 1
    For what purpose? Code only answers don't explain much. – Kevin Jan 2 at 20:20

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.