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.
I'm trying to create a 'Set data value' action. I'd like to achieve this:
%node:field_datestamp_1[0]['value'] + %node:field_integer[0]['value'] = %node:field_datestamp_2[0]['value']
These are my rule's settings:
- Data to be modified:
node:field-datestamp-2
- New value to set for the specified data:
node:field-datestamp-1
(I assume this is now the $value variable in the PHP Eval.) - PHP Eval:
$addtime = %node:field_integer[0]['value']; return $value + $addtime;
When node:field_integer
is for example set to 100000 in the node form, the rule doesn't work. Apparently, the PHP isn't evaluated. However, when I change the code to $addtime = 100000
, it works fine.
How can I access the variable in the PHP data processor correctly? Please provide a fix for this!
Comments
Comment #1
fagoIn contrast to the php input evaluator, the php data processor doesn't support accessing variables yet. Patch welcome.
Comment #2
fagoComment #3
blackclover CreditAttribution: blackclover commentedSome day.
Still learning about Drupal's APIs. Really appreciate all the great work you guys are doing!
Cheers
Comment #4
mitchell CreditAttribution: mitchell commentedComment #5
firfin CreditAttribution: firfin commented#1223218: PHP Filter: 'Add a variable' text token does not work seems to be a duplicate of this issue.
Although that issue is older, I think this one has better information.
Comment #6
mitchell CreditAttribution: mitchell commentedAdding to #1,
RulesPHPEvaluator
can access variables, whereasRulesPHPDataProcessor
cannot. This is a commonly requested feature (see some more issues below), so raising priority.Comment #7
Ananya MultiTech CreditAttribution: Ananya MultiTech commentedI have posted a comment in IssuuQ of : In "Set a data value", the "Data selector" field should not be required to set/modify a value using PHP evaluation under Rules. Then I came across this. As so many people are trying and demanding this feature I think this should be useful.
I wasted my 2 days just to understand that "Data selector" field should be provided for using PHP evaluation (Otherwise your code is ignored!!!). I have some complex PHP processing to be performed on field. I also realized a fact that none of the variables are available here in this PHP block. Not even the variables passed to Rules Component. But then I have a workaround. Now I am using "Calculate a value" just prior to "Set a data value". I collect all my data there, do processing and then pass the result as variable. And then I use it directly...
Actually we need a good documentation on Rules, I feel. I m not demanding this as I know you people are doing great job here and certainly dont have enough time for documentation. But I have some observations as below : (i m mentioning it here because I found mitchell fago here.. sorry if wrong place..)
Comment #8
firfin CreditAttribution: firfin commented@Ananya MultiTech This is very much off-topic imho. It might be better to start a new issue for these kind of replies / issues.
I believe the rules documentation is really quite good actually. Have you seen http://drupal.org/documentation/modules/rules ?
Glad you want to help make drupal documentation even better. I think you might want to start here: http://drupal.org/contribute/documentation
Comment #9
Ananya MultiTech CreditAttribution: Ananya MultiTech commentedhii firfin,
thanks for your links..
Can i/you delete my previous comment if its wrong place? sorry for trouble..
Comment #10
eliaspallanzani CreditAttribution: eliaspallanzani commentedthe php filter break the data selector. you must insert a wrong value and save, and then drupal shoe the error message and the data selector is avaible.
Comment #11
nmudgal CreditAttribution: nmudgal commentedGuys, any update/patch/workaround on this one? Thanks.
Comment #12
Jeroen94 CreditAttribution: Jeroen94 commentedI'd like to get a solution or a workaround for this request too!
Comment #13
TBarina CreditAttribution: TBarina commentedI'd like to get a solution too
Comment #14
Jeroen94 CreditAttribution: Jeroen94 commentedI've edited this question in order to make it easier to understand for other users. As this is a frequently asked feature (and a very old problem indeed), I hope there will be a fix for it soon and that more people will comment on this issue now.
Comment #15
Jeroen94 CreditAttribution: Jeroen94 commentedComment #16
ssoulless CreditAttribution: ssoulless commentedOK any new in this issue? I think this is very important, any quick or dirty solution?
Comment #17
ssoulless CreditAttribution: ssoulless commentedOK any new in this issue? I think this is very important, any quick or dirty solution?
Comment #18
ssoulless CreditAttribution: ssoulless commentedOK any new in this issue? I think this is very important, any quick or dirty solution?
Comment #19
sylvaticus CreditAttribution: sylvaticus commentedSame here. The problem with the common workaround to use PHP in "Set a value" action is that list variables don't allow to use PHP either in creation or value assignments :-/
Comment #20
dan.mantyla CreditAttribution: dan.mantyla commentedis there a way to set a variable using the "Execute custome PHP code" action??
Comment #21
dan.mantyla CreditAttribution: dan.mantyla commentedto answer my own question, it IS possible! Here is an example for how to set a DATE field from within the "Execute custome PHP code" action in Rules.
The format is extremely important! It will not work unless it matches the format exactly! The format is controlled by the 'Field Type' you have chosen for the date field of the content type - either ISO or Unix Timestamp. I have ISO. I was missing the '\T' part of the format and I was scratching my head for weeks trying to figure it out. This explains it a little: https://www.drupal.org/node/1455576
see: http://drupal.stackexchange.com/questions/203094/set-a-date-field-with-r...
Comment #22
pixelshrink CreditAttribution: pixelshrink commentedRe Dan Mantyla's comment #21, if your date field is not a Date (UNIX timestamp) or Date (ISO) field but just a normal Date field, the following code works for me:
Comment #23
geek-merlin#20-#22: I can't see how this is relevant to this issue.
Comment #24
brendanrjohn CreditAttribution: brendanrjohn commentedsylvaticus, probably 5 years too late, and only a workaround, but for lists, if you "Add a variable" of type list, you can then use php in the "Add an item to a list" action. Remember to set the Data Selector to any value to make it process your php.