How do you declare a new variable within the "Execute Custom PHP Code" action that will be accessible later in the rules action stack? This seems like a very standard behavior but I can't find anything about it in the documentation.

For example, lets say I have a variable in the action stack like $price. I want to do some advanced math on price using PHP. Then, I want to either re-insert $price into the action stack's available variables as $price again, or set a new variable name like $new_price and have it accessible to other non-php actions lower in the list.

Comments

adam.t’s picture

Any feedback on this would be greatly appreciated.

SilviuChingaru’s picture

I'm looking for something like this too...

SilviuChingaru’s picture

Category: support » feature

This is not possible I think because vars are not passed by reference to eval. I think this is more a feature request now...

Gr3fweN’s picture

I want to know that too...

sorensong’s picture

Same here. I can't get much php to actually work with this action.

mitchell’s picture

Component: Documentation » Provided Rules integration
Category: feature » support
Priority: Normal » Minor

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

mitchell’s picture

Title: How make a data variable available to rules action stack via "Execute Custom PHP Code" action » Execute PHP action: Make data variables available
Version: 7.x-2.0 » 7.x-2.x-dev
Category: support » feature
mitchell’s picture

timodwhit’s picture

Would it be more beneficial to write a custom action?

mitchell’s picture

custom action > php text box
reconfigurable / contributed action > custom action

webroru’s picture

Can you tell in greater detail, please?

mitchell’s picture

Title: Execute PHP action: Make data variables available » How to: make data variables available
Component: Provided Rules integration » Documentation
Priority: Minor » Major

aa2007: 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.

PeteS’s picture

Mitchell: 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.

zhangtaihao’s picture

IMHO, 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.

mitchell’s picture

Title: How to: make data variables available » How to: make data variables available for calculations
Status: Active » Postponed (maintainer needs more info)
Issue tags: +data transforms

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

>> How do you declare a new variable ... that will be accessible later in the rules action stack?
Use the action: "Add a value."

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

mitchell’s picture

Status: Postponed (maintainer needs more info) » Fixed

Let'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.

Automatically closed -- issue fixed for 2 weeks with no activity.