3

I'm using the Rules module and the Registration code module and I've tried configuring a rule like so:

  • Rules Event: upon saving a new user account(event).
  • Rules Condition: if the registration code used is "123456"(condition).
  • Rules Action: add a role for a user.

The rules export is as shown below:

{ "rules_give_trial_version" : { "LABEL" : "Give trial version", "PLUGIN" : "reaction rule", "OWNER" : "rules", "REQUIRES" : [ "rules", "regcode" ], "ON" : { "regcode_used" : [] }, "IF" : [ { "data_is" : { "data" : [ "regcode" ], "value" : "123456" } } ], "DO" : [ { "user_add_role" : { "account" : [ "user" ], "roles" : { "value" : { "10" : "10" } } } } ] } }

However, the Rules Action is not working (nothing works).

Any idea how to get this to work?

Note: The Rules Action works with the event "User has used a registration code" without any Rules Condition (if I temporarily remove that Rules Condition). But I'm badly in need of the condition as we want to check if a user has used a specific code. If yes, that user should be assigned a role.

1

I have thought of and implemented a different way out as explained below.

Event: User has used a registration code Conditions: User has used a registration code, Data comparison (regcode:tags) = 14, user has field access = "a user field" Then, Action: set a data value. i was able to view the registration code used (through token), and set it in a user field automatically. Note: I have already tagged the particular code with a taxonomy term from the registration code settings interface. This method was adopted because i realized the regcode:tag is readily available almost all the time in the rules config. Also, 14 above is the taxonomy term id of the term tagged with the code in question.

  • This is an answer to a different question, and "taxonomy term" is not included in the original question. Are you aware that changing the question after it was answered is not allowed, since it invalidates existing answers? I recommend you post a new question, if you want with a reference to this question, and then move your answer here to that new (self answered question) ... – Pierre.Vriens Nov 14 '16 at 21:36
  • It's not a different question. It only requires a different approach since that approach gives exactly what i'm looking for. Thanks – primerater01 Nov 15 '16 at 6:56
1

The typical Rules Condition doesn't seem to work here

Typically when using Rules, you can always give it a try to see if you can add, as a FIRST Rules Condition, a condition like "Entity has field". Or any other variation of a condition that informs the Rules module that you want to test the value of that field in a subsequent rules condition. Without that, Rules has no clue about which data to make available for subsequent processing ...

But, that doesn't seem to work/help for this particular case. For more details on that (if you really want to know why it doesn't work), you may want to look at the active Major task (not bug!) about Make better use of the Entity system ... (from Fago, who has quite a Rules "reputation" ...).

Rules debugging

If that doesn't help, I typically add a (temporary) Rules Action to just "Show a message on the site" (eg to show the value of a field ...). That might result in other unexpected results, to help you further debug it ...

But that doesn't seem to help either (grrrr), so time to move on to Rules debugging, as explained in my answer to "How can I display the value of a variable inside a condition?".

Depending on what the result of that type of debugging is, you may also want to (temporary) enable the Devel module, so that you can (temporary) add a Rules Action like "Display a value".

Try to find (and read) the documentation

Too bad, this module doesn't seem to have a lot of documentation, and the README.txt is not of much help either.

If anything else fails, read the sources

OK, the advantage of open source ... lets go read the sources ...

Aha, I see the regcode.rules.inc (regcode in this filename is the machine name of the Registration code module), so let's see what this module's Rules integration has to offer:

  • A Rules Event with machine name "regcode_used" and label "User has used a registration code". Fine, that's also what's used in the rule in the question here (nothing new).
  • A Rules Condition with (a loooong) machine name "regcode_rules_condition_regcode_available" and label "User has used a registration code". Huh??? That's the same label as in the Rules Event, confusing!!! But that Rules Condition is what intrigues me. Time to fabricate a variation of the rule in the question.

Rule prototype (1st attempt)

Have a look at this variation of the Rule in the question (also in Rules export format, use the Rules UI to import it in your own site):

{ "rules_give_trial_version" : {
    "LABEL" : "Give trial version",
    "PLUGIN" : "reaction rule",
    "OWNER" : "rules",
    "REQUIRES" : [ "regcode", "rules" ],
    "ON" : { "regcode_used" : [] },
    "IF" : [
      { "regcode_rules_condition_regcode_available" : { "user" : [ "user" ] } },
      { "data_is" : { "data" : [ "regcode" ], "value" : "123456" } }
    ],
    "DO" : [
      { "user_add_role" : { "account" : [ "user" ], "roles" : { "value" : { "10" : "10" } } } }
    ]
  }
}

