0

I have a boolean field ( field_rejected ) in my custom comment implementation ,I need to set this field to false if this comment is published

I tried hook_comment_update and hook_comment_insert, This didn't work as it says in the documentation :

"Note that hook implementations may not alter the stored entity data."

Then I tried doing this using hook_entity_presave but this also didn't work as it results in an infinite loading or error code 500.

This is the code I used:

function custom_comments_notification_comment_presave($comment) {
  if ($comment->isPublished() && !$comment->original->isPublished()) {
    $comment->set('field_rejected', 0, $notify = true);
    $comment->save();
  }
}

Note that this is implemented in custom module's .module file.

How can I solve this? How can change the boolean 'field_rejected' after publishing a comment?

3
  • remove that $comment->save(); Because you are already in the save process and you will trigger it over and over again with this. And use FALSE instead of 0.
    – DiDebru
    Commented Feb 15, 2018 at 13:17
  • Thanks!!, i removed the $comment->save(); and it worked !! the 0 also works as a FALSE Commented Feb 15, 2018 at 13:20
  • Yes that was just a suggestion imho it looks a bit better :)
    – DiDebru
    Commented Feb 15, 2018 at 13:46

2 Answers 2

1

Remove that $comment->save(); Because you are already in the save process and you will trigger it over and over again with this.

0

$comment->save(); should not be there as a save will be done automatically (you are in a _presave hook)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.