Is there a way to get notified when a user is blocked ? like a hook in user module or ? or there are another way ?


There is hook_user_update() for this. Below is an example on how you can use it for your purpose:

 * Implements hook_user_update().
function my_module_user_update(&$edit, $account, $category) {
  // Check if the user was blocked
  if ($account->original->status != $account->status && $account->status == 0) {
    // Handle notification here

To send an email programmatically from Drupal, you can check out this question.

Another way to do this, would be by using the Rules module and following the steps below:

  1. Create a new rule that reacts on the user event "After updating an existing user account".
  2. Add a condition of type "User is blocked" and save it with the defaults.
  3. Add another condition of type "User is blocked", but this time use "account-unchanged" for the Data Selector and check the Negate checkbox.
  4. Add an action of type System > "Send mail" and play with it.
  • That is good, But this hook is going to be called when ever any user changes any thing, Is there a hook doing that ? – Samuel Beniamin Apr 1 '13 at 14:26
  • I'm not sure I understand your question ... but yes, this hook gets called every time a user account gets updated (either by users or administrators). – ovi Apr 1 '13 at 14:31
  • I just added an alternative solution. This one involves using the Rules module and you don't need to write any code to use it. – ovi Apr 1 '13 at 16:42
  • That restrict your action for just sending email. but the first is much powerful. – Samuel Beniamin Apr 2 '13 at 16:58
  • You are not restricted at all. There are more actions you can use and if they are not sufficient, you can always use PHP code as an action (you need to have PHP filter enabled first). – ovi Apr 3 '13 at 9:50

There are function - user_is_blocked($name). Where do you want to notify user?

But you can simply check user name like this:

  if (user_is_blocked($user->name) != FALSE) {
       drupal_set_message('account blocked');

To notify user when account is blocked you can go to admin/config/people/accounts there are tab - Account blocked - and you can check Notify user when account is blocked. You can set subject and body.

  • Okay I meant when the user get blocked by an administrator I get notified ? – Samuel Beniamin Apr 1 '13 at 14:02
  • updated my answer – Petro Popelyshko Apr 1 '13 at 14:07
  • I mean that when any user get blocked I as a developer get notified by hook or any other mean ? – Samuel Beniamin Apr 1 '13 at 14:14

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.