2

I have 2 different content types "factory" and "measurement" that are related with an entity reference field. I am using a computed field to store the latest measurement as a field of the factory content type. The problem is that in order for the computed field to be "re-computed", the factory node that the measurement refers to, needs to be updated.

My first thought was to batch update all the factory nodes in predefined intervals. This has a very important downside: the factory information (and specifically the latest measurement that I'm interested in) will not be updated until the batch update is executed.

My next thought was that I could use a hook whenever a new measurement is created and update the corresponding (=the one that the entity reference points to) factory. The problem is that hook_node_insert will not give me the information that I want (i.e. the entity reference field that points to the factory) since in the hook's description it states clearly that:

You should not rely on data in the database at this time as it is not updated yet.

Is there any other way to execute a piece of code whenever a new node of specific type (i.e. measurement) is saved/created and have the node's fields available in my code? How do Rules do it?

P.S: I thought about using Rules but it seems like an overkill for such a simple thing to install a pretty heavy module.

0

1 Answer 1

1

In hook_node_insert($node) you cannot reliably read saved node's data from database, but you can read data from $node object provided to that hook.

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.