2

I have exported rules from the existing rules and I wanted to import rule using custom module. But when I enable this module. Rules weren't imported in rules list.

I created 3 files:

1) my_module.INFO file:

name = My module example
description = My module for import rules
core = 7.x
package = My module

2) my_module.MODULE file (blank file):

<?php

3) my_module.rules_defaults.INC file:

<?php
     function rules_example_default_rules_configuration() {
      $configs = array();
      $rule = '{ "rules_final_save_pdfs" : {
        "LABEL" : "final save pdfs",
        "PLUGIN" : "reaction rule",
        "ACTIVE" : false,
        "OWNER" : "rules",
        "REQUIRES" : [ "rules", "views_pdf", "entityform" ],
        "ON" : { "entityform_insert" : [] },
        "IF" : [
          { "entity_has_field" : { "entity" : [ "entityform" ], "field" : "field_test_pdf_form" } }
        ],
        "DO" : [
          { "views_pdf_rules_action_save" : {
              "views_pdf" : "test_new_view:pdf_1",
              "path" : "\/workout\/sites\/default\/files\/test_pdfs\/[entityform:field-date]"
            }
          }
        ]
      }
    }';
      $configs['rules_final_save_pdfs'] = rules_import($rule);
      return $configs;
    }
?>

Please let me know if there any changes to do.

0
0

Assuming this is for D7, try this variation of the your code in your item "3)":

<?php
     function my_module_default_rules_configuration() {
      $configs = array();
      $rule = '{ "rules_final_save_pdfs" : {
        "LABEL" : "final save pdfs",
        "PLUGIN" : "reaction rule",
        "ACTIVE" : false,
        "OWNER" : "rules",
        "REQUIRES" : [ "rules", "views_pdf", "entityform" ],
        "ON" : { "entityform_insert" : [] },
        "IF" : [
          { "entity_has_field" : { "entity" : [ "entityform" ], "field" : "field_test_pdf_form" } }
        ],
        "DO" : [
          { "views_pdf_rules_action_save" : {
              "views_pdf" : "test_new_view:pdf_1",
              "path" : "\/workout\/sites\/default\/files\/test_pdfs\/[entityform:field-date]"
            }
          }
        ]
      }
    }';
      $configs['rules_final_save_pdfs'] = entity_import('rules_config', $rule);
      return $configs;
    }
?>

Note the changes in the line with function ... (near the top) and the line with entity_import (near the end).

Be aware: if I try to import your rule (= the string contained in $rule) in a site of mine (where I don't have views_pdf module enabled), and using the Rules UI, I get an import error like so:

Import failed.

Missing the required module views_pdf.

That makes sense of course (since I don't have that module). So check that that is not the case why your rule isn't imported (same for entityform). A possible technique to prevent this issue, is to declare those 2 modules as a dependency in your my_module.info file.

PS: have a look at the code of the Answers Userpoints module for an example where this technique is used also.

1
  • Do we have to do any other changes in module [ my_module.MODULE ] file?
    – kishore ms
    Jan 19 '17 at 16:31

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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