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 wrote a simple rule to change a file field description when it is empty
{ "rules_lab_fix_file_desc" : {
"LABEL" : "Try to change file description if empty",
"PLUGIN" : "reaction rule",
"WEIGHT" : "-10",
"REQUIRES" : [ "rules" ],
"ON" : [ "node_presave" ],
"IF" : [
{ "node_is_of_type" : { "node" : [ "node" ], "type" : { "value" : { "mytype" : "mytype" } } } },
{ "data_is_empty" : { "data" : [ "node:field-file:description" ] } }
],
"DO" : [
{ "data_set" : {
"data" : [ "node:field-file:description" ],
"value" : "Bla bla bla."
}
}
]
}
}
but it doesn't work... the rules' log shows that the rule fires as expected but changes simply doesn't affect the string description value.
Adding some dsm() in the rule's action part I've found that the "set a data value" action does its work but the the wrong language :)
dsm($node) before the "set a data value" on node:field-avviso-file:description reports
[...]
$node->field-file['und']['description'] = ''
but after the "set a data value" action the $node contains
[...]
$node->field-file['und']['description'] = ''
$node->field-file['it']['description'] = 'Bla bla bla.'
So it seems to me that rules wrongly sets the value for another language.
Comments
Comment #1
mrfree CreditAttribution: mrfree commentedAs a temporary workaround I use a PHP action instead of the "set a data value" with something like:
$node->field_file['und'][0]['description'] = "Bla bla bla";
Comment #2
TR CreditAttribution: TR commentedIs this still an issue? You say "rules wrongly sets the value for another language", but from what I see it doesn't seem to be wrong necessarily. Rules will always operate on the object in the current language. So if your user language is set to 'it' when you edit the node, and the node:language token contains 'it' (you can check that by using a "Show system message" action in your above rule), then the "Set a data value" action will change that version of the node and NOT the 'und' version. This is true for all actions and conditions in Rules. If you want to operate on some other version of the node, or some other language, then you have to do something different.
If that's not the case, and you're still having this problem in the current version of Rules, please export your Rule again, check the node:language token, and describe how you've set up languages on your site. Also explore whether this rule operates differently for users who have different languages set.
Comment #3
TR CreditAttribution: TR commentedNo further information provided.