I need to trigger some logic to recalculate certain computed fields based on what fields may have been altered when a user updates a node. So I'm looking to create a function that returns a list of of fields the user has updated.
I've come pretty far by using hook_node_update().
Currently the process is like this.
My hook_node_update looks roughly like this:
function hook_node_update($node) {
$old_node = node_load($node->nid);
// Figure out what fields have been altered.
$altered = _get_altered_fields($node, $old_node);
// Recalculate the fields *after* node save.
drupal_register_shutdown_function('_process_fields', $node, $altered);
}
And the _get_altered_fields function:
function _get_altered_fields($node, $old_node) {
$changed = array();
if ($old_node-> field_somefield != $node-> field_somefield) {
$changed[] = 'somefield';
} elseif ($old_node-> field_someotherfield != $node-> field_someotherfield) {
$changed[] = 'someotherfield';
}
return $changed;
}
The problem with the above is that I get 'false positives' (i.e. 'someotherfield' gets registered as changed even though it is not) because the $node object does not have the exact same data structure as $old_node, which was fetched from the database. In general $node has more attributes - especially if it is a taxonomy field - such as 'vid', 'weight' and 'rdf_mapping'.
I tried creating a normalize function where I unset those attributes, but it gets very messy. I figure someone must have done this before me. Any suggestions how to do this in a clean way?