Skip to main content

Creating a Rules event for unblocking a user

File under

Oddly enough, the extremely useful Rules module doesn’t come with an event for unblocking a user. Thankfully, implementing a custom event for this occurrence is really easy. There’s two basic steps:

  1. Implement hook_rules_event_info(). All you’re doing here is registering your custom event with Rules. Like other _info() functions, this function mostly takes metadata about your event: it’s label, the group it should belong to, and so on. You also need to tell Rules whether this event should make any variables available to subsequent actions. In this case, we want to pass it the account of the user who was just unblocked.
  2. Fire the event at the appropriate time. We want to fire the event when a user is unblocked, so we hook into hook_user_update() and check to see if the user was just unblocked. If they were, we fire off the event.

Here are teh codez.

/**
 * Implements hook_rules_event_info().
 *
 * Registers the 'After unblocking a user' event with Rules.
 */
function MYMODULE_rules_event_info() {
  return array(
    'user_was_unblocked' => array(
      'label' => t('After unblocking a user'),
      'group' => t('MYMODULE'),
      'module' => 'MYMODULE',
      'variables' => array(
        'account' => array(
          'type' => 'user',
          'label' => t('updated user'),
        ),
      ),
    ),
  );
}

/**
 * Implements hook_user_update().
 *
 * If the user is unblocked, fire off a custom Rules event.
 */
function MYMODULE_user_update(&$edit, $account, $category) {
  if ($edit['status'] == 1 && $edit['original']->status == 0) {
    // "... cause I went from negative to positive" http://goo.gl/BCGHg
    if (module_exists('rules')) {
      $user = user_load($edit['original']->uid);
      rules_invoke_event('user_was_unblocked', $user);
    }
  }
}

Post new comment

The content of this field is kept private and will not be shown publicly.
  • You can use Markdown syntax to format and style the text. Also see Markdown Extra for tables, footnotes, and more.
  • Web page addresses and e-mail addresses turn into links automatically.
  • Allowed HTML tags: <a> <em> <strong> <cite> <img> <code> <ul> <ol> <li> <dl> <dt> <dd> <pre> <h1> <h2> <h3> <h4> <h5> <h6>
  • Lines and paragraphs break automatically.
  • Adds typographic refinements.

More information about formatting options