5

Is there any universal formula or module to create custom rules actions, events and conditions? So that I just have to replace the required values and rest of the code remains the same. I searched, but I did not find such code. I found few, but those were targeted towards a specific need/question.

For example there are lot of things missing. I am using message module to log activity. I am using statuses module and flag 2 (flag 3 is not compatible with the statuses module) I am trying to do following

  1. Log statuses comments. Logs only for "currently logged in user". Cannot determine who commented. for e.g. John commented on your status update. Event available, but only shows current user name instead of who commented.
  2. Log activity for "John liked your status update". No event available.
  3. Similarly, for status updates comments, "likes flag".
  4. John started following you on stackexchange. Event is available, but it also lacks option to tell who followed you. It also only shows current user name. Obviously you do not need your name here, you want to know who followed you.
  5. John mentioned you on stackexchange. Again, instead of john, it will show current user because you do not have any option when setting up the rule.

The simple activities are easily created though.

1
  • statuses module and message module are totally different. I think u should read the description of the modules
    – Umair
    Commented Sep 25, 2016 at 11:14

1 Answer 1

6

A "universal formula or module to create custom rules actions, events and conditions" (as is your question), is to write a custom module. To do so, you either create a new module, or you extend any of your existing custom modules. Refer to Drupal custom Rules how to write your own events conditions, actions and custom object (+custom tokens) for a great tutorial about how to do so. The main steps that are detailed in this tutorial are further described below (with the code included below quoted from that link).

Part 1: Defining the Event, Condition and Action

Define the Rules Event

/**
* Implementation of hook_rules_event_info().
* @ingroup rules
*/
function your_module_rules_event_info() {
  return array(
    'your_module_package_bought' => array(
      'label' => t('A package was bought'),
      'module' => 'your_module',
      'arguments' => array(
        'acting_user' => array('type' => 'user', 'label' => t('The user who bought the package.')),
        'package' => array('type' => 'package', 'label' => t('The purchased package.')),
      ),
    ),
  );
}

Define the Rules Condition

/**
 * implementation of hook_rules_condition_info()
 */
function your_module_rules_condition_info() {
  return array(
    'your_module_condition_package_type' => array(
      'label' => t('Type of the package'),
      'arguments' => array(
        'package_id' => array('type' => 'value', 'label' => t('The type of the purchased package.')),
        'package_type' => array('type' => 'string', 'label' => t('The type of the purchased package is')),
      ),
      'module' => 'your_module',
    ),
  );
}

Define the Rules Action

/**
 * Implementation of hook_rules_action_info().
 */
function packages_rules_action_info() {
  return array(
    'your_module_action_change_order_status' => array(
      'label' => t('Change the order status'),
      'arguments' => array(
        'order' => array('type' => 'value', 'label' => t('The order object.')),
      ),
      'module' => 'your_module',
    ),
  );
}

Part 2: Coding the custom Event, Condition and Action

Event:

Somewhere in your custom module, you need to include logic to trigger the event, with something similar to this example (note the your_module_package_bought in it):

  //here the code for buying a package will be located
  //when that code returns that a package was bought trigger the rule
  $order = order_load($oid);//$oid will be the id of the order made
  $package = package_load($pid);//pid will be the id of the bought package
  global $user;
  rules_invoke_event('your_module_package_bought', $user, $package, $order);

Condition:

To evaluate the Rules condition (to check if it is TRUE or FALSE), the function your_module_condition_package_type is executed using the arguments provided. Here is an example of how such function could look like:

function your_module_condition_package_type($pid, $type) {
  $package = package_load($pid);
  return ($package->type == $type) ? true : false;
}

Action:

Somewhere in your custom module, you need to include logic to perform the actual action, in this example it should be done within the function named your_module_action_change_order_status.

9
  • Is it important to create a new module or I can insert it in my existing module?
    – Umair
    Commented Sep 27, 2016 at 7:58
  • @Umair : check my updated answer (2nd phrase I added): you can perfectly use your existing module. Commented Sep 27, 2016 at 8:04
  • For few days, I was not able to come online. Actually I wanted to say something. Your answer is incomplete and a little difficult for a beginner like me to understand and implement. I think there should be more detailed instructions. Secondly, you did not tell me the code to get all the replacement patterns in a rule. I am pasting the .module and .inc file of the rules integration with statuses module which contains the rules, events and conditions for statuses module.
    – Umair
    Commented Oct 7, 2016 at 12:35
  • Module name is statuses and the sub-module name is fbss_rule (This sub-module integrates statuses with rules)
    – Umair
    Commented Oct 7, 2016 at 12:40
  • There is not much space here to paste the code at once. I will provide you the link
    – Umair
    Commented Oct 7, 2016 at 12:46

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.