Support for Drupal 7 is ending on 5 January 2025—it’s time to migrate to Drupal 10! Learn about the many benefits of Drupal 10 and find migration tools in our resource center.
By jhodgdon on
Change record status:
Published (View all published change records)
Project:
Introduced in branch:
8.x
Introduced in version:
8.x
Issue links:
Description:
hook_comment_publish() and hook_comment_unpublish() have been removed in Drupal 8.x.
You can react to an update that changes the published status of a comment by using hook_entity_update(), hook_entity_insert(), or better yet hook_ENTITY_TYPE_update() and hook_ENTITY_TYPE_insert().
The update hooks will have $entity->original set to the original entity, before the changes.
Example in Drupal 7:
function mymodule_comment_publish($comment) {
// The comment has just been published. React accordingly.
}
function mymodule_comment_unpublish($comment) {
// The comment has just been unpublished. React accordingly.
}
Drupal 8:
function mymodule_comment_update($comment) {
if ($comment->isPublished() && !$comment->original->isPublished()) {
// The comment has just been published for the first time. React accordingly.
}
elseif (!$comment->isPublished() && $comment->original->isPublished()) {
// The comment has just been unpublished for the first time. React accordingly.
}
}
function mymodule_comment_insert($comment) {
if ($comment->isPublished()) {
// A new published comment has just been created. React accordingly.
}
else {
// A new unpublished comment has just been created. React accordingly.
}
}
Impacts:
Module developers