1

I have two different content types: "Parent" and "Child".

On Parent nodes, there is a multi-value reference field where I keep some Child nodes of this node. Every child node has a field "Integer" and I keep some integer values in it.

What I want is keeping the sum of those Integer values of all child nodes of a Parent inside a field "Sum".

So I will have:

  • Parent 1 (sum=11)
    • Child one (integer=3)
    • Child two (integer=6)
    • Child three (integer=2)

Is it possible?

0

I managed to do this with Rules module, to keep it short.

  1. By default, gave 0 to the value of integer for Parent content type.

  2. Added a rule to calculate the value of Parent's integer field by summing up the values of child nodes and setting it to Parent's integer value. Event is creating new content

  3. Added a rule to recalculate the value of Parent's integer field when adding or updating a Child content. For this, I am calculating the difference of new and old integer values of Child node. Then, adding this value to Parent's integer value.

If anyone is interested, I can also give the details of the rules.

Below is an export of the second rule:

{ "rules___aa_test" : {
"LABEL" : "__aa_test",
"PLUGIN" : "reaction rule",
"REQUIRES" : [ "rules" ],
"ON" : [ "node_update" ],
"IF" : [
  { "entity_has_field" : { "entity" : [ "node" ], "field" : "field_parent_story" } },
  { "entity_has_field" : { "entity" : [ "node" ], "field" : "field_time_left" } },
  { "entity_has_field" : { "entity" : [ "node:field-parent-story" ], "field" : "field_time_left" } }
],
"DO" : [
  { "data_calc" : {
      "USING" : {
        "input_1" : [ "node:field-time-left" ],
        "op" : "-",
        "input_2" : [ "node-unchanged:field-time-left" ]
      },
      "PROVIDE" : { "result" : { "difference" : "Difference" } }
    }
  },
  { "data_calc" : {
      "USING" : {
        "input_1" : [ "node:field-parent-story:field-time-left" ],
        "op" : "+",
        "input_2" : [ "difference" ]
      },
      "PROVIDE" : { "result" : { "result" : "Calculation result" } }
    }
  },
  { "data_set" : {
      "data" : [ "node:field-parent-story:field-time-left" ],
      "value" : [ "result" ]
    }
  }
]

} }

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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