/**
* Implements hook_preprocess_HOOK().
*/
function blacklist_preprocess_field(&$variables) {
$field_name = $variables['element']['#field_name'];
if ($field_name === 'comment_forum') {
/** @var \Drupal\node\NodeInterface $node */
$node = $variables['element']['#object'];
$comment_storage = \Drupal::entityTypeManager()->getStorage('comment');
// Select comments from blacklisted user.
$query = $comment_storage->getQuery()
->accessCheck()
->condition('entity_id', $node->id())
->condition('entity_type', 'node')
->condition('uid', '963083') // DEBUG!!!!
->condition('status', 1)
->sort('created', 'DESC');
$comment_ids = $query->execute();
$comments = $comment_storage->loadMultiple($comment_ids);
/** @var \Drupal\comment\CommentInterface $comment */
foreach ($comments as $comment) {
// Get all comments from threads from blacklisted user.
$thread_prefix = explode('.', $comment->getThread())[0];
$query = \Drupal::entityQuery('comment')
->accessCheck()
->condition('entity_id', $comment->getCommentedEntityId())
->condition('thread', $thread_prefix, 'STARTS_WITH')
->sort('thread');
$cids = $query->execute();
// Hide comments and threads from blacklisted user.
foreach ($cids as $cid) {
unset($variables['comments'][$cid]);
}
}
}
}