1

i have created list of component fields in a Drupal 7 module, copied the exported Rules code and then pasted it into our custom module

But it is not working. Any ideas how to get it to work?

This is my exported Rule:

$rule = '{ "rules_faircent_rules" : {
"LABEL" : "Faircent Rules",
"PLUGIN" : "rule set",
"OWNER" : "rules",
"TAGS" : [ "Faircent Rules" ],
"REQUIRES" : [ "rules" ],
"USES VARIABLES" : {
  "fc_home_city" : { "label" : "Home city ", "type" : "integer" },
  "fc_pin" : { "label" : "Pin Code", "type" : "integer" },
  "fc_loan" : { "label" : "Loan Type", "type" : "integer" }
},
"RULES" : [
  { "RULE" : {
      "PROVIDE" : { "result" : { "result1" : "Calculation result", "result" : "Calculation result" } },
      "IF" : [
        { "data_is" : { "data" : [ "fc-home-city" ], "value" : "10" } },
        { "data_is" : { "data" : [ "fc-pin" ], "value" : "5" } },
        { "data_is" : { "data" : [ "fc-loan" ], "value" : "12" } }
      ],
      "DO" : [
        { "data_set" : { "data" : [ "fc-loan" ], "value" : "35" } },
        { "data_set" : { "data" : [ "fc-home-city" ], "value" : "15" } },
        { "data_set" : { "data" : [ "fc-pin" ], "value" : "25" } },
        { "data_calc" : {
            "USING" : { "input_1" : [ "fc-home-city" ], "op" : "+", "input_2" : [ "fc-pin" ] },
            "PROVIDE" : { "result" : { "result1" : "Calculation result" } }
          }
        },
        { "data_calc" : {
            "USING" : { "input_1" : [ "result1" ], "op" : "+", "input_2" : [ "fc-loan" ] },
            "PROVIDE" : { "result" : { "result" : "Calculation result" } }
          }
        }
      ],
      "LABEL" : "Calculate over all "
    }
  }
]

} }';

$configs['commerce_checkout_order_email'] = rules_import($rule);
echo '<pre>';
print_r($configs['commerce_checkout_order_email'] );

Above code is not working for me. What will be exact solution?

2
  • Please EDIT your question to clarify what you mean by "getting empty results". I can interpreted that in at least 2 different ways, but I don't want to guess what the answer is before posting an answer (at the risk of answering the wrong question ...) Commented Sep 1, 2015 at 7:26
  • Hi Pierre above code is not working can you suggest me exact solution how to integrate exported rules component code into custom module ?? Thanks Commented Sep 1, 2015 at 7:57

2 Answers 2

3

Here is what's needed to integrate your exported Rules component in a custom module:

<?php
function MY_MODULE_NAME_default_rules_configuration() {
  $configs = array();
  $rule = '{ "my_exported_rule" : {
    "LABEL" : "My Exported Rule",
    ...,
  }
}';
  $configs['my_exported_rule'] = rules_import($rule);
  return $configs;
}
?>

So to use such exported Rule, you need to run it through the function rules_import(). This will change the exported Rule in a format that is usable for the Rules module.

To reuse this example:

  • Replace MY_MODULE_NAME with the machine name of your module.
  • Replace the value used for $rule by the content of your exported rule.
  • Make sure to use the machine name of your rule (= my_exported_rule in my sample) as the key in $configs.

In the sample contained in the question, the machine name of the rule is rules_faircent_rules, though that is NOT what is used in the $configs ... Therefor I think you should correct your code like so:

$configs['rules_faircent_rules'] = rules_import($rule);
echo '<pre>';
print_r($configs['rules_faircent_rules'] );

Refer to the articled "Drupal 7 - Defining rules within a module using PHP" for some more details.

0
1

In drupal 7 the best way to export drupal / modules(contributor) configurations is feature. Feature allows ability to export few configurations (rules by exemple) into files instead of custom modules. @see https://www.drupal.org/project/features

Good chance :)

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.