comparison core/modules/comment/src/CommentLinkBuilder.php @ 0:c75dbcec494b

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