I only really started using Rules with version 2 and it has made a huge difference to my development. Massive thanks!

When setting a data value (text) I am unable to include a rule variable (also text) in direct input mode. I was originally trying to do this to build up a large string from fields while looping through a list of nodes. For the sake of understandability I've reduced this to the simplest possible case. The rule export is below and requires Devel module.

Note: I can perform direct assignment of one variable to another using data selection. It's just direct input mode where I'm failing.

Please can someone tell me whether I should be able to do this and, if so, what I'm doing wrong?

Edit: Saw how busy the Rules issue queue was so have cross-posted to Drupal Answers...
http://drupal.stackexchange.com/questions/29373/how-to-include-a-rule-va...

{ "rules_example_text_inclusion" : {
    "LABEL" : "Example text inclusion",
    "PLUGIN" : "reaction rule",
    "REQUIRES" : [ "rules", "devel" ],
    "ON" : [ "node_view" ],
    "DO" : [
      { "variable_add" : {
          "USING" : { "type" : "text", "value" : "*This is the value of text_variable_1*" },
          "PROVIDE" : { "variable_added" : { "text_variable_1" : "Text variable 1" } }
        }
      },
      { "variable_add" : {
          "USING" : {
            "type" : "text",
            "value" : "*This is the initial value of text variable 2*"
          },
          "PROVIDE" : { "variable_added" : { "text_variable_2" : "Text variable 2" } }
        }
      },
      { "data_set" : {
          "data" : [ "text-variable-2" ],
          "value" : "Overwriting the value of text_variable_2:\r\n[text-variable-1]\r\n[text_variable_1]\r\n"
        }
      },
      { "devel_debug" : { "value" : [ "text-variable-2" ] } }
    ]
  }
}
Files: 
CommentFileSizeAuthor
#30 1547160-30-rules-variables-direct-input.patch4.55 KBmitchell
PASSED: [[SimpleTest]]: [MySQL] 327 pass(es).
[ View ]
#30 1547160-9--30-interdiff.txt1.23 KBmitchell
#17 patch denied.jpg64.12 KBIWasBornToWin
#9 1547160-9-rules-variables-direct-input.patch4.09 KBmilesw
PASSED: [[SimpleTest]]: [MySQL] 327 pass(es).
[ View ]
#6 1547160-4-rules-variables-direct-input.patch4.26 KBmilesw
PASSED: [[SimpleTest]]: [MySQL] 327 pass(es).
[ View ]

Comments

Wow, I've just spent the day trying to figure this out. None of the variable tokens I use in Direct Input mode get translated, even though they're all text values. Using the Data Selector they work just fine.

Still not sure if it's a bug or by design. Glad I'm not the only one scratching my head.

These issues make me think it could be a bug...

#1415370: Variables as tokens in direct input mode
#1499386: Token unavailable for action provided variables.
#1183676: improve support for arbitrary data structures
#1262904: Why are Replacement Patterns for Direct Input much more restricted than Data Selectors?

Alright, dug into the Rules code today and here's what I found:

  • Rules does not currently support text variables in Direct Input mode.
  • Rules does not currently support other simple variables types like integer, decimal, uri in Direct Input mode.

Why? See the RulesTokenEvaluator class. Tokens without a semicolon are completely ignored. So [variable-name] will never be translated as a token. This is not actually Rules, it's the core token system. See token_scan().

Title:Attempting to use a rule text variable in "Set a data value" using direct input modeAdd note to UI about unavailability of some variables in direct input mode
Component:Rules Engine» User interface
Category:support» feature

Wow, thanks for all the research Miles!

I was wondering where this issue should go. It sounds from the other issues you highlighted, specifically
#1415370: Variables as tokens in direct input mode
that extending the availability of tokens available in direct input mode has already been requested (although that issue is marked as a bug report).

Looking at the other related issues you found, it's obvious that this behaviour affects a lot of different uses of variables. I tried putting
drupal rules does not support text variables in Direct Input mode
in to Google but did not get anything obviously relevant apart from this post. A short term solution to avoid more people posting to the Rules issue queue on related topics would seem to be adding a note to the UI in Direct input mode to say, "Note: Not all variables available to data selection mode are available in Direct input mode."

Miles, what do you think? And, if you think it's a good idea, are you in to the Rules code enough to patch this easily?

Title:Add note to UI about unavailability of some variables in direct input modeSupport variable substitution in direct input mode
Version:7.x-2.1» 7.x-2.x-dev
Component:User interface» Rules Engine
Status:Active» Needs review

