1

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.

7
  • Thanks Pierre for your interest in my question. The rules export is in the question now.
    – Toki
    Dec 7, 2016 at 8:36
  • Merci for the exported rule, it helps to better understand your question. But still I'm confused about (a) what you want to happen when a node is cloned and (b) what is currently happening (or not) when a node is cloned. Can you add a small sample to the end of your question with (say) "node/1" that is created, "node/2" that gets updated and "node/4" that is actually created by cloning it from "node/3". And for each of the 3 nodes, indicate what the title and that (calculated) field value actually is, and if it yes/no matches with what you want it to be (if not: what should it be). Dec 7, 2016 at 11:10
  • Any idea Pierre?
    – Toki
    Dec 7, 2016 at 15:19
  • Thanks to you! Tell me if you need more details (computed code, other modules involved, etc.).
    – Toki
    Dec 7, 2016 at 16:13
  • Merci Pierre. Sorry for the delay, I was afk. I will try your suggestion today and get back to you!
    – Toki
    Dec 15, 2016 at 10:46

2 Answers 2

0

Have a look at this (extremely basic) rule, in Rules export format:

{ "rules_demo_of_node_cloning" : {
    "LABEL" : "Demo of node cloning",
    "PLUGIN" : "reaction rule",
    "OWNER" : "rules",
    "REQUIRES" : [ "rules", "clone" ],
    "ON" : { "clone_node" : [] },
    "DO" : [
      { "drupal_message" : { "message" : "bingo, a cloned node ... add any additional Rules Actions you want (after this action)." } }
    ]
  }
}

The above rule uses Rules Event After cloning a node (an event which is available if you have the Node clone module enabled). All this rule does is to show some message starting with "bingo, a cloned node ..." (sorry, my favorite way of unit testing any rule ...). At least that's what's supposed to happen ...

But, according to comment nr 6 in issue nr 2212039, this event is only triggered if using Node clone's configuration option "Save as a new node then edit.", and not if using the option "Pre-populate the node form fields". These options can be found at admin/config/content/clone. And sure enough, after changing this option, you get such bingo-message (so the rule gets triggered by that event).

So as a work around, you may want to use that option "Save as a new node then edit.", and then either add this Rules Event (= After cloning a node) to your existing rule. Or otherwise create a new rule with this Rules Event, and add appropriate Rules Conditions/Actions which are probably similar to the rule you have already working for new or updated nodes. Better still, by only changing that Node clone configuration option mentioned above, your existing rule (to create or update a node) may get triggered already (so you don't have to do anything else).

0

Finally, a solution has been found, without Computed Field.

I worked with all modules cited in Edit 5 except CF and create my auto-increment value with Rules :

  • After saving new content/After updating existing content
  • If Field to auto-increment is empty
  • Then Execute custom PHP code (PHP Filter core module requested) to get the right value (SQL)
  • Set a Data Value to rewrite the node title, using the previous calculated value (with Tokens)

To force a new calculation when cloning a node, I worked with the Rules event "After cloning a node" (as suggested by Pierre.Vriens) but do not forget to choose the option "Save as a new node then edit" in Node Clone's configuration. Then, I have built a Rule to calculate the value after cloning a node and rewrite the node title.

This issue led me to meet a new powerful module : Conditional Rules. Thanks to that fabulous module, you may add if, else, switch, while... within your Rules. Must have.

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.