You are here

protected function CommentDeleteManager::softDelete in Comment Delete 8

Soft delete comment.

Parameters

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

2 calls to CommentDeleteManager::softDelete()
CommentDeleteManager::deleteReplies in src/CommentDeleteManager.php
Delete comment and replies.
CommentDeleteManager::keepReplies in src/CommentDeleteManager.php
Soft delete comment and keep replies at current level.

File

src/CommentDeleteManager.php, line 299

Class

CommentDeleteManager
Service container for comment delete operations.

Namespace

Drupal\comment_delete

Code

protected function softDelete(Comment $comment) {
  if ($this->config
    ->get('soft_mode') === 'unpublished') {

    // Hiding a comment is the same as setting unpublished. This mode is not
    // recommended because it can cause thread indentation to go out of whack
    // (unpublished comments still count towards the thread). Mitigated by the
    // fact an unpublished comment can be published to restore indentation.
    try {
      $comment
        ->setUnpublished();
      $comment
        ->save();
    } catch (EntityStorageException $e) {

      // Unable to save.
      watchdog_exception('comment_delete', $e);
    }
  }
  else {
    $unset_author = $this->config
      ->get('soft_user');
    $fields = array_filter($comment
      ->getFields(), function ($field_machine_name) {
      return $field_machine_name !== 'field_name' && preg_match('/^field_(.*)/i', $field_machine_name);
    }, ARRAY_FILTER_USE_KEY);
    try {

      // Reset values on source and all translations.
      foreach ($comment
        ->getTranslationLanguages() as $translation) {
        $comment_translation = $comment
          ->getTranslation($translation
          ->getId());
        if ($unset_author) {
          $comment
            ->setOwnerId(0);
        }
        $comment_translation
          ->set('subject', NULL);
        $comment_translation
          ->set('comment_body', NULL);
        foreach ($fields as $field_machine_name => $field) {
          $comment_translation
            ->set($field_machine_name, NULL);
        }
        $comment_translation
          ->save();
      }
    } catch (EntityStorageException $e) {

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