Skip to main content

Drupal: Disabling another module's rule configuration from my own module.

Today I was working on a feature on a site that was using Ubercart's uc_roles module. By default, whenever a customer would buy a product with a role attached to it, an email would be generated stating what role that customer bought. However, in my use case, we did not want these emails generated and we needed to keep our settings in code. So, the question was, how do I disable another module's rules from my own module? After Jedi mind tricks failed, I came across the solution in the Rules API and the function hook_default_rules_configuration_alter.

To disable rules from within a module

  1. First, add a new file to your module named "MY_MODULE.rules_defaults.inc", replacing MY_MODULE with your own module's name.
  2. Within the file you just created add the following function:

    1. /**
    2.   * Implements hook_default_rules_configuration_alter().
    3.   */
    4.   function bfa_instructor_listings_default_rules_configuration_alter(&$configs) {}

  3. Then, you need to get the machine name of the rule you want to disable. If you go to the rules admin screen, underneath the name is the machine name.
  4. Once you have the machine name, go back to your hook_default_rules_configuration_alter function and add the following to the function:
    1. $configs['uc_role_notify_grant']->active = FALSE;
  5. So, my final code to disable all uc_role emails rules looked like this:
    1. /**
    2.   * Implements hook_default_rules_configuration_alter().
    3.   */
    4.   function MY_MODULE_default_rules_configuration_alter(&$configs) {
    5.     $configs['uc_role_notify_grant']->active = FALSE;
    6.     $configs['uc_role_notify_revoke']->active = FALSE;
    7.     $configs['uc_role_notify_renew']->active = FALSE;
    8.     $configs['uc_role_notify_reminder']->active = FALSE;
    9.   }

Comments