1

I need to compare the before save/update value with the after save/update value of a node field.

Example: There is an already existing node with the title of "Hello". I then edit the node and change the title to "World".

I need to get the before value of "Hello".

function mymodule_node_presave($node){

    $before = $node->title; 
    drupal_set_message($before, 'error');  

}


function mymodule_node_update($node){

    $after = $node->title; 
    drupal_set_message($after, 'warning');  

}

But I always keep getting the same value...

enter image description here

Did I just discover a drupal bug? or what? Because I've used this method in the past with user hooks and it works as expected:

function mymodule_user_presave(&$edit, $account, $category) {

     $before = $account->field_custom['und'][0]['value'];
     drupal_set_message($before, 'error'); 

}

function mymodule_user_update(&$edit, $account, $category) {

     $after = $account->field_custom['und'][0]['value'];
     drupal_set_message($after, 'warning');  

}

enter image description here

  • 1
    I can't understand what you are asking... :/ – Jimmy Ko Dec 1 '16 at 10:22
  • @JimmyKo Node title is "Hello", I then edit the node and erase the "Hello" and instead put "World" and save node. I need to know the before/deleted value of "Hello". – No Sssweat Dec 1 '16 at 10:28
  • 1
    I confused about which situation you expect to have because you have 2 cases in the question. :P – Jimmy Ko Dec 1 '16 at 10:34
  • 1
    For new created nodes in hook_node_update you will never get any old values for any $node fields. Use node_load($node->nid) to load the previous instance of the $node and do any comparisons with it. You can also use $node->original to view the commited values to the database. – mchar Dec 1 '16 at 10:41
  • 1
    I am thinking the same thing as @mchar. – Jimmy Ko Dec 1 '16 at 10:52
1

For new created nodes in hook_node_update you will never get any old values for any $node fields. Use node_load($node->nid) to load the previous instance of the $node and do any comparisons with it. You can also use $node->original to view the commited values to the database.

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.