View source
<?php
namespace Drupal\comment;
use Drupal\Core\Entity\Display\EntityViewDisplayInterface;
use Drupal\Core\Entity\EntityDisplayRepositoryInterface;
use Drupal\Core\Entity\EntityInterface;
use Drupal\Core\Entity\EntityRepositoryInterface;
use Drupal\Core\Entity\EntityTypeInterface;
use Drupal\Core\Entity\EntityTypeManagerInterface;
use Drupal\Core\Entity\EntityViewBuilder;
use Drupal\Core\Language\LanguageManagerInterface;
use Drupal\Core\Session\AccountInterface;
use Drupal\Core\Theme\Registry;
use Symfony\Component\DependencyInjection\ContainerInterface;
class CommentViewBuilder extends EntityViewBuilder {
protected $currentUser;
protected $entityTypeManager;
public function __construct(EntityTypeInterface $entity_type, EntityRepositoryInterface $entity_repository, LanguageManagerInterface $language_manager, AccountInterface $current_user, Registry $theme_registry, EntityDisplayRepositoryInterface $entity_display_repository, EntityTypeManagerInterface $entity_type_manager) {
parent::__construct($entity_type, $entity_repository, $language_manager, $theme_registry, $entity_display_repository);
$this->currentUser = $current_user;
$this->entityTypeManager = $entity_type_manager;
}
public static function createInstance(ContainerInterface $container, EntityTypeInterface $entity_type) {
return new static($entity_type, $container
->get('entity.repository'), $container
->get('language_manager'), $container
->get('current_user'), $container
->get('theme.registry'), $container
->get('entity_display.repository'), $container
->get('entity_type.manager'));
}
protected function getBuildDefaults(EntityInterface $entity, $view_mode) {
$build = parent::getBuildDefaults($entity, $view_mode);
$commented_entity = $entity
->getCommentedEntity();
$build['#comment_threaded'] = is_null($commented_entity) || $commented_entity
->getFieldDefinition($entity
->getFieldName())
->getSetting('default_mode') === CommentManagerInterface::COMMENT_MODE_THREADED;
if ($build['#comment_threaded']) {
unset($build['#cache']['keys']);
}
return $build;
}
public function buildComponents(array &$build, array $entities, array $displays, $view_mode) {
if (empty($entities)) {
return;
}
$uids = [];
foreach ($entities as $entity) {
$uids[] = $entity
->getOwnerId();
}
$this->entityTypeManager
->getStorage('user')
->loadMultiple(array_unique($uids));
parent::buildComponents($build, $entities, $displays, $view_mode);
$current_indent = 0;
$attach_history = $this->moduleHandler
->moduleExists('history') && $this->currentUser
->isAuthenticated();
foreach ($entities as $id => $entity) {
if ($build[$id]['#comment_threaded']) {
$comment_indent = count(explode('.', (string) $entity
->getThread())) - 1;
if ($comment_indent > $current_indent) {
$build[$id]['#comment_indent'] = 1;
$current_indent++;
}
else {
$build[$id]['#comment_indent'] = $comment_indent - $current_indent;
$current_indent = $comment_indent;
}
}
$commented_entity = $entity
->getCommentedEntity();
$bundle = $commented_entity ? $commented_entity
->bundle() : '';
$is_node = $commented_entity ? $commented_entity
->getEntityTypeId() === 'node' : NULL;
$build[$id]['#entity'] = $entity;
$build[$id]['#theme'] = 'comment__' . $entity
->getFieldName() . '__' . $bundle;
$display = $displays[$entity
->bundle()];
if ($display
->getComponent('links')) {
$build[$id]['links'] = [
'#lazy_builder' => [
'comment.lazy_builders:renderLinks',
[
$entity
->id(),
$view_mode,
$entity
->language()
->getId(),
!empty($entity->in_preview),
],
],
'#create_placeholder' => TRUE,
];
}
if (!isset($build[$id]['#attached'])) {
$build[$id]['#attached'] = [];
}
$build[$id]['#attached']['library'][] = 'comment/drupal.comment-by-viewer';
if ($attach_history && $is_node) {
$build[$id]['#attached']['library'][] = 'comment/drupal.comment-new-indicator';
$build[$id]['history'] = [
'#lazy_builder' => [
'\\Drupal\\history\\HistoryRenderCallback::lazyBuilder',
[
$commented_entity
->id(),
],
],
'#create_placeholder' => TRUE,
];
}
}
if ($build[$id]['#comment_threaded']) {
$build[$id]['#comment_indent_final'] = $current_indent;
}
}
protected function alterBuild(array &$build, EntityInterface $comment, EntityViewDisplayInterface $display, $view_mode) {
parent::alterBuild($build, $comment, $display, $view_mode);
if (empty($comment->in_preview)) {
$prefix = '';
if ($build['#comment_threaded']) {
$prefix .= $build['#comment_indent'] <= 0 ? str_repeat('</div>', abs($build['#comment_indent'])) : "\n" . '<div class="indented">';
}
$build['#prefix'] = $prefix;
if (!empty($build['#comment_indent_final'])) {
$build['#suffix'] = str_repeat('</div>', $build['#comment_indent_final']);
}
}
}
}