In the process of redesigning a client's Drupal site, my project team decided that the community site's users would benefit from the ability to receive notifications about content events as they occur. These events include the addition of new comments, but could include any number of content-related events. Our team chose the Message Stack modules to help us build a richer user experience for our client's community.
When designing a Drupal site, the developer should consider the user's need and desire to subscribe to and receive notifications about content events. The message stack modules allow users to subscribe to content and be notified when events occur involving that content. The Message Subscribe module uses Flag to handle the content subscriptions and Message Notify provides notification methods. Message provides the base on which this functionality is built.
In order to implement the message stack, the following modules (and their dependencies) must be downloaded and enabled: message, message_notify, message_subscribe, and flag. The following drush commands will perform this task:
drush dl message, message_notify, message_subscribe
drush en message, message_notify, message_subscribe
These commands will also download and enable the entity and flag modules. Once the modules are enabled, navigate to admin/config/system/message-subscribe and configure the message subscribe module.
The following options appear on this form:
Default message notifiers - These are the message notifiers that will be added to every subscription. The message_notify module provides two notifiers: email and sms (requires the sms framework).
Notify author of their own submissions - Checking this box will cause users to receive notifications for their own actions on the site.
Flag prefix - Flags whose names are prefixed with this string will be used for subscriptions. The stack provides default flags for users, nodes, and terms - these can be enabled at admin/structure/flags.
Use queue - Whether or not Drupal’s queue will be used to process the notifications.
Once you’ve configured message subscribe and created flags at admin/structure/flags, you’ll likely want to add some views for users to manage their subscriptions. You’ll also want to add and theme flag/unflag links as described in the Flag module documentation. The message_subscribe_ui module provides boilerplate subscription management views in the user profile:
Create Message Types
Once you’ve completed the basic setup for the message stack, you’ll want to create at least one message type for notification. Message types can be managed at admin/structure/messages. Each message_notify notifier plugin uses view modes to take the message text and render it for use in the notification. The email notifier provided by message_notify defines two view modes for the subject and body of the email. You can write your own notifiers if you need more robust functionality than is provided by the module.
Enabling the Token and Entity Token modules will allow the use of replacement tokens in email messages.
Custom Module Creation
Once you’ve created at least one message type for notification, you’ll want to create a custom module that implements message_notify_send_message on a new message instance. You’ll want to implement this code in hooks that fire when content is created or updated, such as hook_node_insert, hook_comment_insert, etc.
Message_subscribe_send_message accepts an optional $context argument. If no $context is provided, the value returned by message_subscribe_get_basic_context() will be used. The context is an array keyed with the entity type and array of entity IDs as the value: these entities are those related to the entity for which the notification will be sent. For instance, taxonomy terms on a node or the parent node for a comment.
If you need to customize the context for your notifications, you’ll want to create a new function based on message_subscribe_build_context().
Whether you use a default or custom context, that context will be used by all modules that implement hook_message_subscribe_get_subscribers(). These functions add and manipulate a list of all users that are subscribed to the content, as well as which flags are responsible for the subscriptions, and which notifiers will be used for the message. If you need to implement hook_message_subscribe_get_subscribers() in your custom module, see message_subscribe_message_subscribe_get_subscribers() for example code.
Each user returned by the functions mentioned above will have a list of notifiers. Message_subscribe_send_message() will use each of these notifiers (and the default notifiers configured during the message stack setup) to send the message to that user. Message_notify provides email and sms notifiers, but it’s possible to create custom notifiers for any service.
While it does require some custom code, the message stack provides an elegant solution to the problem of content notifications in Drupal 7. The message_notify_example module provides example code for implementing these features, as do the references listed below.
- Message Stack Handbook
- Creating Messages for Notification
- How to Use Message_subscribe with Organic Groups
- Creating Message Instances
- Message-subscribe - A New Subscription System