3 namespace Drupal\comment;
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;
16 * Defines a class for building markup for comment links on a commented entity.
18 * Comment links include 'log in to post new comment', 'add new comment' etc.
20 class CommentLinkBuilder implements CommentLinkBuilderInterface {
22 use StringTranslationTrait;
27 * @var \Drupal\Core\Session\AccountInterface
29 protected $currentUser;
32 * Comment manager service.
34 * @var \Drupal\comment\CommentManagerInterface
36 protected $commentManager;
39 * Module handler service.
41 * @var \Drupal\Core\Extension\ModuleHandlerInterface
43 protected $moduleHandler;
46 * The entity manager service.
48 * @var \Drupal\Core\Entity\EntityManagerInterface
50 protected $entityManager;
53 * Constructs a new CommentLinkBuilder object.
55 * @param \Drupal\Core\Session\AccountInterface $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.
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;
77 public function buildCommentedEntityLinks(FieldableEntityInterface $entity, array &$context) {
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:
83 // - constructing a search result excerpt.
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)) {
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->toUrl(),
112 if ($this->moduleHandler->moduleExists('history')) {
113 $links['comment-new-comments'] = [
115 'url' => Url::fromRoute('<current>'),
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,
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',
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,
144 $links['comment-add'] += ['url' => $entity->toUrl()];
147 elseif ($this->currentUser->isAnonymous()) {
148 $links['comment-forbidden'] = [
149 'title' => $this->commentManager->forbiddenMessage($entity, $field_name),
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
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',
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,
177 $links['comment-add']['url'] = $entity->toUrl();
181 elseif ($this->currentUser->isAnonymous()) {
182 $links['comment-forbidden'] = [
183 'title' => $this->commentManager->forbiddenMessage($entity, $field_name),
190 if (!empty($links)) {
191 $entity_links['comment__' . $field_name] = [
192 '#theme' => 'links__entity__comment__' . $field_name,
194 '#attributes' => ['class' => ['links', 'inline']],
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
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;
209 'new_comment_count' => (int) $new_comments,
210 'first_new_comment_link' => $entity->url('canonical', [
215 $parents = ['comment', 'newCommentsLinks', $entity->getEntityTypeId(), $field_name, $entity->id()];
216 NestedArray::setValue($entity_links['comment__' . $field_name]['#attached']['drupalSettings'], $parents, $value);
221 return $entity_links;