Chris@0
|
1 <?php
|
Chris@0
|
2
|
Chris@0
|
3 namespace Drupal\comment;
|
Chris@0
|
4
|
Chris@0
|
5 use Drupal\comment\Plugin\Field\FieldType\CommentItemInterface;
|
Chris@0
|
6 use Drupal\Component\Utility\NestedArray;
|
Chris@0
|
7 use Drupal\Core\Entity\EntityManagerInterface;
|
Chris@0
|
8 use Drupal\Core\Entity\FieldableEntityInterface;
|
Chris@0
|
9 use Drupal\Core\Extension\ModuleHandlerInterface;
|
Chris@0
|
10 use Drupal\Core\Session\AccountInterface;
|
Chris@0
|
11 use Drupal\Core\StringTranslation\StringTranslationTrait;
|
Chris@0
|
12 use Drupal\Core\StringTranslation\TranslationInterface;
|
Chris@0
|
13 use Drupal\Core\Url;
|
Chris@0
|
14
|
Chris@0
|
15 /**
|
Chris@0
|
16 * Defines a class for building markup for comment links on a commented entity.
|
Chris@0
|
17 *
|
Chris@0
|
18 * Comment links include 'log in to post new comment', 'add new comment' etc.
|
Chris@0
|
19 */
|
Chris@0
|
20 class CommentLinkBuilder implements CommentLinkBuilderInterface {
|
Chris@0
|
21
|
Chris@0
|
22 use StringTranslationTrait;
|
Chris@0
|
23
|
Chris@0
|
24 /**
|
Chris@0
|
25 * Current user.
|
Chris@0
|
26 *
|
Chris@0
|
27 * @var \Drupal\Core\Session\AccountInterface
|
Chris@0
|
28 */
|
Chris@0
|
29 protected $currentUser;
|
Chris@0
|
30
|
Chris@0
|
31 /**
|
Chris@0
|
32 * Comment manager service.
|
Chris@0
|
33 *
|
Chris@0
|
34 * @var \Drupal\comment\CommentManagerInterface
|
Chris@0
|
35 */
|
Chris@0
|
36 protected $commentManager;
|
Chris@0
|
37
|
Chris@0
|
38 /**
|
Chris@0
|
39 * Module handler service.
|
Chris@0
|
40 *
|
Chris@0
|
41 * @var \Drupal\Core\Extension\ModuleHandlerInterface
|
Chris@0
|
42 */
|
Chris@0
|
43 protected $moduleHandler;
|
Chris@0
|
44
|
Chris@0
|
45 /**
|
Chris@0
|
46 * The entity manager service.
|
Chris@0
|
47 *
|
Chris@0
|
48 * @var \Drupal\Core\Entity\EntityManagerInterface
|
Chris@0
|
49 */
|
Chris@0
|
50 protected $entityManager;
|
Chris@0
|
51
|
Chris@0
|
52 /**
|
Chris@0
|
53 * Constructs a new CommentLinkBuilder object.
|
Chris@0
|
54 *
|
Chris@0
|
55 * @param \Drupal\Core\Session\AccountInterface $current_user
|
Chris@0
|
56 * Current user.
|
Chris@0
|
57 * @param \Drupal\comment\CommentManagerInterface $comment_manager
|
Chris@0
|
58 * Comment manager service.
|
Chris@0
|
59 * @param \Drupal\Core\Extension\ModuleHandlerInterface $module_handler
|
Chris@0
|
60 * Module handler service.
|
Chris@0
|
61 * @param \Drupal\Core\StringTranslation\TranslationInterface $string_translation
|
Chris@0
|
62 * String translation service.
|
Chris@0
|
63 * @param \Drupal\Core\Entity\EntityManagerInterface $entity_manager
|
Chris@0
|
64 * The entity manager service.
|
Chris@0
|
65 */
|
Chris@0
|
66 public function __construct(AccountInterface $current_user, CommentManagerInterface $comment_manager, ModuleHandlerInterface $module_handler, TranslationInterface $string_translation, EntityManagerInterface $entity_manager) {
|
Chris@0
|
67 $this->currentUser = $current_user;
|
Chris@0
|
68 $this->commentManager = $comment_manager;
|
Chris@0
|
69 $this->moduleHandler = $module_handler;
|
Chris@0
|
70 $this->stringTranslation = $string_translation;
|
Chris@0
|
71 $this->entityManager = $entity_manager;
|
Chris@0
|
72 }
|
Chris@0
|
73
|
Chris@0
|
74 /**
|
Chris@0
|
75 * {@inheritdoc}
|
Chris@0
|
76 */
|
Chris@0
|
77 public function buildCommentedEntityLinks(FieldableEntityInterface $entity, array &$context) {
|
Chris@0
|
78 $entity_links = [];
|
Chris@0
|
79 $view_mode = $context['view_mode'];
|
Chris@0
|
80 if ($view_mode == 'search_index' || $view_mode == 'search_result' || $view_mode == 'print' || $view_mode == 'rss') {
|
Chris@0
|
81 // Do not add any links if the entity is displayed for:
|
Chris@0
|
82 // - search indexing.
|
Chris@0
|
83 // - constructing a search result excerpt.
|
Chris@0
|
84 // - print.
|
Chris@0
|
85 // - rss.
|
Chris@0
|
86 return [];
|
Chris@0
|
87 }
|
Chris@0
|
88
|
Chris@0
|
89 $fields = $this->commentManager->getFields($entity->getEntityTypeId());
|
Chris@0
|
90 foreach ($fields as $field_name => $detail) {
|
Chris@0
|
91 // Skip fields that the entity does not have.
|
Chris@0
|
92 if (!$entity->hasField($field_name)) {
|
Chris@0
|
93 continue;
|
Chris@0
|
94 }
|
Chris@0
|
95 $links = [];
|
Chris@0
|
96 $commenting_status = $entity->get($field_name)->status;
|
Chris@0
|
97 if ($commenting_status != CommentItemInterface::HIDDEN) {
|
Chris@0
|
98 // Entity has commenting status open or closed.
|
Chris@0
|
99 $field_definition = $entity->getFieldDefinition($field_name);
|
Chris@0
|
100 if ($view_mode == 'teaser') {
|
Chris@0
|
101 // Teaser view: display the number of comments that have been posted,
|
Chris@0
|
102 // or a link to add new comments if the user has permission, the
|
Chris@0
|
103 // entity is open to new comments, and there currently are none.
|
Chris@0
|
104 if ($this->currentUser->hasPermission('access comments')) {
|
Chris@0
|
105 if (!empty($entity->get($field_name)->comment_count)) {
|
Chris@0
|
106 $links['comment-comments'] = [
|
Chris@0
|
107 'title' => $this->formatPlural($entity->get($field_name)->comment_count, '1 comment', '@count comments'),
|
Chris@0
|
108 'attributes' => ['title' => $this->t('Jump to the first comment.')],
|
Chris@0
|
109 'fragment' => 'comments',
|
Chris@0
|
110 'url' => $entity->urlInfo(),
|
Chris@0
|
111 ];
|
Chris@0
|
112 if ($this->moduleHandler->moduleExists('history')) {
|
Chris@0
|
113 $links['comment-new-comments'] = [
|
Chris@0
|
114 'title' => '',
|
Chris@0
|
115 'url' => Url::fromRoute('<current>'),
|
Chris@0
|
116 'attributes' => [
|
Chris@0
|
117 'class' => 'hidden',
|
Chris@0
|
118 'title' => $this->t('Jump to the first new comment.'),
|
Chris@0
|
119 'data-history-node-last-comment-timestamp' => $entity->get($field_name)->last_comment_timestamp,
|
Chris@0
|
120 'data-history-node-field-name' => $field_name,
|
Chris@0
|
121 ],
|
Chris@0
|
122 ];
|
Chris@0
|
123 }
|
Chris@0
|
124 }
|
Chris@0
|
125 }
|
Chris@0
|
126 // Provide a link to new comment form.
|
Chris@0
|
127 if ($commenting_status == CommentItemInterface::OPEN) {
|
Chris@0
|
128 $comment_form_location = $field_definition->getSetting('form_location');
|
Chris@0
|
129 if ($this->currentUser->hasPermission('post comments')) {
|
Chris@0
|
130 $links['comment-add'] = [
|
Chris@0
|
131 'title' => $this->t('Add new comment'),
|
Chris@0
|
132 'language' => $entity->language(),
|
Chris@0
|
133 'attributes' => ['title' => $this->t('Share your thoughts and opinions.')],
|
Chris@0
|
134 'fragment' => 'comment-form',
|
Chris@0
|
135 ];
|
Chris@0
|
136 if ($comment_form_location == CommentItemInterface::FORM_SEPARATE_PAGE) {
|
Chris@0
|
137 $links['comment-add']['url'] = Url::fromRoute('comment.reply', [
|
Chris@0
|
138 'entity_type' => $entity->getEntityTypeId(),
|
Chris@0
|
139 'entity' => $entity->id(),
|
Chris@0
|
140 'field_name' => $field_name,
|
Chris@0
|
141 ]);
|
Chris@0
|
142 }
|
Chris@0
|
143 else {
|
Chris@0
|
144 $links['comment-add'] += ['url' => $entity->urlInfo()];
|
Chris@0
|
145 }
|
Chris@0
|
146 }
|
Chris@0
|
147 elseif ($this->currentUser->isAnonymous()) {
|
Chris@0
|
148 $links['comment-forbidden'] = [
|
Chris@0
|
149 'title' => $this->commentManager->forbiddenMessage($entity, $field_name),
|
Chris@0
|
150 ];
|
Chris@0
|
151 }
|
Chris@0
|
152 }
|
Chris@0
|
153 }
|
Chris@0
|
154 else {
|
Chris@0
|
155 // Entity in other view modes: add a "post comment" link if the user
|
Chris@0
|
156 // is allowed to post comments and if this entity is allowing new
|
Chris@0
|
157 // comments.
|
Chris@0
|
158 if ($commenting_status == CommentItemInterface::OPEN) {
|
Chris@0
|
159 $comment_form_location = $field_definition->getSetting('form_location');
|
Chris@0
|
160 if ($this->currentUser->hasPermission('post comments')) {
|
Chris@0
|
161 // Show the "post comment" link if the form is on another page, or
|
Chris@0
|
162 // if there are existing comments that the link will skip past.
|
Chris@0
|
163 if ($comment_form_location == CommentItemInterface::FORM_SEPARATE_PAGE || (!empty($entity->get($field_name)->comment_count) && $this->currentUser->hasPermission('access comments'))) {
|
Chris@0
|
164 $links['comment-add'] = [
|
Chris@0
|
165 'title' => $this->t('Add new comment'),
|
Chris@0
|
166 'attributes' => ['title' => $this->t('Share your thoughts and opinions.')],
|
Chris@0
|
167 'fragment' => 'comment-form',
|
Chris@0
|
168 ];
|
Chris@0
|
169 if ($comment_form_location == CommentItemInterface::FORM_SEPARATE_PAGE) {
|
Chris@0
|
170 $links['comment-add']['url'] = Url::fromRoute('comment.reply', [
|
Chris@0
|
171 'entity_type' => $entity->getEntityTypeId(),
|
Chris@0
|
172 'entity' => $entity->id(),
|
Chris@0
|
173 'field_name' => $field_name,
|
Chris@0
|
174 ]);
|
Chris@0
|
175 }
|
Chris@0
|
176 else {
|
Chris@0
|
177 $links['comment-add']['url'] = $entity->urlInfo();
|
Chris@0
|
178 }
|
Chris@0
|
179 }
|
Chris@0
|
180 }
|
Chris@0
|
181 elseif ($this->currentUser->isAnonymous()) {
|
Chris@0
|
182 $links['comment-forbidden'] = [
|
Chris@0
|
183 'title' => $this->commentManager->forbiddenMessage($entity, $field_name),
|
Chris@0
|
184 ];
|
Chris@0
|
185 }
|
Chris@0
|
186 }
|
Chris@0
|
187 }
|
Chris@0
|
188 }
|
Chris@0
|
189
|
Chris@0
|
190 if (!empty($links)) {
|
Chris@0
|
191 $entity_links['comment__' . $field_name] = [
|
Chris@0
|
192 '#theme' => 'links__entity__comment__' . $field_name,
|
Chris@0
|
193 '#links' => $links,
|
Chris@0
|
194 '#attributes' => ['class' => ['links', 'inline']],
|
Chris@0
|
195 ];
|
Chris@0
|
196 if ($view_mode == 'teaser' && $this->moduleHandler->moduleExists('history') && $this->currentUser->isAuthenticated()) {
|
Chris@0
|
197 $entity_links['comment__' . $field_name]['#cache']['contexts'][] = 'user';
|
Chris@0
|
198 $entity_links['comment__' . $field_name]['#attached']['library'][] = 'comment/drupal.node-new-comments-link';
|
Chris@0
|
199 // Embed the metadata for the "X new comments" link (if any) on this
|
Chris@0
|
200 // entity.
|
Chris@0
|
201 $entity_links['comment__' . $field_name]['#attached']['drupalSettings']['history']['lastReadTimestamps'][$entity->id()] = (int) history_read($entity->id());
|
Chris@0
|
202 $new_comments = $this->commentManager->getCountNewComments($entity);
|
Chris@0
|
203 if ($new_comments > 0) {
|
Chris@0
|
204 $page_number = $this->entityManager
|
Chris@0
|
205 ->getStorage('comment')
|
Chris@0
|
206 ->getNewCommentPageNumber($entity->{$field_name}->comment_count, $new_comments, $entity, $field_name);
|
Chris@0
|
207 $query = $page_number ? ['page' => $page_number] : NULL;
|
Chris@0
|
208 $value = [
|
Chris@0
|
209 'new_comment_count' => (int) $new_comments,
|
Chris@0
|
210 'first_new_comment_link' => $entity->url('canonical', [
|
Chris@0
|
211 'query' => $query,
|
Chris@0
|
212 'fragment' => 'new',
|
Chris@0
|
213 ]),
|
Chris@0
|
214 ];
|
Chris@0
|
215 $parents = ['comment', 'newCommentsLinks', $entity->getEntityTypeId(), $field_name, $entity->id()];
|
Chris@0
|
216 NestedArray::setValue($entity_links['comment__' . $field_name]['#attached']['drupalSettings'], $parents, $value);
|
Chris@0
|
217 }
|
Chris@0
|
218 }
|
Chris@0
|
219 }
|
Chris@0
|
220 }
|
Chris@0
|
221 return $entity_links;
|
Chris@0
|
222 }
|
Chris@0
|
223
|
Chris@0
|
224 }
|