Verified Commit 19163c55 authored by godotislate's avatar godotislate
Browse files

feat: #3547353 Move HIDDEN, CLOSED and OPEN constants in CommentItemInterface...

feat: #3547353 Move HIDDEN, CLOSED and OPEN constants in CommentItemInterface to new CommentingStatus enum

By: mstrelan
By: smustgrave
By: godotislate
parent f5b6b5ef
Loading
Loading
Loading
Loading
Loading
+2 −3
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\Access\AccessResult;
use Drupal\Core\Entity\EntityAccessControlHandler;
use Drupal\Core\Entity\EntityInterface;
@@ -109,9 +108,9 @@ protected function checkFieldAccess($operation, FieldDefinitionInterface $field_
        if ($commented_entity && $comment_field_name) {
          // We are creating a new comment, user can edit create only fields if
          // commenting is open.
          $commenting_status = (int) $commented_entity->get($comment_field_name)->status;
          $commenting_status = CommentingStatus::tryFrom((int) $commented_entity->get($comment_field_name)->status);
          $access_result = $access_result
            ->andIf(AccessResult::allowedIf($commenting_status !== CommentItemInterface::CLOSED))
            ->andIf(AccessResult::allowedIf($commenting_status !== CommentingStatus::Closed))
            ->addCacheableDependency($commented_entity);
        }
        return $access_result;
+2 −3
Original line number Diff line number Diff line
@@ -2,7 +2,6 @@

namespace Drupal\comment;

use Drupal\comment\Plugin\Field\FieldType\CommentItemInterface;
use Drupal\Component\Datetime\TimeInterface;
use Drupal\Component\Utility\Html;
use Drupal\Component\Utility\Unicode;
@@ -400,7 +399,7 @@ public function preview(array &$form, FormStateInterface $form_state) {
      // field output.
      $field_name = $comment->getFieldName();
      $entity = clone $entity;
      $entity->$field_name->status = CommentItemInterface::HIDDEN;
      $entity->$field_name->status = CommentingStatus::Hidden->value;
      $build = $this->entityTypeManager
        ->getViewBuilder($entity->getEntityTypeId())
        ->view($entity);
@@ -425,7 +424,7 @@ public function save(array $form, FormStateInterface $form_state) {
    $uri = $entity->toUrl();
    $logger = $this->logger('comment');

    if ($this->currentUser->hasPermission('post comments') && ($this->currentUser->hasPermission('administer comments') || $entity->{$field_name}->status == CommentItemInterface::OPEN)) {
    if ($this->currentUser->hasPermission('post comments') && ($this->currentUser->hasPermission('administer comments') || $entity->{$field_name}->status == CommentingStatus::Open->value)) {
      $comment->save();
      $form_state->setValue('cid', $comment->id());

+2 −3
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\Entity\EntityFormBuilderInterface;
use Drupal\Core\Entity\EntityInterface;
use Drupal\Core\Entity\EntityTypeManagerInterface;
@@ -174,9 +173,9 @@ public function renderLinks($comment_entity_id, $view_mode, $langcode, $is_in_pr
   */
  protected function buildLinks(CommentInterface $entity, EntityInterface $commented_entity) {
    $links = [];
    $status = $commented_entity->get($entity->getFieldName())->status;
    $status = CommentingStatus::tryFrom((int) $commented_entity->get($entity->getFieldName())->status);

    if ($status == CommentItemInterface::OPEN) {
    if ($status == CommentingStatus::Open) {
      if ($entity->access('delete')) {
        $links['comment-delete'] = [
          'title' => $this->t('Delete'),
+4 −4
Original line number Diff line number Diff line
@@ -74,8 +74,8 @@ public function buildCommentedEntityLinks(FieldableEntityInterface $entity, arra
        continue;
      }
      $links = [];
      $commenting_status = $entity->get($field_name)->status;
      if ($commenting_status != CommentItemInterface::HIDDEN) {
      $commenting_status = CommentingStatus::tryFrom((int) $entity->get($field_name)->status);
      if ($commenting_status != CommentingStatus::Hidden) {
        // Entity has commenting status open or closed.
        $field_definition = $entity->getFieldDefinition($field_name);
        if ($view_mode == 'teaser') {
@@ -93,7 +93,7 @@ public function buildCommentedEntityLinks(FieldableEntityInterface $entity, arra
            }
          }
          // Provide a link to new comment form.
          if ($commenting_status == CommentItemInterface::OPEN) {
          if ($commenting_status == CommentingStatus::Open) {
            $comment_form_location = $field_definition->getSetting('form_location');
            if ($this->currentUser->hasPermission('post comments')) {
              $links['comment-add'] = [
@@ -124,7 +124,7 @@ public function buildCommentedEntityLinks(FieldableEntityInterface $entity, arra
          // Entity in other view modes: add a "post comment" link if the user
          // is allowed to post comments and if this entity is allowing new
          // comments.
          if ($commenting_status == CommentItemInterface::OPEN) {
          if ($commenting_status == CommentingStatus::Open) {
            $comment_form_location = $field_definition->getSetting('form_location');
            if ($this->currentUser->hasPermission('post comments')) {
              // Show the "post comment" link if the form is on another page, or
+32 −0
Original line number Diff line number Diff line
<?php

declare(strict_types=1);

namespace Drupal\comment;

use Drupal\Core\StringTranslation\TranslatableMarkup;
use Drupal\Core\Utility\OptionsEnumTrait;

/**
 * Defines the comment field status options.
 */
enum CommentingStatus: int {

  use OptionsEnumTrait;

  case Open = 2;
  case Closed = 1;
  case Hidden = 0;

  /**
   * {@inheritdoc}
   */
  public function label(): string|\Stringable {
    return match ($this) {
      self::Open => new TranslatableMarkup('Open'),
      self::Closed => new TranslatableMarkup('Closed'),
      self::Hidden => new TranslatableMarkup('Hidden'),
    };
  }

}
Loading