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

fago’s picture

Title: Get Invalid data value given. Error Whenever I try to access a field in a node with PHP eval » Provide access to variables to the PHP data processor
Component: Rules Engine » Provided Rules integration
Category: bug » feature
Priority: Critical » Normal

In contrast to the php input evaluator, the php data processor doesn't support accessing variables yet. Patch welcome.

fago’s picture

Category: feature » task
blackclover’s picture

Some day.
Still learning about Drupal's APIs. Really appreciate all the great work you guys are doing!
Cheers

mitchell’s picture

Component: Provided Rules integration » Rules Core
Status: Active » Postponed
firfin’s picture

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

mitchell’s picture

Priority: Normal » Major
Status: Postponed » Active

Adding to #1, RulesPHPEvaluator can access variables, whereas RulesPHPDataProcessor cannot. This is a commonly requested feature (see some more issues below), so raising priority.

Ananya MultiTech’s picture

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

  • I think people choose Drupal for developing Web Applications rather than ust developing some websites. I m myself using Drupal for 2 applications : Patient Management System & MLM Software.
  • "Entity Reference, Views, Context, Rules & Panels" are distinct features/facilities of Drupal which makes it sooo powerful
  • Extremely powerful web-app can be developed using these features, which I can feel after reading thousands of pages on Drupal in last 2 months. Most important part is very less coding is required & lot things are taken care of..
  • But unfortunately, there is not streamlined simple documentation which will take u through Drupal & explain possibilities. Most of the time examples are given related to news site or articles. Where as Web App Developers has specific requirement which will require examples of custom contents... this is missing.
  • Most of tutorials/examples are mixed. So they show up everything right from D5 to D8.. modules, issues, patches and like what not. This only adds to confusion. I also visited IRC and discussed with some new-comers and realized that they are also trying to resolve some common issues which are so deeply lost just because they can not be searched properly..
  • and I should mention one observation that I generally navigated 6 pages to search for 1 type of information. This not only waste the bandwidth but also uses unnecessary server & network resources..
  • These are not complaints, please dont take me wrong.. We all love Drupal and respect you MAKERS of Drupal.. But we want something better. Its like kids demanding more and more from their parents..
  • I have been involved in development of course curriculum of computer institute. So I am confident that I should be able to write a nice walk-through for Drupal Configuration without using *much* PHP. But i dont know where to write this and how.. Can you people please guide me so that this can be a help for new comers..
firfin’s picture

@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

Ananya MultiTech’s picture

hii firfin,

thanks for your links..
Can i/you delete my previous comment if its wrong place? sorry for trouble..

eliaspallanzani’s picture

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

nmudgal’s picture

Guys, any update/patch/workaround on this one? Thanks.

Jeroen94’s picture

Issue summary: View changes

I'd like to get a solution or a workaround for this request too!

TBarina’s picture

I'd like to get a solution too

Jeroen94’s picture

Issue summary: View changes
Issue tags: +PHP, +rules, +variables

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

Jeroen94’s picture

Status: Active » Needs work
ssoulless’s picture

OK any new in this issue? I think this is very important, any quick or dirty solution?

ssoulless’s picture

OK any new in this issue? I think this is very important, any quick or dirty solution?

ssoulless’s picture

OK any new in this issue? I think this is very important, any quick or dirty solution?

sylvaticus’s picture

Same 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 :-/

dan.mantyla’s picture

is there a way to set a variable using the "Execute custome PHP code" action??

dan.mantyla’s picture

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

// set the date
$date = format_date(strtotime($data['components']['date']['value'][0] . ' ' . $data['components']['time']['value'][0]), 'custom', 'Y-m-d\TH:i:s', 'UTC'); // ISO format (yours may be Unix Timestamp)
$entity_created->field_date[LANGUAGE_NONE][0] = array(
    'value' => $date,
    'timezone' => 'UTC',  
    'timezone_db' => 'UTC',
  );

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

pixelshrink’s picture

Re 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:

$thedate = $data['components']['date']['value'][0].' 00:00:00';

// set the date
$entity_created->field_date[LANGUAGE_NONE][0] = array(
    'value' => $thedate,
  );
geek-merlin’s picture

#20-#22: I can't see how this is relevant to this issue.

brendanrjohn’s picture

Issue tags: -

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