2

I have a node that holds an entity reference field (multi value) to other nodes, all the referenced nodes of the same type.

In the referenced nodes I have a computed field that do some calculations. Also, in the parent node I have a computed field that takes calculated values from referenced nodes and do other calculations. All the values are stored in DB.

What I need is every time a referenced node is saved, also to re-save (update) the parent node, so that computed field can make the calculations using the new values from the referenced node.

Is it possible to get the NID of the parent node in the referenced node computed field, so I can add some code to launch the parent re-save?

All I can think right now is using SQL or adding a field (in referenced nodes) that will hold the parent NID when the referenced nodes are created; is it another way?

Thanks.

4

With the logical structure that is mentioned, it is possible that a node may have more than one parent. Wouldn't it be wiser to make a reference upwards than downwards? You would end up with an entityreference field of granularity 1. Unless there is some other reason for which to keep this structure.

There is no explanation on the specifics of this calculation that takes place. Is it in a custom module, is it rules, is it some other contrib module?

In any case, you can use an EntityFieldQuery in order to get a list of all nodes that reference the particular node. More details about how to use here. There is a particular reference for entityreference fields:

Likewise an image field would use 'fid', 'alt', and 'title' as column names; an entity reference field would use 'target_id' and 'target_type' as column names.

Example (in CAPS the variables that you need to handle):

<?php
$query = new EntityFieldQuery();
$query->entityCondition('entity_type', 'node')
  ->entityCondition('bundle', 'MY_CONTENT_TYPE')
  ->propertyCondition('status', 1) // published? yes
  ->fieldCondition('field_MY_ENTITYREFERENCE', 'target_id', $MY_CURRENT_NODE_ID)
  ->range(0, 10); //do not forget the semicolon at the end of the query conditions
$result = $query->execute();
if (isset($result['node'])) {
  $items_nids = array_keys($result['node']);
  $items = entity_load('node', $items_nids);
}
?>

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.