I'm updating a node with nodeapi update, but there's more that I need to do behind the scenes which requires me to know the old value of a field/ Is there a way I could get the old value of a field before it's overwritten.
+2
A:
Edit
hook_nodeapi()
only acts on the new $node
object, so my earlier answer won't help you. Instead, you'll need to access the node as it gets submitted. To do that, you'll need to register your own submit handler that'll get called when the node form is submitted. It'll give you access to both the current values and the new values:
function test_form_alter(&$form, &$form_state, $form_id) {
if ($form_id === 'contenttype_node_form') { // Replace contenttype
$form['#submit'][] = 'test_submit'; // Add a submit handler
}
}
function test_submit($form, &$form_state) {
// Load the current node object
$node = node_load($form_state['values']['nid']);
// Display the current node object's values
dsm($node);
// Display the submitted values
dsm($form_state['values']);
}
update
is called the $node
object has been updated. You might be more interested in presave
, which checks the node after validation, or validate
, which checks it before validation; both $op
s fire before the new $node
object is saved.
Mark Trapp
2010-08-17 21:30:53
The answer doesn't say where to find the old value, though. When a submission handler is invoked, `$form_state['values']` contain the new values, but inside `$form` there are the old values (the `#default_value` indexes). See [`taxonomy_overview_vocabularies_submit`](http://api.drupal.org/api/function/taxonomy_overview_vocabularies_submit/6), which doesn't use the default values, though.
kiamlaluno
2010-08-18 13:28:28
I took him to mean he was trying to get the node's values (i.e. CCK fields) before it got saved (which is why he started with hook_nodeapi()). In the solution I proposed, loading the $node object in the submit handler will get the node's current state. Traversing $form is a grest general-purpose solution, though.
Mark Trapp
2010-08-18 15:54:08