Support for Drupal 7 is ending on 5 January 2025—it’s time to migrate to Drupal 10! Learn about the many benefits of Drupal 10 and find migration tools in our resource center.
support tokens for primitive variables + arbitrary data structures
Comment | File | Size | Author |
---|---|---|---|
#37 | rules_token.patch | 5.99 KB | fago |
#23 | rules-simple-token-812058-22.patch | 6.7 KB | das-peter |
#14 | rules-simple-token-812058-14.patch | 3.21 KB | milesw |
#13 | rules-simple-token-812058-13.patch | 3.66 KB | das-peter |
#10 | rules-simple-token-812058-10.patch | 2.63 KB | das-peter |
Comments
Comment #1
fagomarked http://drupal.org/node/1032356 as duplicate
Comment #2
hunziker CreditAttribution: hunziker commentedAny update on this?
Comment #3
mropanen CreditAttribution: mropanen commentedThis would be extremely useful. I was trying to load a taxonomy term with a tid supplied by a non-entity Token, but I suppose this is currently not possible(?)
Comment #4
MustangGB CreditAttribution: MustangGB commentedAny tips whereabouts to make a start with this?
Comment #5
patrickd CreditAttribution: patrickd commented+1
Comment #6
torotil CreditAttribution: torotil commentedHere is my first attempt on building token-support for non-entities. It successfully replaces variables of type "text".
Comment #7
mitchell CreditAttribution: mitchell commentedUpdated component.
Comment #8
Anonymous (not verified) CreditAttribution: Anonymous commentedThis issue should have a higher priority. It's two years old. And it's one of those issues making Rules fail for common users at a late state of implementing a rule.
What's the best workaround, so we could assign this issue to "Documentation"? Just kidding... :(
Comment #9
IWasBornToWin CreditAttribution: IWasBornToWin commentedPatch works well....although it's not committed to latest version of rules....will it be?
Comment #10
das-peter CreditAttribution: das-peter commentedI like the approach :)
Fixed some coding standard issues and created new patch.
I'm just about to test this - thus needs further review.
Comment #11
fagoyeah, I like the approach as well. Although it's a bit inconsistent to have e.g. a node variable as "node" replacement whereas a string would appear as rule:string ?
Comment #12
fagoalso, turns out we have two issues and patches for the same problem. see #1547160: Support variable substitution in direct input mode
Comment #13
das-peter CreditAttribution: das-peter commentedI've just added the function
_rules_system_valid_text_type()
from the other issue to enhance the patch here.Well, that really could be, but I don't think it's a huge UX issue. E.g. we could change the description from:
Replacement patterns for This rule
to
Text replacement patterns for this rule
Comment #14
milesw CreditAttribution: milesw commentedThis looks nice. I cleaned up and moved the code from _rules_system_valid_text_type() since it probably doesn't need to be a separate function.
Also changed
Replacement patterns for This rule
toReplacement patterns for variables within this rule
. How does that sound?Just to clarify, the list of variables available for replacement only includes text variables, but nothing prevents you from shoving other variables in there as tokens.
Comment #15
das-peter CreditAttribution: das-peter commentedIs not fully correct because if there's a variable of type node the tokens for this variable are listed as
Replacement patterns for Node
.That's also the reason fago mentioned the inconsistency.
A variable of type node will have replacement patterns like
[node:title]
.A variable of type text will have replacement patterns like
[rule:variable]
.Now you could argue the replacement pattern for the variable with the type node should be
[rule:node:title]
or the text variable pattern should look like[variable]
.Comment #16
milesw CreditAttribution: milesw commentedAh, I see what you mean, that's confusing. Maybe just
Replacement patterns for text variables
?Comment #17
torotil CreditAttribution: torotil commentedI think there is at least some consensus that the patch in the current version is useful and the solution is a good one (within the constraints of token). So I'd vote for committing even if the discussion about the label is still ongoing.
Indeed the functionality could and should be extended to all (non-entity) variables in the future. But ftw I think "Replacement patterns for text variables" is just fine.
Comment #18
rocketeerbkw CreditAttribution: rocketeerbkw commentedI found this issue because moderation_workflow provides
[previous-state]
and[new-state]
variables which work everywhere except when trying to send an email.The patch in #14 fixes that by providing
[rule:new-state]
. It is weird that[new-state]
works in some actions and[rule:new-state]
must be used for others. It's not a huge deal because a new section of replacement patterns is shown to list these. What about "Replacement patterns for extra variables" as a label?I do agree with @torotil that labeling shouldn't hold this up though.
Comment #19
mitchell CreditAttribution: mitchell commentedFWIW, here are my observations and questions:
I think the variable prefix and description texts can still be improved. "All variables that are available in this rule and cannot be accessed otherwise." isn't very specific. ;)
I'm not sure if the 'rule' prefix is correct, since all configured things aren't necessarily 'rules', such as action sets and condition sets. What about type-name-specific prefixes instead?
In that case, I think something like "Variables provided by this $configuration_type" might work better. But if I had to choose one prefix, I would favor 'configuration' over 'rule'.
Then, another possible change:
- "Replacement patterns for Rules variables"
+ "Replacement patterns for . [$config->name]'s . variables"
More questions:
* How well do you think it would work to put a connective between the prefix and the variable name, ie parameter, or some other triple chain?
* Could 'provided variable' ever be an option, or only parameters?
* What other token sources does this patch provide?
Comment #20
das-peter CreditAttribution: das-peter commentedExport of a test rules component:
Comment #21
fagoExactly - that's confusing. Thinking about it a simple but good solution would be to use "variable:value" for non complex data. token_scan() requires us to have a ":" as separator in the token, so let's have one. Then, we just need to do a hook_tokens() implementation inside of rules for those primitive data types (integer, text, ..) and add this data type to 'token type' in hook_rules_data_info() - see _rules_system_token_map_type().
Then, I think we should prefix our data types when exposing them to token, e.g. do rules_text and rules_integer so we stay in our namespace.
That way the implementation should be straight forward and rather simple, as it's just implementing existing APIs :-)
Comment #22
Fabianx CreditAttribution: Fabianx commented@#21: That is a very thoughtful approach. I really like that.
[variable:value] makes total sense to me and is in line with what people are used from field API.
+1 to that idea
Best,
Fabian
Comment #23
das-peter CreditAttribution: das-peter commentedI hope I got the idea of #812058-21: add token support for non-entities
Here's a new patch.
Attention: The function
rules_tokens()
handles all tokens that have a type which's prefixed withrules_
. I'm not sure if this is viable, it's very generic but maybe it's better to have a fixed list of types to handle as this would be easier to understand.I've changed the title for the patterns to
Replacement patterns for this rule's simple variables
- I think this really covers the idea of those patterns and should be understandable.Comment #24
IWasBornToWin CreditAttribution: IWasBornToWin commentedI've been watching this post. I'm still using original patch for variables. Will applying the latest patch cause me headaches and needed adjustments to all of my rules currently using variables as tokens?
Thanks
Comment #25
das-peter CreditAttribution: das-peter commentedYes - token names for simple variables will change.
Comment #26
IWasBornToWin CreditAttribution: IWasBornToWin commentedYou emphasized "simple". Does this mean some token variables won't change? Should I go ahead and apply patch now assuming whatever variables I currently have [or new ones] will, at some point, have to be changed to this new method?
Thanks
Comment #27
das-peter CreditAttribution: das-peter commentedVariables not touched by any of this patches won't change.
Yes, it's likely that you'll have to change them some-when.
The patches aren't compatible between the different approaches.
And there's not yet a final decision about how the token should look like. However since we've discussed this and fago proposed to use
[variablename:value]
it's likely this pattern will be used since fago is the one that has the power to make the necessary commit. :)Comment #28
IWasBornToWin CreditAttribution: IWasBornToWin commentedSo, [variablename:value] will replace my current tokens of [rule:variablename] ?
Comment #29
Fabianx CreditAttribution: Fabianx commentedYes, that will most probably be the case.
Comment #30
IWasBornToWin CreditAttribution: IWasBornToWin commentedSorry for continuing to ask questions. So, I need to go ahead and apply patch and go in and change variable names?
I want to do whatever I need to do before i continue creating more rules which will also have to be changed more, later. I wonder if I could apply patch and then go in sql and replace all of rules:myvaraiablname to variablename:value?
Comment #31
Fabianx CreditAttribution: Fabianx commentedYes, I think that would be the cause of action.
* Revert your patch
* Apply this patch
* Rename rules (I personally would export them to a feature and do a manual search and replace)
While you are on that:
Could you test that patch - if it works for you and mark RTBC then again?
Thanks,
Fabian
Comment #32
IWasBornToWin CreditAttribution: IWasBornToWin commentedI will try to get to this over weekend. cant rename rules, i have too many and too many components interchanged with each other. I'll just revert my patch, apply yours and change variable names per rule. Is reverting patch self-explanatory in net beans? I suspect I could replace old-applied-patch files with rules latest files also, as a way to revert?
Comment #33
Anonymous (not verified) CreditAttribution: Anonymous commentedI tested the patch in #23 and it worked for me. Would be great to see this in the next release of Rules.
Comment #34
Anonymous (not verified) CreditAttribution: Anonymous commentedChanging status to RTBC
Comment #35
sthzg CreditAttribution: sthzg commentedDid I get this right? After patching the dev version of the rules module I should be able to use [variablename:value] as a replacement token for content in a direct input field. So this should work?
1. add a variable of type text called logmessage
2. add another action to that rule that should 'Set a data value'
3. type [logmessage:value] to the direct input field
I'm asking because I am not quite sure whether I understood this right and this example is not working for me.
Any help would be appreciated. :)
--- edit ---
Okay, my bad. It works as expected. My mistake was that I didn't pick the right data selector to begin with.
Comment #36
Fabianx CreditAttribution: Fabianx commented@fago: Time to commit this then ;-).
Comment #37
fagoHm, I'm not sure why the patch still does so much magic in help()? That shouldn't be necessary once we register proper token types.
I took a stab on that and fixed this. For replacing tokens, I just leveraged entity tokens' token processor which already takes care of all our primitives - we just have to pass on a "struct" type with a wrapper as value to it. This makes it work with dates and even chains properly to support date formats.
E.g. my test with variable worked:
I've committed the attached patch. Thanks.
Comment #39
7wonders CreditAttribution: 7wonders commentedShould this work with out of the box with rules data passed in via hook_rules_data_info() ? I cant seem to get any of my non-entity tokens :(
Comment #40
adam-vessey CreditAttribution: adam-vessey commentedI've run into this as well... Given that this is closed probably good to open another ticket?...
Another note anyway: Conflicting docs?:
Comment #41
davidwhthomas CreditAttribution: davidwhthomas commentedJust noting I tried the patches from both #23 and #37 but only the patch from #23 worked for me.
Comment #42
IWasBornToWin CreditAttribution: IWasBornToWin commentedBased on changes I already made, see comment #28 above - if I install the latest rules, will I need to redo these variables again? It seems like you're using different names again?
Thanks