You are here

protected function CommentDeleteManager::moveReplies in Comment Delete 8

Delete comment and move replies up one level.

Parameters

\Drupal\comment\Entity\Comment $comment: The comment entity.

1 call to CommentDeleteManager::moveReplies()
CommentDeleteManager::delete in src/CommentDeleteManager.php
Delete a comment and do something with its replies.

File

src/CommentDeleteManager.php, line 220

Class

CommentDeleteManager
Service container for comment delete operations.

Namespace

Drupal\comment_delete

Code

protected function moveReplies(Comment $comment) {

  // Get all direct replies to the comment. Each will be given to the next
  // thread up if one is available or set as a top level thread.
  $query = $this->commentStorage
    ->getQuery()
    ->condition('pid', $comment
    ->id());
  $comment_ids = $query
    ->execute();
  if (!empty($comment_ids)) {
    $comments = $this->commentStorage
      ->loadMultiple($comment_ids);
    foreach ($comments as $_comment) {

      /** @var \Drupal\comment\Entity\Comment $_comment */
      try {
        $parent_comment = $comment
          ->getParentComment();
        if ($parent_comment && ($pid = $comment
          ->getParentComment()
          ->id())) {
          $_comment
            ->set('pid', $pid);
        }
        else {
          $_comment
            ->set('pid', NULL);
        }
        $_comment
          ->save();
      } catch (EntityStorageException $e) {

        // Unable to save.
        watchdog_exception('comment_delete', $e);
      }
    }
  }
  try {
    $comment
      ->delete();
    $this->thread
      ->thread($comment
      ->getCommentedEntity());
  } catch (EntityStorageException $e) {

    // Unable to delete.
    watchdog_exception('comment_delete', $e);
  }
}