The only difference in this rule (as compared to the original rule in the question) is the very first (confusing labeled) Rules Condition with machine name regcode_rules_condition_regcode_available.

That additional Rules Condition seems pretty close to the typical Rules Condition as I mentioned in the beginning of my answer (and Fago's issue I mentioned before makes a lot of sense to me now). And since the question states "... badly in need of the condition ...", I thought I'd leave the QA-testing of this variation of the rule in the question to the OPer of the question ...

Too bad, but that 2nd Rules Condition can never work, since the "[regcode]" token is not the token we're looking for (ie something that is a string, and which we want to check if it yes or no matches the registration code we're interested in to assign that special role). Read on for a new (2nd) attempt ...

Rule prototype (2nd attempt)

With some additional eyeballing and experiments, I noticed that within "Rules Actions" (not Rules Conditions!) there is the token [regcode:code], apart from all sorts of other regcode related tokens. So that opens another opportunity to get it to work anyway: let's just grab that [regcode:code] string and pass it on to a (new) Rules Component, in which we're going to do our thing (ie verify the registration code and grant the role if the code is what we want it to be. Below is the adapted rule from the previous attempt, and the (new) Rules Component:

a) Adapted Rule:

{ "rules_grant_a_role_after_using_a_registration_code" : {
    "LABEL" : "Grant a role after using a registration code",
    "PLUGIN" : "reaction rule",
    "ACTIVE" : false,
    "OWNER" : "rules",
    "REQUIRES" : [ "regcode", "rules" ],
    "ON" : { "regcode_used" : [] },
    "IF" : [
      { "regcode_rules_condition_regcode_available" : { "user" : [ "user" ] } }
    ],
    "DO" : [
      { "drupal_message" : { "message" : "Value of token \u0022regcode:code\u0022 is: [regcode:code] and will be used as parm for the subsequent Rules Action, which invokes a Rule Component." } },
      { "component_rules_grant_role_for_specific_regcode" : { "actual_regcode" : "[regcode:code]" } }
    ]
  }
}

b) Rules Component:

{ "rules_grant_role_for_specific_regcode" : {
    "LABEL" : "Grant role for specific regcode",
    "PLUGIN" : "rule",
    "OWNER" : "rules",
    "REQUIRES" : [ "rules" ],
    "USES VARIABLES" : { "actual_regcode" : { "label" : "Actual Regcode", "type" : "text" } },
    "IF" : [ { "data_is" : { "data" : [ "actual-regcode" ], "value" : "123456" } } ],
    "DO" : [
      { "user_add_role" : {
          "account" : [ "site:current-user" ],
          "roles" : { "value" : { "10" : "10" } }
        }
      },
      { "drupal_message" : { "message" : "Since you used registration code \u0022[actual-regcode:value]\u0022, you have been granted a special role that corresponds to it." } }
    ]
  }
}

Note the 2 extra "Show a message" actions I added (to simplify QA testing).

Curious to hear if with this 2nd attempt it does work, or if you can MAKE it work from here on ...

  • Comments are not for extended discussion; this conversation has been moved to chat. – Clive Nov 11 '16 at 17:37
  • Thanks for the above. Did you try the rule? I imported it and test. Nothing changes still! – primerater01 Nov 11 '16 at 19:31
  • @primerater01 Did you see the "I thought I'd leave (at least for now) the QA-testing of this variation of the rule in the question to the OPer of the question ..."? And what about your new "Rules debugging" results? – Pierre.Vriens Nov 11 '16 at 19:40
  • From the Rules debugging, The first condition returns true, while the second condition returns false. Hence, the rules will not run since both conditions are expected to return true for it to work – primerater01 Nov 11 '16 at 20:49
  • @primerater01 : your turn again to verify which results you get with my "2nd attempt" (which is what I added now, together with some edits at the end of the 1st attempt to explain why that could never work (understanding the problem is already part of the solution, right?). Let me know how it turns out ... – Pierre.Vriens Nov 13 '16 at 14:33

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.