Closed (fixed)
Project:
Rules
Version:
7.x-2.x-dev
Component:
Documentation
Priority:
Major
Category:
Feature request
Assigned:
Unassigned
Issue tags:
Reporter:
Created:
24 Jan 2012 at 18:14 UTC
Updated:
4 Jan 2014 at 01:39 UTC
Jump to comment: Most recent
Comments
Comment #1
adam.t commentedAny feedback on this would be greatly appreciated.
Comment #2
SilviuChingaru commentedI'm looking for something like this too...
Comment #3
SilviuChingaru commentedThis is not possible I think because vars are not passed by reference to eval. I think this is more a feature request now...
Comment #4
Gr3fweN commentedI want to know that too...
Comment #5
sorensong commentedSame here. I can't get much php to actually work with this action.
Comment #6
mitchell commentedThe php module is generally frowned upon for security reasons, and because it keeps you from learning how to use the api the same way every other plugin does.
Either way, if someone produces a solution here, then please create a documentation page in the handbook.
Comment #7
mitchell commentedComment #8
mitchell commentedMarked #1353564: How to modify variable in custom php code? as a duplicate.
Comment #9
timodwhit commentedWould it be more beneficial to write a custom action?
Comment #10
mitchell commentedcustom action > php text box
reconfigurable / contributed action > custom action
Comment #11
webroru commentedCan you tell in greater detail, please?
Comment #12
mitchell commentedaa2007: learn the api, don't use the php module.
timodwhit: in general yes. in this case the actions is already available...
>> How do you declare a new variable ... that will be accessible later in the rules action stack?
Use the action: "Add a value."
Edit. Removed stale docs reference.
Comment #13
PeteS commentedMitchell: when the API works 100% of the time, I will use the API 100% of the time. In the meantime, it would be fantastic if you would get off of your high horse and either answer the question or politely pass and spare us the spam.
Comment #14
zhangtaihao commentedIMHO, Rules has a general use case in elevating special behavior from the module level to the site configuration level. By definition, then, writing a module to achieve that behavior goes against this use case.
Also, this need may have been inherited from Rules 6.x-1.x where you can use variables by reference with PHP action.
Not to say insecure PHP isn't a problem, but that it's quite provable that this need exists.
@PeteS: In code, though, robust data typing in Rules 2.x means the literal value is intentionally separated from the wrapped data that Rules can flexibly use to do its magic.
Having said that, you can use "Add a variable" to assign a new variable with PHP Evaluation. To alter an existing variable, use "Set a data value" to set a variable to itself, using PHP Evaluation to modify the value.
EDIT: Again, to echo @mitchell's point, this should be done only as a last resort.
Comment #15
mitchell commentedOkay sure. #12 wasn't very nice and, overall, this issue wasn't one I was committing serious attention to (I'll admit, when I see "how do I use PHP module to do... my brain turns off a bit; this isn't good for ramping up contributions, so I'm sorry all), but regardless, I've worked very hard over the years on Rules support requests and documentation trying to make Rules what we all know it can/will be, so cut me some slack.. And, I disagree that requesting help on the docs is spam. As of yet, only ~1% of 1% of Rules users help with it. Note from the OP: "This seems like a very standard behavior but I can't find anything about it in the documentation."
#1203886: Remove the PHP module from Drupal core will answer all your questions on why using PHP text is a pattern that Drupal is trying to steer away from upstream. That change will also break your sites in D8, so I hope everyone here sees the value in reusable actions. We can all benefit from them and improve in the issue queues.
Solved? Feedback?
> #9, #10, #11, and OP, "I want to do some advanced math on price using PHP.":
* #1479526: Action: modulus, ceiling, floor (rounding)
* #1600272: Calculating with lists: sum, mean, count, min, max
> Then, I want to either re-insert $price into the action stack's available variables as $price again
This is a common misconception users with imperative programming backgrounds have when using Rules. There's never a need to modify, convert, overwrite, replace, etc a variable in an execution process. You're only generating new values based on existing values, so you'll have variables like 'incoming_price' and 'outgoing_price' or 'subtotal' and 'total_with_tax'. I'd be happy to have anyone with a stronger CS background and familiarity with Rules explains this, but AFAIK, Rules uses immutable objects because it provides a flow-based, functional programming environment. See also #1681510: Add plugin factory to UI for more info on this from someone who can explain it a lot better.
Note: That tag should probably become 'data transforms and calculations'.
Comment #16
mitchell commentedLet's follow up with the PHP variable adding part of this issue in #1223218: PHP Filter: 'Add a variable' text token does not work and mark the rest as fixed. The documentation aspect should be covered in #1393336: Improve Data selection, Data availability, and Setting data values docs. Also, there is still also a lot of work to do on the referenced calculation issues, many with patches that need review.