annotate core/modules/comment/src/CommentViewBuilder.php @ 19:fa3358dc1485 tip

Add ndrum files
author Chris Cannam
date Wed, 28 Aug 2019 13:14:47 +0100
parents af1871eacc83
children
rev   line source
Chris@0 1 <?php
Chris@0 2
Chris@0 3 namespace Drupal\comment;
Chris@0 4
Chris@0 5 use Drupal\Core\Entity\Display\EntityViewDisplayInterface;
Chris@18 6 use Drupal\Core\Entity\EntityDisplayRepositoryInterface;
Chris@0 7 use Drupal\Core\Entity\EntityInterface;
Chris@18 8 use Drupal\Core\Entity\EntityRepositoryInterface;
Chris@0 9 use Drupal\Core\Entity\EntityTypeInterface;
Chris@18 10 use Drupal\Core\Entity\EntityTypeManagerInterface;
Chris@0 11 use Drupal\Core\Entity\EntityViewBuilder;
Chris@0 12 use Drupal\Core\Language\LanguageManagerInterface;
Chris@0 13 use Drupal\Core\Session\AccountInterface;
Chris@18 14 use Drupal\Core\Theme\Registry;
Chris@0 15 use Symfony\Component\DependencyInjection\ContainerInterface;
Chris@0 16
Chris@0 17 /**
Chris@0 18 * View builder handler for comments.
Chris@0 19 */
Chris@0 20 class CommentViewBuilder extends EntityViewBuilder {
Chris@0 21
Chris@0 22 /**
Chris@0 23 * The current user.
Chris@0 24 *
Chris@0 25 * @var \Drupal\Core\Session\AccountInterface
Chris@0 26 */
Chris@0 27 protected $currentUser;
Chris@0 28
Chris@0 29 /**
Chris@18 30 * The entity type manager.
Chris@18 31 *
Chris@18 32 * @var \Drupal\Core\Entity\EntityTypeManagerInterface
Chris@18 33 */
Chris@18 34 protected $entityTypeManager;
Chris@18 35
Chris@18 36 /**
Chris@0 37 * Constructs a new CommentViewBuilder.
Chris@0 38 *
Chris@0 39 * @param \Drupal\Core\Entity\EntityTypeInterface $entity_type
Chris@0 40 * The entity type definition.
Chris@18 41 * @param \Drupal\Core\Entity\EntityRepositoryInterface $entity_repository
Chris@18 42 * The entity repository service.
Chris@0 43 * @param \Drupal\Core\Language\LanguageManagerInterface $language_manager
Chris@0 44 * The language manager.
Chris@0 45 * @param \Drupal\Core\Session\AccountInterface $current_user
Chris@0 46 * The current user.
Chris@18 47 * @param \Drupal\Core\Theme\Registry $theme_registry
Chris@18 48 * The theme registry.
Chris@18 49 * @param \Drupal\Core\Entity\EntityDisplayRepositoryInterface $entity_display_repository
Chris@18 50 * The entity display repository.
Chris@18 51 * @param \Drupal\Core\Entity\EntityTypeManagerInterface $entity_type_manager
Chris@18 52 * The entity type manager.
Chris@0 53 */
Chris@18 54 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) {
Chris@18 55 parent::__construct($entity_type, $entity_repository, $language_manager, $theme_registry, $entity_display_repository);
Chris@0 56 $this->currentUser = $current_user;
Chris@18 57 $this->entityTypeManager = $entity_type_manager;
Chris@0 58 }
Chris@0 59
Chris@0 60 /**
Chris@0 61 * {@inheritdoc}
Chris@0 62 */
Chris@0 63 public static function createInstance(ContainerInterface $container, EntityTypeInterface $entity_type) {
Chris@0 64 return new static(
Chris@0 65 $entity_type,
Chris@18 66 $container->get('entity.repository'),
Chris@0 67 $container->get('language_manager'),
Chris@18 68 $container->get('current_user'),
Chris@18 69 $container->get('theme.registry'),
Chris@18 70 $container->get('entity_display.repository'),
Chris@18 71 $container->get('entity_type.manager')
Chris@0 72 );
Chris@0 73 }
Chris@0 74
Chris@0 75 /**
Chris@0 76 * {@inheritdoc}
Chris@0 77 */
Chris@0 78 protected function getBuildDefaults(EntityInterface $entity, $view_mode) {
Chris@0 79 $build = parent::getBuildDefaults($entity, $view_mode);
Chris@0 80
Chris@0 81 /** @var \Drupal\comment\CommentInterface $entity */
Chris@0 82 // Store a threading field setting to use later in self::buildComponents().
Chris@0 83 $build['#comment_threaded'] = $entity->getCommentedEntity()
Chris@0 84 ->getFieldDefinition($entity->getFieldName())
Chris@0 85 ->getSetting('default_mode') === CommentManagerInterface::COMMENT_MODE_THREADED;
Chris@0 86 // If threading is enabled, don't render cache individual comments, but do
Chris@0 87 // keep the cacheability metadata, so it can bubble up.
Chris@0 88 if ($build['#comment_threaded']) {
Chris@0 89 unset($build['#cache']['keys']);
Chris@0 90 }
Chris@0 91
Chris@0 92 return $build;
Chris@0 93 }
Chris@0 94
Chris@0 95 /**
Chris@0 96 * {@inheritdoc}
Chris@0 97 *
Chris@0 98 * In addition to modifying the content key on entities, this implementation
Chris@0 99 * will also set the comment entity key which all comments carry.
Chris@0 100 *
Chris@0 101 * @throws \InvalidArgumentException
Chris@0 102 * Thrown when a comment is attached to an entity that no longer exists.
Chris@0 103 */
Chris@0 104 public function buildComponents(array &$build, array $entities, array $displays, $view_mode) {
Chris@0 105 /** @var \Drupal\comment\CommentInterface[] $entities */
Chris@0 106 if (empty($entities)) {
Chris@0 107 return;
Chris@0 108 }
Chris@0 109
Chris@0 110 // Pre-load associated users into cache to leverage multiple loading.
Chris@0 111 $uids = [];
Chris@0 112 foreach ($entities as $entity) {
Chris@0 113 $uids[] = $entity->getOwnerId();
Chris@0 114 }
Chris@18 115 $this->entityTypeManager->getStorage('user')->loadMultiple(array_unique($uids));
Chris@0 116
Chris@0 117 parent::buildComponents($build, $entities, $displays, $view_mode);
Chris@0 118
Chris@0 119 // A counter to track the indentation level.
Chris@0 120 $current_indent = 0;
Chris@16 121 $attach_history = $this->moduleHandler->moduleExists('history') && $this->currentUser->isAuthenticated();
Chris@0 122
Chris@0 123 foreach ($entities as $id => $entity) {
Chris@0 124 if ($build[$id]['#comment_threaded']) {
Chris@0 125 $comment_indent = count(explode('.', $entity->getThread())) - 1;
Chris@0 126 if ($comment_indent > $current_indent) {
Chris@0 127 // Set 1 to indent this comment from the previous one (its parent).
Chris@0 128 // Set only one extra level of indenting even if the difference in
Chris@0 129 // depth is higher.
Chris@0 130 $build[$id]['#comment_indent'] = 1;
Chris@0 131 $current_indent++;
Chris@0 132 }
Chris@0 133 else {
Chris@0 134 // Set zero if this comment is on the same level as the previous one
Chris@0 135 // or negative value to point an amount indents to close.
Chris@0 136 $build[$id]['#comment_indent'] = $comment_indent - $current_indent;
Chris@0 137 $current_indent = $comment_indent;
Chris@0 138 }
Chris@0 139 }
Chris@0 140
Chris@0 141 // Commented entities already loaded after self::getBuildDefaults().
Chris@0 142 $commented_entity = $entity->getCommentedEntity();
Chris@0 143
Chris@0 144 $build[$id]['#entity'] = $entity;
Chris@0 145 $build[$id]['#theme'] = 'comment__' . $entity->getFieldName() . '__' . $commented_entity->bundle();
Chris@0 146
Chris@0 147 $display = $displays[$entity->bundle()];
Chris@0 148 if ($display->getComponent('links')) {
Chris@0 149 $build[$id]['links'] = [
Chris@0 150 '#lazy_builder' => [
Chris@0 151 'comment.lazy_builders:renderLinks',
Chris@0 152 [
Chris@0 153 $entity->id(),
Chris@0 154 $view_mode,
Chris@0 155 $entity->language()->getId(),
Chris@0 156 !empty($entity->in_preview),
Chris@0 157 ],
Chris@0 158 ],
Chris@0 159 '#create_placeholder' => TRUE,
Chris@0 160 ];
Chris@0 161 }
Chris@0 162
Chris@0 163 if (!isset($build[$id]['#attached'])) {
Chris@0 164 $build[$id]['#attached'] = [];
Chris@0 165 }
Chris@0 166 $build[$id]['#attached']['library'][] = 'comment/drupal.comment-by-viewer';
Chris@16 167 if ($attach_history && $commented_entity->getEntityTypeId() === 'node') {
Chris@0 168 $build[$id]['#attached']['library'][] = 'comment/drupal.comment-new-indicator';
Chris@0 169
Chris@0 170 // Embed the metadata for the comment "new" indicators on this node.
Chris@0 171 $build[$id]['history'] = [
Chris@0 172 '#lazy_builder' => ['history_attach_timestamp', [$commented_entity->id()]],
Chris@0 173 '#create_placeholder' => TRUE,
Chris@0 174 ];
Chris@0 175 }
Chris@0 176 }
Chris@0 177 if ($build[$id]['#comment_threaded']) {
Chris@0 178 // The final comment must close up some hanging divs.
Chris@0 179 $build[$id]['#comment_indent_final'] = $current_indent;
Chris@0 180 }
Chris@0 181 }
Chris@0 182
Chris@0 183 /**
Chris@0 184 * {@inheritdoc}
Chris@0 185 */
Chris@0 186 protected function alterBuild(array &$build, EntityInterface $comment, EntityViewDisplayInterface $display, $view_mode) {
Chris@0 187 parent::alterBuild($build, $comment, $display, $view_mode);
Chris@0 188 if (empty($comment->in_preview)) {
Chris@0 189 $prefix = '';
Chris@0 190
Chris@0 191 // Add indentation div or close open divs as needed.
Chris@0 192 if ($build['#comment_threaded']) {
Chris@0 193 $prefix .= $build['#comment_indent'] <= 0 ? str_repeat('</div>', abs($build['#comment_indent'])) : "\n" . '<div class="indented">';
Chris@0 194 }
Chris@0 195
Chris@0 196 $build['#prefix'] = $prefix;
Chris@0 197
Chris@0 198 // Close all open divs.
Chris@0 199 if (!empty($build['#comment_indent_final'])) {
Chris@0 200 $build['#suffix'] = str_repeat('</div>', $build['#comment_indent_final']);
Chris@0 201 }
Chris@0 202 }
Chris@0 203 }
Chris@0 204
Chris@0 205 }