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" ] } }
    ]
  }
}
Support from Acquia helps fund testing for Drupal Acquia logo

Comments

milesw’s picture

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?

milesw’s picture

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().

crantok’s picture

Title: Attempting to use a rule text variable in "Set a data value" using direct input mode » Add 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?

milesw’s picture

Title: Add note to UI about unavailability of some variables in direct input mode » Support 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.

milesw’s picture

Removing duplicate post. Crappy wireless fail...

milesw’s picture

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

crantok’s picture

Great! I'll try that out.

crantok’s picture

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.
milesw’s picture

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

crantok’s picture

Wahoo! That totally worked for me.

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

milesw’s picture

Glad to hear it :)

crantok’s picture

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?

milesw’s picture

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.

IWasBornToWin’s picture

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

mitchell’s picture

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

IWasBornToWin’s picture

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.

IWasBornToWin’s picture

FileSize
64.12 KB

See screenshot.

milesw’s picture

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.

IWasBornToWin’s picture

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.

coreyp_1’s picture

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.

pluess’s picture

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.

IWasBornToWin’s picture

Can this patch be committed please? Great work!

andypost’s picture

+1 to commit, really useful

fluffy’s picture

Patched 2.1 version with #9, works perfectly.

fago’s picture

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.

mitchell’s picture

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.

milesw’s picture

@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.

franxo’s picture

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

onelittleant’s picture

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

+1 to commit, or the variant in #25

mitchell’s picture

Assigned: Unassigned » klausi
Status: Needs work » Reviewed & tested by the community
FileSize
1.23 KB
4.55 KB
1.29 KB
4.61 KB

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.

fago’s picture

Status: Reviewed & tested by the community » Needs review

Please do not RTBC your own patches.

Fabianx’s picture

Status: Needs review » Reviewed & tested by the community

Works like a charm!

fago’s picture

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.

Chipie’s picture

Thank you. The Patch in #30 works great.

fago’s picture

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

milesw’s picture

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().

fago’s picture

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.

fago’s picture

Issue summary: View changes

Adding notice of cross-posting.

Kcannick’s picture

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

Kcannick’s picture

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.

subhojit777’s picture

I can confirm #30 patch is working (the 4.61 KB one), although the patch does not applies cleanly.

roynilanjan’s picture

Although the #30 patch has applied to version *7.x-2.9* but no replacement patterns are found on direct input mode.
Please have a look the export,

{ "rules_custom_event" : {
    "LABEL" : "Custom event",
    "PLUGIN" : "reaction rule",
    "OWNER" : "rules",
    "REQUIRES" : [ "rules", "moviemanagement_apachesolr_configuration" ],
    "ON" : { "moviemanagement_apachesolr_configuration_rule_event" : [] },
    "IF" : [
      { "node_is_of_type" : {
          "node" : [ "movie-management" ],
          "type" : { "value" : { "movie_management" : "movie_management" } }
        }
      }
    ],
    "DO" : [
      { "moviemanagement_apachesolr_configuration_view_message" : { "user" : [ "site:current-user" ], "node" : "249", "roles" : "name" } }
    ]
  }
}

In action hard-coded nid have been placed without any replacement-pattern

roynilanjan’s picture

Status: Closed (duplicate) » Needs review
roynilanjan’s picture

fago’s picture

Status: Needs review » Closed (duplicate)

This should work already, see #812058: add token support for non-entities. For any follow-up items, please open a new issue.