Chris@0: 'entity.manager']; Chris@0: Chris@0: /** Chris@0: * Current user. Chris@0: * Chris@0: * @var \Drupal\Core\Session\AccountInterface Chris@0: */ Chris@0: protected $currentUser; Chris@0: Chris@0: /** Chris@0: * Comment manager service. Chris@0: * Chris@0: * @var \Drupal\comment\CommentManagerInterface Chris@0: */ Chris@0: protected $commentManager; Chris@0: Chris@0: /** Chris@0: * Module handler service. Chris@0: * Chris@0: * @var \Drupal\Core\Extension\ModuleHandlerInterface Chris@0: */ Chris@0: protected $moduleHandler; Chris@0: Chris@0: /** Chris@18: * The entity type manager service. Chris@0: * Chris@18: * @var \Drupal\Core\Entity\EntityTypeManagerInterface Chris@0: */ Chris@18: protected $entityTypeManager; Chris@0: Chris@0: /** Chris@0: * Constructs a new CommentLinkBuilder object. Chris@0: * Chris@0: * @param \Drupal\Core\Session\AccountInterface $current_user Chris@0: * Current user. Chris@0: * @param \Drupal\comment\CommentManagerInterface $comment_manager Chris@0: * Comment manager service. Chris@0: * @param \Drupal\Core\Extension\ModuleHandlerInterface $module_handler Chris@0: * Module handler service. Chris@0: * @param \Drupal\Core\StringTranslation\TranslationInterface $string_translation Chris@0: * String translation service. Chris@18: * @param \Drupal\Core\Entity\EntityTypeManagerInterface $entity_type_manager Chris@18: * The entity type manager. Chris@0: */ Chris@18: public function __construct(AccountInterface $current_user, CommentManagerInterface $comment_manager, ModuleHandlerInterface $module_handler, TranslationInterface $string_translation, EntityTypeManagerInterface $entity_type_manager) { Chris@0: $this->currentUser = $current_user; Chris@0: $this->commentManager = $comment_manager; Chris@0: $this->moduleHandler = $module_handler; Chris@0: $this->stringTranslation = $string_translation; Chris@18: $this->entityTypeManager = $entity_type_manager; Chris@0: } Chris@0: Chris@0: /** Chris@0: * {@inheritdoc} Chris@0: */ Chris@0: public function buildCommentedEntityLinks(FieldableEntityInterface $entity, array &$context) { Chris@0: $entity_links = []; Chris@0: $view_mode = $context['view_mode']; Chris@0: if ($view_mode == 'search_index' || $view_mode == 'search_result' || $view_mode == 'print' || $view_mode == 'rss') { Chris@0: // Do not add any links if the entity is displayed for: Chris@0: // - search indexing. Chris@0: // - constructing a search result excerpt. Chris@0: // - print. Chris@0: // - rss. Chris@0: return []; Chris@0: } Chris@0: Chris@0: $fields = $this->commentManager->getFields($entity->getEntityTypeId()); Chris@0: foreach ($fields as $field_name => $detail) { Chris@0: // Skip fields that the entity does not have. Chris@0: if (!$entity->hasField($field_name)) { Chris@0: continue; Chris@0: } Chris@0: $links = []; Chris@0: $commenting_status = $entity->get($field_name)->status; Chris@0: if ($commenting_status != CommentItemInterface::HIDDEN) { Chris@0: // Entity has commenting status open or closed. Chris@0: $field_definition = $entity->getFieldDefinition($field_name); Chris@0: if ($view_mode == 'teaser') { Chris@0: // Teaser view: display the number of comments that have been posted, Chris@0: // or a link to add new comments if the user has permission, the Chris@0: // entity is open to new comments, and there currently are none. Chris@0: if ($this->currentUser->hasPermission('access comments')) { Chris@0: if (!empty($entity->get($field_name)->comment_count)) { Chris@0: $links['comment-comments'] = [ Chris@0: 'title' => $this->formatPlural($entity->get($field_name)->comment_count, '1 comment', '@count comments'), Chris@0: 'attributes' => ['title' => $this->t('Jump to the first comment.')], Chris@0: 'fragment' => 'comments', Chris@17: 'url' => $entity->toUrl(), Chris@0: ]; Chris@0: if ($this->moduleHandler->moduleExists('history')) { Chris@0: $links['comment-new-comments'] = [ Chris@0: 'title' => '', Chris@0: 'url' => Url::fromRoute(''), Chris@0: 'attributes' => [ Chris@0: 'class' => 'hidden', Chris@0: 'title' => $this->t('Jump to the first new comment.'), Chris@0: 'data-history-node-last-comment-timestamp' => $entity->get($field_name)->last_comment_timestamp, Chris@0: 'data-history-node-field-name' => $field_name, Chris@0: ], Chris@0: ]; Chris@0: } Chris@0: } Chris@0: } Chris@0: // Provide a link to new comment form. Chris@0: if ($commenting_status == CommentItemInterface::OPEN) { Chris@0: $comment_form_location = $field_definition->getSetting('form_location'); Chris@0: if ($this->currentUser->hasPermission('post comments')) { Chris@0: $links['comment-add'] = [ Chris@0: 'title' => $this->t('Add new comment'), Chris@0: 'language' => $entity->language(), Chris@0: 'attributes' => ['title' => $this->t('Share your thoughts and opinions.')], Chris@0: 'fragment' => 'comment-form', Chris@0: ]; Chris@0: if ($comment_form_location == CommentItemInterface::FORM_SEPARATE_PAGE) { Chris@0: $links['comment-add']['url'] = Url::fromRoute('comment.reply', [ Chris@0: 'entity_type' => $entity->getEntityTypeId(), Chris@0: 'entity' => $entity->id(), Chris@0: 'field_name' => $field_name, Chris@0: ]); Chris@0: } Chris@0: else { Chris@17: $links['comment-add'] += ['url' => $entity->toUrl()]; Chris@0: } Chris@0: } Chris@0: elseif ($this->currentUser->isAnonymous()) { Chris@0: $links['comment-forbidden'] = [ Chris@0: 'title' => $this->commentManager->forbiddenMessage($entity, $field_name), Chris@0: ]; Chris@0: } Chris@0: } Chris@0: } Chris@0: else { Chris@0: // Entity in other view modes: add a "post comment" link if the user Chris@0: // is allowed to post comments and if this entity is allowing new Chris@0: // comments. Chris@0: if ($commenting_status == CommentItemInterface::OPEN) { Chris@0: $comment_form_location = $field_definition->getSetting('form_location'); Chris@0: if ($this->currentUser->hasPermission('post comments')) { Chris@0: // Show the "post comment" link if the form is on another page, or Chris@0: // if there are existing comments that the link will skip past. Chris@0: if ($comment_form_location == CommentItemInterface::FORM_SEPARATE_PAGE || (!empty($entity->get($field_name)->comment_count) && $this->currentUser->hasPermission('access comments'))) { Chris@0: $links['comment-add'] = [ Chris@0: 'title' => $this->t('Add new comment'), Chris@0: 'attributes' => ['title' => $this->t('Share your thoughts and opinions.')], Chris@0: 'fragment' => 'comment-form', Chris@0: ]; Chris@0: if ($comment_form_location == CommentItemInterface::FORM_SEPARATE_PAGE) { Chris@0: $links['comment-add']['url'] = Url::fromRoute('comment.reply', [ Chris@0: 'entity_type' => $entity->getEntityTypeId(), Chris@0: 'entity' => $entity->id(), Chris@0: 'field_name' => $field_name, Chris@0: ]); Chris@0: } Chris@0: else { Chris@17: $links['comment-add']['url'] = $entity->toUrl(); Chris@0: } Chris@0: } Chris@0: } Chris@0: elseif ($this->currentUser->isAnonymous()) { Chris@0: $links['comment-forbidden'] = [ Chris@0: 'title' => $this->commentManager->forbiddenMessage($entity, $field_name), Chris@0: ]; Chris@0: } Chris@0: } Chris@0: } Chris@0: } Chris@0: Chris@0: if (!empty($links)) { Chris@0: $entity_links['comment__' . $field_name] = [ Chris@0: '#theme' => 'links__entity__comment__' . $field_name, Chris@0: '#links' => $links, Chris@0: '#attributes' => ['class' => ['links', 'inline']], Chris@0: ]; Chris@0: if ($view_mode == 'teaser' && $this->moduleHandler->moduleExists('history') && $this->currentUser->isAuthenticated()) { Chris@0: $entity_links['comment__' . $field_name]['#cache']['contexts'][] = 'user'; Chris@0: $entity_links['comment__' . $field_name]['#attached']['library'][] = 'comment/drupal.node-new-comments-link'; Chris@0: // Embed the metadata for the "X new comments" link (if any) on this Chris@0: // entity. Chris@0: $entity_links['comment__' . $field_name]['#attached']['drupalSettings']['history']['lastReadTimestamps'][$entity->id()] = (int) history_read($entity->id()); Chris@0: $new_comments = $this->commentManager->getCountNewComments($entity); Chris@0: if ($new_comments > 0) { Chris@18: $page_number = $this->entityTypeManager Chris@0: ->getStorage('comment') Chris@0: ->getNewCommentPageNumber($entity->{$field_name}->comment_count, $new_comments, $entity, $field_name); Chris@0: $query = $page_number ? ['page' => $page_number] : NULL; Chris@0: $value = [ Chris@0: 'new_comment_count' => (int) $new_comments, Chris@18: 'first_new_comment_link' => $entity->toUrl('canonical', [ Chris@0: 'query' => $query, Chris@0: 'fragment' => 'new', Chris@18: ])->toString(), Chris@0: ]; Chris@0: $parents = ['comment', 'newCommentsLinks', $entity->getEntityTypeId(), $field_name, $entity->id()]; Chris@0: NestedArray::setValue($entity_links['comment__' . $field_name]['#attached']['drupalSettings'], $parents, $value); Chris@0: } Chris@0: } Chris@0: } Chris@0: } Chris@0: return $entity_links; Chris@0: } Chris@0: Chris@0: }