Drupal 7 How to trigger / enable the node event "Publish"?

In Drupal 7, I want to add an extra process when node is published. How can I fire when a Publish node event occurs?

Is there a hook for the publish node?

+4
source share
4 answers

Ayesh K answer is good.
And I also found another alternative, using Drupal "Rules" to fire a publishing event.

+2
source

With basic functionality there is no hook. But the revisioning module provides one.

However, you can workaround by checking the status of the node when updating the 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); } } 
+7
source

According to Ayesh K, I also do not know about the basic functionality. Its workaround works, but it skips the case when a published node is instantly created.

So, I expanded the code and included it in the 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; } } 
+1
source

if the trigger function for updating the node itself, change the function MYMODULE_node_update ($ node) to the function MYMODULE_node_presave ($ node)

-2
source

Source: https://habr.com/ru/post/1438190/


All Articles