Categories

(83)
(69)
(8)
(34)
(74)
(149)

Creating of custom actions and events using rules api

03.05.2013
Creating of custom actions and events using rules api
Author:

Module rules provide 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 afterward, we will proceed to fulfill 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.rules.inc, ("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» appear 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)) {
      drupal_set_message($message);
    }
  }
}

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 module.rules.inc.

/**
 * 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.

7 votes, Rating: 5

Read also

1

Let us give a brief outline of the library to be going with. pChart is a set of classes that were designed for plotting charts, diagrams, etc. on php.

2

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

3

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...

4

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

5

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

Subscribe to our blog updates