You are here

function comment_user_cancel in Drupal 10

Same name and namespace in other branches
  1. 8 core/modules/comment/comment.module \comment_user_cancel()
  2. 7 modules/comment/comment.module \comment_user_cancel()
  3. 9 core/modules/comment/comment.module \comment_user_cancel()

Implements hook_user_cancel().

File

core/modules/comment/comment.module, line 449
Enables users to comment on published content.

Code

function comment_user_cancel($edit, UserInterface $account, $method) {
  switch ($method) {
    case 'user_cancel_block_unpublish':
      $comments = \Drupal::entityTypeManager()
        ->getStorage('comment')
        ->loadByProperties([
        'uid' => $account
          ->id(),
      ]);
      foreach ($comments as $comment) {
        $comment
          ->setUnpublished();
        $comment
          ->save();
      }
      break;
    case 'user_cancel_reassign':

      /** @var \Drupal\comment\CommentInterface[] $comments */
      $comments = \Drupal::entityTypeManager()
        ->getStorage('comment')
        ->loadByProperties([
        'uid' => $account
          ->id(),
      ]);
      foreach ($comments as $comment) {
        $langcodes = array_keys($comment
          ->getTranslationLanguages());

        // For efficiency manually save the original comment before applying any
        // changes.
        $comment->original = clone $comment;
        foreach ($langcodes as $langcode) {
          $comment_translated = $comment
            ->getTranslation($langcode);
          $comment_translated
            ->setOwnerId(0);
          $comment_translated
            ->setAuthorName(\Drupal::config('user.settings')
            ->get('anonymous'));
        }
        $comment
          ->save();
      }
      break;
  }
}