@crantok: I gave it a shot and have a decent patch here. Since you've outlined the use case so well, I'd rather post the patch here than in the other issues.

This patch adds functionality to RulesTokenEvaluator to look for variable tokens inside direct input text.

- Non-variable tokens (real tokens) work exactly as before.
- Variables can be specified using [variable_name] or [variable-name].
- Variable tokens cannot contain colons, meaning you cannot directly access sub-properties of custom non-entity structures. So [my_object_variable:my_text_property] will not work. Instead, you should first create a new rules variable from that sub-property.
- Variables must be of type text or a derivative of text (integer, decimal, uri).
- The "Replacement patterns" now includes a list of variables suitable for embedding in text.

If this works out, we should add some notes in the online documentation (which is linked under Replacement patterns).

It would be great to have some Rules developers take a look at this since I'm new to The Rules Way.

Removing duplicate post. Crappy wireless fail...

StatusFileSize
new4.26 KB
PASSED: [[SimpleTest]]: [MySQL] 327 pass(es).
[ View ]

Weird, patch and issue settings got lost. Trying again...

Great! I'll try that out.

I tried applying the patch but git status showed no changes :( This is the tail of the output...

.../1547160-4-rules-variables-direct-input.patch:62: trailing whitespace.
warning: squelched 2 whitespace errors
warning: 7 lines add whitespace errors.

StatusFileSize
new4.09 KB
PASSED: [[SimpleTest]]: [MySQL] 327 pass(es).
[ View ]

My bad, editor wasn't set to trim whitespace. Here is a properly trimmed patch.

Wahoo! That totally worked for me.

(BTW, patch didn't apply the first time because I applied it to the wrong repo (blush) )

Glad to hear it :)

Surprised that this has not got more attention.

The patch clearly solves the problem for the test case above, and solves the same problem for the larger use case I was aiming at originally (building up an email body string incrementally). I was wondering whether to mark as "Reviewed and tested by the community" but I don't feel that I've got sufficient knowledge of Rules module to do that.

I followed the "View details" link for the patch and see that its testing is marked as "Postponed". Any idea if this is normal?

I'd say it definitely needs to be reviewed by maintainers, but they're a busy bunch. I did run all tests locally and everything was passing.

I'm unable to apply patch from #9 to rules 7.x 2.1 or dev. Am I doing something wrong? Also, in the patch it shows changes to
--- a/modules/system.eval.inc
+++ b/modules/system.eval.inc

I don't see this anywhere within any rules directory?

Am I missing something? I'm no expert on patches, at all :)

Thanks

Component:Rules Engine» Rules Core

> If this works out, we should add some notes in the online documentation.
Yes, definitely. I made some changes to the Data selection docs to add a section for Direct input. This should make it easier to apply the updates started in #4, #12, OP, and #1415370: Variables as tokens in direct input mode.

> Surprised that this has not got more attention. The patch clearly solves the problem for the test case above... I was wondering whether to mark as "Reviewed and tested by the community"...
After two or three reviews, that's a good thing to do. But the code maintainers will still need to be sure that the solution is sound and won't break anything else before they'll commit it. The best thing to do as a reviewer is to provide information for them that proves it works as described and doesn't break anything else for you. And then, of course, help with the docs.

Marked #1415370: Variables as tokens in direct input mode as a duplicate.
IWasBornToWin: here you go, http://drupal.org/patch/apply

Thanks for the quick reply. Perhaps I misled you. While I am no expert I do know how to apply patches. I use Net beans. The patch will not take. I tried with the regular and dev version of rules.

StatusFileSize
new64.12 KB

See screenshot.

The patch is against Rules 7.x-2.x-dev. It still applies cleanly for me. Are you using 6.x maybe? There's no "modules" subdirectory in that version.

I tried it with dev but it still wouldn't apply. And I tried it prior to modules (7.x. 1) but for anyone else reading this. I changed it manually in 7.x.1 and it works great.

Status:Needs review» Reviewed & tested by the community

I am also reporting that #9 correctly fixes this problem.

I'm marking it "reviewed & tested" since 3 people (myself included) have now reported its success.

I successfull applyed this patch to 7.x-2.x-dev. Text variables are replaced as it's expected now. All other token replacements are working fine as well.

Can this patch be committed please? Great work!

+1 to commit, really useful

Patched 2.1 version with #9, works perfectly.

Status:Reviewed & tested by the community» Needs work

Thanks for that patch. Finally a review.. :

+++ b/modules/system.eval.inc
@@ -152,7 +152,7 @@ class RulesTokenEvaluator extends RulesDataInputEvaluator {
+    $this->setting = token_scan($text) || _rules_system_find_variables($text) ? TRUE : NULL;

hm, it's odd that token_scan() does not work for tokens without colons. Still, it's strange to have two different approaches for finding tokens: one by looking for all tokens and one by looking for variables.

Let's make our own token_scan variant and use only that instead. It can be a method of the RulesTokenEvaluator.

+++ b/modules/system.eval.inc
@@ -170,6 +170,18 @@ class RulesTokenEvaluator extends RulesDataInputEvaluator {
       if (isset($var_info[$var_name]) && ($token_type = _rules_system_token_map_type($var_info[$var_name]['type']))) {
@@ -247,5 +279,39 @@ function _rules_system_token_map_type($type) {
+  $data_types = rules_fetch_data('data_info');
+  $valid_types = array('text', 'text_formatted', 'integer', 'decimal', 'uri');
+  if (in_array($type, $valid_types)) {
+    return TRUE;
+  }
+  elseif (!empty($data_types[$type]['parent']) && in_array($data_types[$type]['parent'], $valid_types)) {

This should lookup the 'token type' key of the data type, _rules_system_token_map_type() already supports that. Let's just expose Rules' token integration that adds support for those data types, as we've done in d6 and add the mappings to the respective Rules data types. We should better prefix the token types with 'rules_' though, thus do 'rules_text', 'rules_duration', ..

+++ b/modules/system.eval.inc
@@ -247,5 +279,39 @@ function _rules_system_token_map_type($type) {
+function _rules_system_find_variables($text) {

Let's include all helper ins the class and use "protected" for making helpers private. The token_scan() replacement can be public I think though.

Priority:Normal» Major

@milesw: is this still on your radar? @fago: maybe you could commit this with todos and #25 could be made into a followup, just in case milesw can't get to these improvements soon.
--

I made some updates to the Data selection docs page in anticipation of this issue. Marked #1183676: improve support for arbitrary data structures as a duplicate of this issue and raised priority.

@fago, thanks for the review! And @mitchell, thanks for the reminder. I won't have a chance to work on this for a few more weeks. If anyone else is eager and wants to jump in, feel free.

I can't apply the patch correctly :(
+1 to commit

Patch in #9 applied no problem to 7.x-2.2 and solved some headaches.

+1 to commit, or the variant in #25

Assigned:Unassigned» klausi
Status:Needs work» Reviewed & tested by the community
StatusFileSize
new1.23 KB
new4.55 KB
PASSED: [[SimpleTest]]: [MySQL] 327 pass(es).
[ View ]
new1.29 KB
new4.61 KB
PASSED: [[SimpleTest]]: [MySQL] 327 pass(es).
[ View ]

This updated patch includes TODOs for #25 to be done in #1774630: Make our own token_scan() variant. IMHO, this is an appropriate course.

@milesw: I don't mean to cut you off, but I know users are running your patch in production and some documented features depend on it. I'd love to see you continue this in the follow up issue, should klausi and fago agree with this approach.

EDIT: See the 3rd and 4th files.

Status:Reviewed & tested by the community» Needs review

Please do not RTBC your own patches.

Status:Needs review» Reviewed & tested by the community

Works like a charm!

Assigned:klausi» fago
Status:Reviewed & tested by the community» Needs work

This updated patch includes TODOs for #25 to be done in #1774630: Make our own token_scan() variant. IMHO, this is an appropriate course.

uhm, let's do it properly *now*. I'll take a look at it.

Thank you. The Patch in #30 works great.

turns out we have two issues and patches for the same problem. see #812058: add token support for non-entities

I like the approach used over there, which makes all variables within a rule available as [rule:variable_name]. No need for custom token_scan().

Status:Needs work» Closed (duplicate)

After giving this more thought I think we should follow what I've outlined at http://drupal.org/node/812058#comment-6536478. Also, let's better use a single issue so I'm closing this one. Please add further comments to the other one.

Issue summary:View changes

Adding notice of cross-posting.

Does patch in #9 still work or has it been committed to dev. I am currently running

Drupal 7.26
Rules 2.6
Token 1.5

Incase anyone else happens to come across this post experiencing the same problem I had.... Tokens will work in direct input mode but I had to add an action to fetch the entity by ID before I could use any of its tokens.