4

In Drupal 7, I want to add a additional process when a node gets Published. How can I get triggered when that node's "Publish" event fires?

Is there any hook for node "Publish"?

qasimzee
  • 640
  • 1
  • 12
  • 30
夏期劇場
  • 17,821
  • 44
  • 135
  • 217

4 Answers4

8

With core functionality, there is no hook. But Revisioning module provides one.

You can however workaround by checking node's status on update OP. Not very smart though.

<?php
function MYMODULE_node_update($node){
  if (isset($node->original->status) && $node->original->status == 0 && $node->status == 1){
     MYMODULE_mymagic_func($node);
  }
}
AKS
  • 4,618
  • 2
  • 29
  • 48
3

As Ayesh K writes, I am also not aware of a core functionality. His workaround works but misses the case that a newly created node is being published immediately.

So I extended the code and wrapped it into a function:

/**
 * Checks if a node is being published.
 *
 * @param object $node
 *   The node to check.
 *
 * @return bool
 *   TRUE if node is now published and
 *     1) was not published before or
 *     2) did not exist before;
 *   FALSE in all other cases.
 */
function MYMODULE_node_is_being_published(&$node) {

  if (isset($node->original)) {
    return (
      isset($node->original->status) && 
      $node->original->status == 0 && 
      $node->status == 1
    );
  }
  else {
    return $node->status == 1;
  }
}
Community
  • 1
  • 1
Georg Jähnig
  • 779
  • 1
  • 8
  • 19
2

Ayesh K answer is good.
And i also found another alternative by using Drupal "Rules" to trigger the publish event.

夏期劇場
  • 17,821
  • 44
  • 135
  • 217
-2

if trigger function is for update node it's self, change function MYMODULE_node_update($node) to function MYMODULE_node_presave($node)