Chris@0: currentUser = $current_user; Chris@18: $this->entityTypeManager = $entity_type_manager; Chris@0: } Chris@0: Chris@0: /** Chris@0: * {@inheritdoc} Chris@0: */ Chris@0: public static function createInstance(ContainerInterface $container, EntityTypeInterface $entity_type) { Chris@0: return new static( Chris@0: $entity_type, Chris@18: $container->get('entity.repository'), Chris@0: $container->get('language_manager'), Chris@18: $container->get('current_user'), Chris@18: $container->get('theme.registry'), Chris@18: $container->get('entity_display.repository'), Chris@18: $container->get('entity_type.manager') Chris@0: ); Chris@0: } Chris@0: Chris@0: /** Chris@0: * {@inheritdoc} Chris@0: */ Chris@0: protected function getBuildDefaults(EntityInterface $entity, $view_mode) { Chris@0: $build = parent::getBuildDefaults($entity, $view_mode); Chris@0: Chris@0: /** @var \Drupal\comment\CommentInterface $entity */ Chris@0: // Store a threading field setting to use later in self::buildComponents(). Chris@0: $build['#comment_threaded'] = $entity->getCommentedEntity() Chris@0: ->getFieldDefinition($entity->getFieldName()) Chris@0: ->getSetting('default_mode') === CommentManagerInterface::COMMENT_MODE_THREADED; Chris@0: // If threading is enabled, don't render cache individual comments, but do Chris@0: // keep the cacheability metadata, so it can bubble up. Chris@0: if ($build['#comment_threaded']) { Chris@0: unset($build['#cache']['keys']); Chris@0: } Chris@0: Chris@0: return $build; Chris@0: } Chris@0: Chris@0: /** Chris@0: * {@inheritdoc} Chris@0: * Chris@0: * In addition to modifying the content key on entities, this implementation Chris@0: * will also set the comment entity key which all comments carry. Chris@0: * Chris@0: * @throws \InvalidArgumentException Chris@0: * Thrown when a comment is attached to an entity that no longer exists. Chris@0: */ Chris@0: public function buildComponents(array &$build, array $entities, array $displays, $view_mode) { Chris@0: /** @var \Drupal\comment\CommentInterface[] $entities */ Chris@0: if (empty($entities)) { Chris@0: return; Chris@0: } Chris@0: Chris@0: // Pre-load associated users into cache to leverage multiple loading. Chris@0: $uids = []; Chris@0: foreach ($entities as $entity) { Chris@0: $uids[] = $entity->getOwnerId(); Chris@0: } Chris@18: $this->entityTypeManager->getStorage('user')->loadMultiple(array_unique($uids)); Chris@0: Chris@0: parent::buildComponents($build, $entities, $displays, $view_mode); Chris@0: Chris@0: // A counter to track the indentation level. Chris@0: $current_indent = 0; Chris@16: $attach_history = $this->moduleHandler->moduleExists('history') && $this->currentUser->isAuthenticated(); Chris@0: Chris@0: foreach ($entities as $id => $entity) { Chris@0: if ($build[$id]['#comment_threaded']) { Chris@0: $comment_indent = count(explode('.', $entity->getThread())) - 1; Chris@0: if ($comment_indent > $current_indent) { Chris@0: // Set 1 to indent this comment from the previous one (its parent). Chris@0: // Set only one extra level of indenting even if the difference in Chris@0: // depth is higher. Chris@0: $build[$id]['#comment_indent'] = 1; Chris@0: $current_indent++; Chris@0: } Chris@0: else { Chris@0: // Set zero if this comment is on the same level as the previous one Chris@0: // or negative value to point an amount indents to close. Chris@0: $build[$id]['#comment_indent'] = $comment_indent - $current_indent; Chris@0: $current_indent = $comment_indent; Chris@0: } Chris@0: } Chris@0: Chris@0: // Commented entities already loaded after self::getBuildDefaults(). Chris@0: $commented_entity = $entity->getCommentedEntity(); Chris@0: Chris@0: $build[$id]['#entity'] = $entity; Chris@0: $build[$id]['#theme'] = 'comment__' . $entity->getFieldName() . '__' . $commented_entity->bundle(); Chris@0: Chris@0: $display = $displays[$entity->bundle()]; Chris@0: if ($display->getComponent('links')) { Chris@0: $build[$id]['links'] = [ Chris@0: '#lazy_builder' => [ Chris@0: 'comment.lazy_builders:renderLinks', Chris@0: [ Chris@0: $entity->id(), Chris@0: $view_mode, Chris@0: $entity->language()->getId(), Chris@0: !empty($entity->in_preview), Chris@0: ], Chris@0: ], Chris@0: '#create_placeholder' => TRUE, Chris@0: ]; Chris@0: } Chris@0: Chris@0: if (!isset($build[$id]['#attached'])) { Chris@0: $build[$id]['#attached'] = []; Chris@0: } Chris@0: $build[$id]['#attached']['library'][] = 'comment/drupal.comment-by-viewer'; Chris@16: if ($attach_history && $commented_entity->getEntityTypeId() === 'node') { Chris@0: $build[$id]['#attached']['library'][] = 'comment/drupal.comment-new-indicator'; Chris@0: Chris@0: // Embed the metadata for the comment "new" indicators on this node. Chris@0: $build[$id]['history'] = [ Chris@0: '#lazy_builder' => ['history_attach_timestamp', [$commented_entity->id()]], Chris@0: '#create_placeholder' => TRUE, Chris@0: ]; Chris@0: } Chris@0: } Chris@0: if ($build[$id]['#comment_threaded']) { Chris@0: // The final comment must close up some hanging divs. Chris@0: $build[$id]['#comment_indent_final'] = $current_indent; Chris@0: } Chris@0: } Chris@0: Chris@0: /** Chris@0: * {@inheritdoc} Chris@0: */ Chris@0: protected function alterBuild(array &$build, EntityInterface $comment, EntityViewDisplayInterface $display, $view_mode) { Chris@0: parent::alterBuild($build, $comment, $display, $view_mode); Chris@0: if (empty($comment->in_preview)) { Chris@0: $prefix = ''; Chris@0: Chris@0: // Add indentation div or close open divs as needed. Chris@0: if ($build['#comment_threaded']) { Chris@0: $prefix .= $build['#comment_indent'] <= 0 ? str_repeat('', abs($build['#comment_indent'])) : "\n" . '