0

I'ld like to programmatically trigger some very basic email notification when a new revision of a node is being created. I want to send a link to the compare page /node/$nid/revisions/view/$old_vid/$new_vid.

But I wonder on which hook when saving a node do I have the old and the new vid available at once? hook_node_presave() looks like a dead end to me.


This question is not specifically about D7. Though it's D7 I'm using right now, I'ld prefer to find a solution for both D7 and D8.

0

You simply can add a custom submit callback to the node form.

/**
 * Implements hook_form_alter().
 */
function MYMODULE_form_alter(&$form, &$form_state, $form_id) {

  $node_forms = [
    'article_node_form',
  ];

  if (in_array($form_id, $node_forms)) {

    // Add a custom submit callback.
    $form['actions']['submit']['#submit'][] = '_MYMODULE_postsave';
  }
}

There you can access the original vid from the $form_state array. And when you load the freshly saved node you'll have the new vid available.

/**
 * Custom submit callback.
 */
function _MYMODULE_postsave($form, &$form_state) {

  $old_vid = $form_state['values']['vid'];
  $nid = $form_state['values']['nid'];
  $node = node_load($nid);
  $new_vid = $node->vid;

  // Voilà!
  dpm($old_vid, 'OLD_VID');
  dpm($new_vid, 'NEW_VID');

  // Do your stuff here
}

For reference: https://drupal.stackexchange.com/a/221041/15055

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.