2

I've some string like "11/28/2012" and i want use to populate a date field in my nodes/entity. In rules i use "set a value", select the date field and add php code:

$mydate = field_get_items(($entity_type, $entity, 'field_textdate');

$value = date('m-d-Y', $mydate);

return $value;

But when i execute the rules, i get 46.397 ms Unable to modify data "node:field-datone": Invalid data value given. Be sure it matches the required data type and format. 57.754 ms Unable to evaluate action data_set.

How can i do?

1 Answer 1

2

field_get_items() returns an array of items – try strtotime('m-d-Y', $mydate[0]['value']);. Also, it looks like you have an extra ( on your field_get_items call:

field_get_items((

I would recommend installing the devel module, then you can add this code anywhere you'd like and get the results printed in your messages area for quick debugging and to see what data is available to you and how it's nested:

$mydate = field_get_items($entity_type, $entity, 'field_textdate');
dpm($mydate);
5
  • forget my previous commnent. tnx, but if i write: $timestamp = field_get_items('entity', 349, 'field_unixtimestamp'); $value['UND'][0]['value'] = $timestamp['UND'][0]['value']; return $value; the error is Cannot use a scalar value as an array in eval() (linea 2 di /home/tcbkitrp/public_html/sites/all/modules/rules/modules/php.eval.inc(146) : eval()'d code).
    – user11743
    Nov 30, 2012 at 8:09
  • Just return the $value. Rules should set the value for the element in the current language. Also it's 'und', not 'UND'. Nov 30, 2012 at 15:58
  • yes, it's 'und', but i cannot omit che 'und'.
    – user11743
    Dec 1, 2012 at 17:40
  • Right but you don't need to return $value when $value = array('und' => array(0 => array('value')));. You should just do this: return $timestamp['und'][0]['value']; after you have done $timestamp = field_get_items(...). Dec 1, 2012 at 17:45
  • Oh, thanks. Now i can set the value of the date field with the timestamp, but not the field timezone, timezone_db etc.
    – user11743
    Dec 1, 2012 at 18:19

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.