You are here

public function CommentThread::thread in Comment Delete 8

Re-thread comments attached to an entity.

Parameters

\Drupal\Core\Entity\EntityInterface $entity: The commented entity.

File

src/CommentThread.php, line 39

Class

CommentThread
Service container for comment thread calculations.

Namespace

Drupal\comment_delete

Code

public function thread(EntityInterface $entity) {

  // Get all comments attached to the entity. Not using the entity manager
  // query because its quicker to query the fields directly on big threads.
  $query = $this->connection
    ->select('comment_field_data')
    ->fields('comment_field_data', [
    'cid',
    'pid',
    'created',
  ])
    ->condition('entity_type', $entity
    ->getEntityTypeId())
    ->condition('entity_id', $entity
    ->id())
    ->orderBy('created');
  $comments = $query
    ->execute()
    ->fetchAllAssoc('cid');
  array_walk($comments, function (&$item) {
    $item = (array) $item;
  });
  $tree = $this
    ->getThreadTree($comments);
  $threads = $this
    ->getThreadValues($tree);

  // Update comment threads in database table.
  foreach ($threads as $cid => $thread_string) {
    $this->connection
      ->update('comment_field_data')
      ->fields([
      'thread' => $thread_string . '/',
    ])
      ->condition('cid', $cid)
      ->execute();
  }
}