function CommentHooks::userCancel

Implements hook_user_cancel().

Attributes

File

core/modules/comment/src/Hook/CommentHooks.php, line 376

Class

CommentHooks
Hook implementations for comment.

Namespace

Drupal\comment\Hook

Code

public function userCancel($edit, UserInterface $account, $method) : void {
  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 set the original comment before applying
        // any changes.
        $comment->setOriginal(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;

  }
}

Buggy or inaccurate documentation? Please file an issue. Need support? Need help programming? Connect with the Drupal community.