I have an action that provides me with a file entity, and I would like to the set the file entity as the value of node:field_image.

However, It seems I can only set it as the value of node:field_image:file . This works fine if the field has a value at the start of the rule, but fails if it doesn't. I can't set the value of the whole field.

Is this a bug? or am I doing it wrong?

Comments

OnkelTem’s picture

Component: Rules Core » Rules Engine

I confirm this.

I have a loop in which I use data_set action to set custom field of type Image to some image. If there were no images, then nothing is set.

UPDATE

I figured out the reason hitting my special case since I was creating images on Commerce Products, not nodes.

dynamicdan’s picture

Title: Can't set the value of an image field when the field previously had no value. » Can't set the value of any field when the field previously had no value.
Priority: Normal » Major

I've just run into this problem and I think I know why it doesn't work...

I have an integer field that I'm trying to set a value using php: "return time();". The field in drupal has a default value of 0 when creating NEW content items. The old ones have no value (equivalent to NULL I guess). The rules module probably doesn't get any value to process when trying to save the item and therefore $value (the available var) is probably NULL.

It could however also be failing the field condition ("entity must have field"). I haven't debugged that far yet but the problem is definitely there and should be obvious to a maintainer/fixer.

Note that I expected to return "return array('und'=>array('value'=> time() ));" but I guess Rules/Entity tries to be smarter than that???
For new nodes I'm guessing the default array structure is something like "$node->field_published_date = array()". Perhaps no value can be obtained by Rules when it's like this.

David Stosik’s picture

Title: Can't set the value of any field when the field previously had no value. » Action to initialize the content of a field properly
Component: Rules Engine » Rules Core
Category: bug » feature
Priority: Major » Normal

Hello,
dynamicdan> sorry but I can't reproduce your bug with a simple text field, even when multiple.
But rlmumford, I can reproduce your bug.
An image field consists in an file entity, but it is not enough. Thus, when trying to set node:field_image:file on a node which image field is already set, it works, and the the node's image field get the wanted image.
On the other hand, if you try to set node:field_image:file on a node where no image was set, then it doesn't work and produces the notice:

Undefined index: display_default in entity_metadata_field_file_create_item() (line 579 of /home/sto/dev/git/sandbox/sites/all/modules/entity/modules/callbacks.inc).

On the other way, setting node:field_image using another field_image (from another node, for example) works perfectly.

I would say it is node a bug, but a limitation due to the way Rules is coded. Requesting for a way, in Rules, to initialize a field (whatever the field), properly, sounds appropriate.

Regards,

David