You are here

protected function CommentDeleteManager::deleteReplies in Comment Delete 8

Delete comment and replies.

Parameters

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

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

File

src/CommentDeleteManager.php, line 181

Class

CommentDeleteManager
Service container for comment delete operations.

Namespace

Drupal\comment_delete

Code

protected function deleteReplies(Comment $comment) {

  // Permanently delete comment and replies.
  if (!$this->config
    ->get('soft')) {
    try {
      $comment
        ->delete();
    } catch (EntityStorageException $e) {

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

  // Soft delete comment and replies.
  $thread = $comment
    ->get('thread')
    ->getString();
  $query = $this->commentStorage
    ->getQuery()
    ->condition('entity_type', $comment
    ->get('entity_type')
    ->getString())
    ->condition('entity_id', $comment
    ->get('entity_id')
    ->getString());
  $db_or = $query
    ->orConditionGroup()
    ->condition('thread', $thread)
    ->condition('thread', $this->connection
    ->escapeLike(str_replace('/', '.', $thread)) . '%', 'LIKE');
  $query
    ->condition($db_or);
  $comment_ids = $query
    ->execute();
  if (!empty($comment_ids)) {
    $comment_thread = $this->commentStorage
      ->loadMultiple($comment_ids);
    foreach ($comment_thread as $_comment) {

      /** @var \Drupal\comment\Entity\Comment $_comment */
      $this
        ->softDelete($_comment);
    }
  }
}