I have a Computed field to calculate a value (if another field has value A, count nodes with the same value, add 1 and then use the result for the computed field value, auto-increment-like). This field is then used by Rules to create the node title.
Each time I save a node, this field is calculated. Fine.
Each time I update a node, the field is not calculated. Fine.
But I also need to clone a node and each time I clone a node, the field is NOT calculated though I need it to be recalculated. Consequently, the node title is identical and the computed field value of the cloned node is wrong.
I have tried Rules but a computed field does not support writing with an action like “Set a data value”. Then I tried to abort Computed Field and use Rules to set a data value using PHP evaluation but now I have trouble with tokens. I need to call them in PHP Evaluation and did not manage to… the PHP syntax is different between Computed Field and Rules…
How do I make Computed Field and Node Clone working together?
Thanks for any help.
Interesting questions :
Using computed field value in Rules
EDIT 1 :
I have tried to transfer my code from Computed Field to Rules and I think I am very close. But I still need an advice about tokens. How do I call a token in PHP evaluation (action Set a data value)? token_replace
? For example, if I need the [node:nid]
, how do I get it in a variable $nid
? ($node
is not available in this Rules action...)
Thanks for any tips about this PHP syntax.
EDIT 2 :
As requested, here is my rules export, inspired by the Computed Field code. The problem is the query to get the NID of the Entity Reference Field (field_site_id_target_id
). I worked with field_get_items
in Computed Field but in Rules PHP Evaluation, it does not work (?). I know that this value is available in a token ([node:field-site-id:nid]
) but I do not know how to call it in the PHP Evaluation. I also tried to "Add a variable" to fetch this NID but still I do not know how to call it in the PHP evaluation.
I am sure that someone can solve this in this forum :)
{ "rules_titre_auto_sample_2" : {
"LABEL" : "Titre auto Sample 2",
"PLUGIN" : "reaction rule",
"OWNER" : "rules",
"REQUIRES" : [ "php", "rules" ],
"ON" : {
"node_insert--sample" : { "bundle" : "sample" },
"node_update--sample" : { "bundle" : "sample" }
},
"DO" : [
{ "data_set" : {
"data" : [ "node:field-sample-number-2" ],
"value" : {
"select" : "node:nid",
"php" : { "code" : "$nid_site=db_query(\u0022SELECT field_site_id_target_id FROM {field_data_field_site_id}\u0022)-\u003EfetchField();\r\n$result=db_query(\u0022SELECT count(*) FROM {field_data_field_site_id} WHERE field_site_id_target_id=\u0022.$nid_site)-\u003EfetchField();\r\n$value=sprintf(\u0027%03d\u0027,$result+1);\r\nreturn $value;\r\n" }
}
}
},
{ "data_set" : {
"data" : [ "node:title" ],
"value" : "[node:field-site-id]-[node:field_sample_number_2]"
}
}
]
}
}
EDIT 3 :
As requested to understand the problem :
Create Node/1 with title Site A – 001 (Field Site ID = Site A, Sample Number = 001 (computed)) OK
Create Node/2 with title Site A – 002 (Field Site ID = Site A, Sample Number = 002 (computed)) OK
Edit Node/2 : title unchanged. OK
Create Node/3 with title Site B – 001 (Field Site ID = Site B, Sample Number = 001 (computed)) OK
Clone Node/3 for Node/4 with title Site B – 001 (Field Site ID = Site B, Sample Number = 001 (computed)) ERROR : should be Site B – 002.
Cloning the node paste the Sample number value instead of a new calculation.
Hope this helps.
EDIT 4 :
Answer to Pierre Vriens about the first answer and the option "Save then edit" for Node Clone.
Unfortunately it does not work for my original problem. You are right, with the option "Save then edit", the Rules is triggered if I use the After cloning Event. But still, my computed field is not re-computed, the field value is cloned and then the title is the same.
For my case, I only see two ways :
abort Computed Field and only work with Rules and PHP Evaluation
OR
find a way to force the re-computing of my computed field when a content is saved or updated. I know there is an option on Computed Field to recalculate but it is working every time it is displayed, giving me some weird results with non-stop increasing number...
The problem is that a computed field does not support writing... So the action Set data a value from Rules can not be used here.
Besides, with your option, it will save a clone immediately and it is not appropriate for my clients : for example, if they just want to try the cloning procedure, they will have to search for the new content and delete it and I want this as user-friendly as possible.
I will re-think my Computed field and see what happens...
EDIT 5 :
After a long run of different combinations between Computed field, Node Clone, Rules, Auto-Label and Pathauto, I am really close to something.
Let me sum up the question so far :
- A Field A is calculated with Computed field, each time a content is saved.
- This field A is an auto-increment one, based on another field's value (Field B). See Edit 3 for an example of the results expected.
- The Automatic Entity Labels module allows me to hide the title in content form (with no pattern for the title)
- Rules is triggered each time a content is saved or updated to build the node title (= Field B - Field A).
- Node Clone allows me to clone a content BUT the Field A is not re-computed and consequently the node title is the same.
Based on my tests and comments from Pierre Vriens, I have tried a new angle.
Computed Field has an option called "Recalculate the field value every time" and I have tried to play with it. Now, I managed to have a computed field and title when creating a node or cloning a node!
BUT I still have a last problem : when I saved a content, I get a « Page not found »...
Actually, Pathauto is working to create an URL alias for each node ([node:content-type]/[node:title]
). Surprisingly, the node is well created with the right URL alias (available in Content Overview) but just after saving the new content (or cloning) a « Page not found » is displayed. I suppose this time I have a problem with the actions sequence.
Any idea for this last obstacle ?
As soon as everything is fine on this procedure, I will create a clear answer with each step I used.