comparison core/modules/comment/src/CommentViewBuilder.php @ 18:af1871eacc83

Update to Drupal core 8.7.1
author Chris Cannam
date Thu, 09 May 2019 15:33:08 +0100
parents c2387f117808
children
comparison
equal deleted inserted replaced
17:129ea1e6d783 18:af1871eacc83
1 <?php 1 <?php
2 2
3 namespace Drupal\comment; 3 namespace Drupal\comment;
4 4
5 use Drupal\Core\Entity\Display\EntityViewDisplayInterface; 5 use Drupal\Core\Entity\Display\EntityViewDisplayInterface;
6 use Drupal\Core\Entity\EntityDisplayRepositoryInterface;
6 use Drupal\Core\Entity\EntityInterface; 7 use Drupal\Core\Entity\EntityInterface;
7 use Drupal\Core\Entity\EntityManagerInterface; 8 use Drupal\Core\Entity\EntityRepositoryInterface;
8 use Drupal\Core\Entity\EntityTypeInterface; 9 use Drupal\Core\Entity\EntityTypeInterface;
10 use Drupal\Core\Entity\EntityTypeManagerInterface;
9 use Drupal\Core\Entity\EntityViewBuilder; 11 use Drupal\Core\Entity\EntityViewBuilder;
10 use Drupal\Core\Language\LanguageManagerInterface; 12 use Drupal\Core\Language\LanguageManagerInterface;
11 use Drupal\Core\Session\AccountInterface; 13 use Drupal\Core\Session\AccountInterface;
14 use Drupal\Core\Theme\Registry;
12 use Symfony\Component\DependencyInjection\ContainerInterface; 15 use Symfony\Component\DependencyInjection\ContainerInterface;
13 16
14 /** 17 /**
15 * View builder handler for comments. 18 * View builder handler for comments.
16 */ 19 */
22 * @var \Drupal\Core\Session\AccountInterface 25 * @var \Drupal\Core\Session\AccountInterface
23 */ 26 */
24 protected $currentUser; 27 protected $currentUser;
25 28
26 /** 29 /**
30 * The entity type manager.
31 *
32 * @var \Drupal\Core\Entity\EntityTypeManagerInterface
33 */
34 protected $entityTypeManager;
35
36 /**
27 * Constructs a new CommentViewBuilder. 37 * Constructs a new CommentViewBuilder.
28 * 38 *
29 * @param \Drupal\Core\Entity\EntityTypeInterface $entity_type 39 * @param \Drupal\Core\Entity\EntityTypeInterface $entity_type
30 * The entity type definition. 40 * The entity type definition.
31 * @param \Drupal\Core\Entity\EntityManagerInterface $entity_manager 41 * @param \Drupal\Core\Entity\EntityRepositoryInterface $entity_repository
32 * The entity manager service. 42 * The entity repository service.
33 * @param \Drupal\Core\Language\LanguageManagerInterface $language_manager 43 * @param \Drupal\Core\Language\LanguageManagerInterface $language_manager
34 * The language manager. 44 * The language manager.
35 * @param \Drupal\Core\Session\AccountInterface $current_user 45 * @param \Drupal\Core\Session\AccountInterface $current_user
36 * The current user. 46 * The current user.
37 */ 47 * @param \Drupal\Core\Theme\Registry $theme_registry
38 public function __construct(EntityTypeInterface $entity_type, EntityManagerInterface $entity_manager, LanguageManagerInterface $language_manager, AccountInterface $current_user) { 48 * The theme registry.
39 parent::__construct($entity_type, $entity_manager, $language_manager); 49 * @param \Drupal\Core\Entity\EntityDisplayRepositoryInterface $entity_display_repository
50 * The entity display repository.
51 * @param \Drupal\Core\Entity\EntityTypeManagerInterface $entity_type_manager
52 * The entity type manager.
53 */
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) {
55 parent::__construct($entity_type, $entity_repository, $language_manager, $theme_registry, $entity_display_repository);
40 $this->currentUser = $current_user; 56 $this->currentUser = $current_user;
57 $this->entityTypeManager = $entity_type_manager;
41 } 58 }
42 59
43 /** 60 /**
44 * {@inheritdoc} 61 * {@inheritdoc}
45 */ 62 */
46 public static function createInstance(ContainerInterface $container, EntityTypeInterface $entity_type) { 63 public static function createInstance(ContainerInterface $container, EntityTypeInterface $entity_type) {
47 return new static( 64 return new static(
48 $entity_type, 65 $entity_type,
49 $container->get('entity.manager'), 66 $container->get('entity.repository'),
50 $container->get('language_manager'), 67 $container->get('language_manager'),
51 $container->get('current_user') 68 $container->get('current_user'),
69 $container->get('theme.registry'),
70 $container->get('entity_display.repository'),
71 $container->get('entity_type.manager')
52 ); 72 );
53 } 73 }
54 74
55 /** 75 /**
56 * {@inheritdoc} 76 * {@inheritdoc}
90 // Pre-load associated users into cache to leverage multiple loading. 110 // Pre-load associated users into cache to leverage multiple loading.
91 $uids = []; 111 $uids = [];
92 foreach ($entities as $entity) { 112 foreach ($entities as $entity) {
93 $uids[] = $entity->getOwnerId(); 113 $uids[] = $entity->getOwnerId();
94 } 114 }
95 $this->entityManager->getStorage('user')->loadMultiple(array_unique($uids)); 115 $this->entityTypeManager->getStorage('user')->loadMultiple(array_unique($uids));
96 116
97 parent::buildComponents($build, $entities, $displays, $view_mode); 117 parent::buildComponents($build, $entities, $displays, $view_mode);
98 118
99 // A counter to track the indentation level. 119 // A counter to track the indentation level.
100 $current_indent = 0; 120 $current_indent = 0;
171 // Add indentation div or close open divs as needed. 191 // Add indentation div or close open divs as needed.
172 if ($build['#comment_threaded']) { 192 if ($build['#comment_threaded']) {
173 $prefix .= $build['#comment_indent'] <= 0 ? str_repeat('</div>', abs($build['#comment_indent'])) : "\n" . '<div class="indented">'; 193 $prefix .= $build['#comment_indent'] <= 0 ? str_repeat('</div>', abs($build['#comment_indent'])) : "\n" . '<div class="indented">';
174 } 194 }
175 195
176 // Add anchor for each comment.
177 $prefix .= "<a id=\"comment-{$comment->id()}\"></a>\n";
178 $build['#prefix'] = $prefix; 196 $build['#prefix'] = $prefix;
179 197
180 // Close all open divs. 198 // Close all open divs.
181 if (!empty($build['#comment_indent_final'])) { 199 if (!empty($build['#comment_indent_final'])) {
182 $build['#suffix'] = str_repeat('</div>', $build['#comment_indent_final']); 200 $build['#suffix'] = str_repeat('</div>', $build['#comment_indent_final']);