Question

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"?

Was it helpful?

Solution 3

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

OTHER TIPS

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);
  }
}

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;
  }
}

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

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top