Drupal 7: how to detect when a node or a comment is changed from unpublished to published or vice versa

In Drupal 7 we don't have a simple way to detect when a node or a comment switches from published to unpublished or vice versa but we can detect these actions with some php code.

Node publishing:

isset($node->original->status) && $node->original->status == 0 && $node->status == 1

Node unpublishing:

isset($node->original->status) && $node->original->status == 1 && $node->status == 0

Comment publishing:

isset($comment->original->status) && $comment->original->status == 0 && $comment->status == 1

Comment unpublishing:

isset($comment->original->status) && $comment->original->status == 1 && $comment->status == 0

 

Tags: