annotate core/modules/comment/src/CommentViewBuilder.php @ 0:4c8ae668cc8c

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