Drupal Subscriptions Module - Referenced Nodes

The Drupal Subscriptions module notifies users on certain events. Usually when a new piece of content has been either saved or updated. Users can subscribe to specific nodes, terms or entire content types allowing them to keep up with the changes on the site. It's a great module.

This starts to break down however, when node references enter the picture. One of our recent Drupal 6 intranet projects required this feature and a custom solution had to be explored. The solution turned out to be pretty simple thanks to a extremely useful hook supplied in the Subscriptions module.

In this example, we have two different content types: Group and Document. Users subscribe to the Group content type, which is node-referenced from the Document content type. When a new Document is saved or updated, it is added to the subscribed users notification queue and an email is sent out on the next cron run.

We start with a hook_subscriptions_queue_alter(). Since we want to add to the queue when a node saves, we'll want to check for that first.

/**
 * Implementation of hook_subscriptions_queue_alter().
 */
function deckfifty_subscriptions_queue_alter(&$event) {
  switch($event['module']) {
    case 'node':
      // insert and update actions
      if($event['action'] == 'insert' || $event['action'] == 'update') {

      }
      break;
  }
}

Next, we'll check the node type (in this case, document) and whether or not the node reference field is populated.

/**
 * Implementation of hook_subscriptions_queue_alter().
 */
function deckfifty_subscriptions_queue_alter(&$event) {
  switch($event['module']) {
    case 'node':
      // insert and update actions
      if($event['action'] == 'insert' || $event['action'] == 'update') {
        // document nodes
        if($event['node']->type == 'document') {
          // group node reference field is populated
          if($event['node']->field_document_group[0]['nid']) {
		  
          }
      }
      break;
  }
}

Lastly we'll pass $event to subscriptions_queue(), but with a little twist. Normally, when a node is saved the node object is passed to this function. Since users can't subscribe to document nodes, only group nodes, we want to pass the document node object to the subscription_queue function. This will allow all users to get notified when a new document has been saved or updated.

/**
 * Implementation of hook_subscriptions_queue_alter().
 */
function deckfifty_subscriptions_queue_alter(&$event) {
  switch($event['module']) {
    case 'node':
      // insert and update actions
      if($event['action'] == 'insert' || $event['action'] == 'update') {
        // document nodes
        if($event['node']->type == 'document') {
          // group node reference field is populated
          if($event['node']->field_document_group[0]['nid']) {
            // alter event
            $event['node'] = node_load($event['node']->field_document_group[0]['nid']);
            // add to queue
            subscriptions_queue($event);
          }
      }
      break;
  }
}

When a document is saved, the subscriptions_queue table will now get updated with all of the users who are subscribed to the group. When cron is run, the users will receive an email notifying them of new content!