Verified Commit 9a2fc401 authored by godotislate's avatar godotislate
Browse files

refactor: #3550054 Move FORM_SEPARATE_PAGE and FORM_BELOW constants in...

refactor: #3550054 Move FORM_SEPARATE_PAGE and FORM_BELOW constants in CommentItemInterface to new FormLocation enum

By: mstrelan
By: smustgrave
By: godotislate
(cherry picked from commit 90bc2112)
parent 77799e3e
Loading
Loading
Loading
Loading
Loading
+5 −6
Original line number Diff line number Diff line
@@ -2,7 +2,6 @@

namespace Drupal\comment;

use Drupal\comment\Plugin\Field\FieldType\CommentItemInterface;
use Drupal\Core\DependencyInjection\DeprecatedServicePropertyTrait;
use Drupal\Core\Entity\EntityTypeManagerInterface;
use Drupal\Core\Entity\FieldableEntityInterface;
@@ -117,7 +116,7 @@ public function buildCommentedEntityLinks(FieldableEntityInterface $entity, arra
          }
          // Provide a link to new comment form.
          if ($commenting_status == CommentingStatus::Open) {
            $comment_form_location = $field_definition->getSetting('form_location');
            $comment_form_location = FormLocation::tryFrom((int) $field_definition->getSetting('form_location'));
            if ($this->currentUser->hasPermission('post comments')) {
              $links['comment-add'] = [
                'title' => $this->t('Add new comment'),
@@ -125,7 +124,7 @@ public function buildCommentedEntityLinks(FieldableEntityInterface $entity, arra
                'attributes' => ['title' => $this->t('Share your thoughts and opinions.')],
                'fragment' => 'comment-form',
              ];
              if ($comment_form_location == CommentItemInterface::FORM_SEPARATE_PAGE) {
              if ($comment_form_location == FormLocation::SeparatePage) {
                $links['comment-add']['url'] = Url::fromRoute('comment.reply', [
                  'entity_type' => $entity->getEntityTypeId(),
                  'entity' => $entity->id(),
@@ -148,17 +147,17 @@ public function buildCommentedEntityLinks(FieldableEntityInterface $entity, arra
          // is allowed to post comments and if this entity is allowing new
          // comments.
          if ($commenting_status == CommentingStatus::Open) {
            $comment_form_location = $field_definition->getSetting('form_location');
            $comment_form_location = FormLocation::tryFrom((int) $field_definition->getSetting('form_location'));
            if ($this->currentUser->hasPermission('post comments')) {
              // Show the "post comment" link if the form is on another page, or
              // if there are existing comments that the link will skip past.
              if ($comment_form_location == CommentItemInterface::FORM_SEPARATE_PAGE || (!empty($entity->get($field_name)->comment_count) && $this->currentUser->hasPermission('access comments'))) {
              if ($comment_form_location == FormLocation::SeparatePage || (!empty($entity->get($field_name)->comment_count) && $this->currentUser->hasPermission('access comments'))) {
                $links['comment-add'] = [
                  'title' => $this->t('Add new comment'),
                  'attributes' => ['title' => $this->t('Share your thoughts and opinions.')],
                  'fragment' => 'comment-form',
                ];
                if ($comment_form_location == CommentItemInterface::FORM_SEPARATE_PAGE) {
                if ($comment_form_location == FormLocation::SeparatePage) {
                  $links['comment-add']['url'] = Url::fromRoute('comment.reply', [
                    'entity_type' => $entity->getEntityTypeId(),
                    'entity' => $entity->id(),
+1 −2
Original line number Diff line number Diff line
@@ -2,7 +2,6 @@

namespace Drupal\comment;

use Drupal\comment\Plugin\Field\FieldType\CommentItemInterface;
use Drupal\Core\Config\ConfigFactoryInterface;
use Drupal\Core\Entity\EntityDisplayRepositoryInterface;
use Drupal\Core\Entity\EntityFieldManagerInterface;
@@ -163,7 +162,7 @@ public function forbiddenMessage(EntityInterface $entity, $field_name) {
    if ($this->authenticatedCanPostComments) {
      // We cannot use the redirect.destination service here because these links
      // sometimes appear on /node and taxonomy listing pages.
      if ($entity->get($field_name)->getFieldDefinition()->getSetting('form_location') == CommentItemInterface::FORM_SEPARATE_PAGE) {
      if ($entity->get($field_name)->getFieldDefinition()->getSetting('form_location') == FormLocation::SeparatePage->value) {
        $comment_reply_parameters = [
          'entity_type' => $entity->getEntityTypeId(),
          'entity' => $entity->id(),
+18 −0
Original line number Diff line number Diff line
<?php

declare(strict_types=1);

namespace Drupal\comment;

/**
 * Options for comment form display.
 */
enum FormLocation: int {

  // Comment form should be displayed on a separate page.
  case SeparatePage = 0;

  // Comment form should be shown below post or list of comments.
  case Below = 1;

}
+3 −2
Original line number Diff line number Diff line
@@ -3,7 +3,7 @@
namespace Drupal\comment\Plugin\Field\FieldFormatter;

use Drupal\comment\CommentingStatus;
use Drupal\comment\Plugin\Field\FieldType\CommentItemInterface;
use Drupal\comment\FormLocation;
use Drupal\Core\Entity\Entity\EntityViewDisplay;
use Drupal\Core\Entity\EntityDisplayRepositoryInterface;
use Drupal\Core\Entity\EntityFormBuilderInterface;
@@ -167,7 +167,8 @@ public function viewElements(FieldItemListInterface $items, $langcode) {

      // Append comment form if the comments are open and the form is set to
      // display below the entity. Do not show the form for the print view mode.
      if ($status == CommentingStatus::Open && $comment_settings['form_location'] == CommentItemInterface::FORM_BELOW && $this->viewMode != 'print') {
      $form_location = FormLocation::tryFrom($comment_settings['form_location']);
      if ($status == CommentingStatus::Open && $form_location == FormLocation::Below && $this->viewMode != 'print') {
        // Only show the add comment form if the user has permission.
        $elements['#cache']['contexts'][] = 'user.roles';
        if ($this->currentUser->hasPermission('post comments')) {
+2 −1
Original line number Diff line number Diff line
@@ -8,6 +8,7 @@
use Drupal\comment\CommentManagerInterface;
use Drupal\comment\CommentPreviewMode;
use Drupal\comment\Entity\CommentType;
use Drupal\comment\FormLocation;
use Drupal\Core\Field\Attribute\FieldType;
use Drupal\Core\Field\FieldDefinitionInterface;
use Drupal\Core\Field\FieldItemBase;
@@ -48,7 +49,7 @@ public static function defaultFieldSettings() {
    return [
      'default_mode' => CommentManagerInterface::COMMENT_MODE_THREADED,
      'per_page' => 50,
      'form_location' => CommentItemInterface::FORM_BELOW,
      'form_location' => FormLocation::Below->value,
      'anonymous' => AnonymousContact::Forbidden->value,
      'preview' => CommentPreviewMode::Optional->value,
    ] + parent::defaultFieldSettings();
Loading