Creating of custom actions and events using rules api


Module rules provides an opportunity for web developers to carry out some actions after certain events have occurred. It has a list of events and activities in its structure, which we may make use of so as to create our own rules. There are situations, however when the items on the list are not applicable to implementing the planned functionality. In this case, you can create your own «action» or «event».

This article is describing how the functionality for creating your own activities and events is implemented. So, first of all you need to download and install the Rules module, which will provide us with the necessary API, and afterwards, we will proceed to fulfilling the set objective. You start creating your own «Action» with the announcement of hook_rules_action_info (). It is desirable, for this purpose, create a separate file, ("module" is the name of your own module), in which the code is supposed to be kept. By way of illustration, we will have some messages to users with a certain role been displayed. We will need to pass into the event execution function the role of users whom the message is going to be displayed to, and the text of the message proper. Let’s start with the implementation of the Hook:

 * Implements hook_rules_action_info().
function internetdevels_rules_action_info() {
  $actions = array(
    'internetdevels_view_message' => array(
      'label' => t('View message for selected users'),
      'group' => t('My custom actions'),
      'parameter' => array(
        // To select a user role.
        'roles' => array(
          'type' => 'list<integer>',
          'label' => t('Roles'),
          'options list' => 'entity_metadata_user_roles',
          'description' => t('Select the roles whose users can view your message.'),
        // Messages that should appear after a particular action.
        'message' => array(
          'type' => 'text',
          'label' => t('Message'),
          'description' => t("The message body."),
  return $actions;

Reset the cache to make your «Action» appeare on the list so you could use it. You also need to add a feature which will work on the operation we need. Thus, we accomplish the following:

 * View message for selected roles.
function internetdevels_view_message($roles, $message) {
  global $user;
  $roles_mg = array_intersect_key($user->roles, $roles);
  foreach ($roles_mg as $key => $value) {
    if (in_array($value, $user->roles)) {

That’s it. Now you can create your rule and add the created event.
Next, we are going to create our own «event». Let’s consider the example which is might illustrate how «Action» will be carried out after we’ve created an action that had removed the nodes of the content type «article». Let’s start with the announcement hook_rules_event_info (), which also should be placed in the file

 * Implements hook_rules_event_info().
function internetdevels_rules_event_info() {
  $events = array(
    'internetdevels_event_delete_node' => array(
      'label' => t('Deleting nodes a specific type of content'),
      'group' => t('My custom events'),
      'variables' => array(
        'node_type' => array(
          'label' => t('Node with with certain type.'),
          'type' => 'node',
          'skip save' => TRUE,
  return $events;

As it was done in the previous example, reset the cache. Next step is to check the type of material and to perform necessary action on hook_node_delete ()

 * Implements hook_node_delete().
function internetdevels_node_delete($node) {
  if ($node->type == 'article') {
    rules_invoke_event('internetdevels_event_delete_node', $node);

Our «event» is completed.
API Modul’s capability is not only restricted to creating some sort of events and actions. You can view all available hooks and functions of rules here

6 votes, Rating: 5

Read also


Let us give a brief outline of the library to be going withpChart ...


There are situations when you need to transfer data from one database to another. This article is meant to cater...


Nowadays iPhone and iPad (further iGadgets) are not something unfamiliar for us. Very often developers come across with the tasks connected with the realization of functional for these...


There may be instances when a web developer needs to transfer some database changes from one site to another. Moving the...


As social networks gain more and more ground customers are often confronted with the task of integrating their sites with Facebook, Twitter, Google+, etc. ...

Need a quote? Let's discuss the project

Are you looking for someone to help you with your Drupal Web Development needs? Let’s get in touch and discuss the requirements of your project. We would love to hear from you.

Join the people who have already subscribed!

Want to be aware of important and interesting things happening? We will inform you about new blog posts on Drupal development, design, QA testing and more, as well news about Drupal events.

No charge. Unsubscribe anytime