Commit 2e4c28af authored by catch's avatar catch
Browse files

task: #3543035 Deprecate CommentManagerInterface::getCountNewComments

By: @mstrelan
By: @dcam
By: @catch
By: @deepakkm
(cherry picked from commit 30a2de10)
parent eb8f115c
Loading
Loading
Loading
Loading
Loading
+4 −38
Original line number Diff line number Diff line
@@ -196,45 +196,11 @@ public function forbiddenMessage(EntityInterface $entity, $field_name) {
   * {@inheritdoc}
   */
  public function getCountNewComments(EntityInterface $entity, $field_name = NULL, $timestamp = 0) {
    // @todo Replace module handler with optional history service injection
    //   after https://www.drupal.org/node/2081585.
    if ($this->currentUser->isAuthenticated() && $this->moduleHandler->moduleExists('history')) {
      // Retrieve the timestamp at which the current user last viewed this
      // entity.
      if (!$timestamp) {
        if ($entity->getEntityTypeId() == 'node') {
          $timestamp = history_read($entity->id());
        }
        else {
          $function = $entity->getEntityTypeId() . '_last_viewed';
          if (function_exists($function)) {
            $timestamp = $function($entity->id());
          }
          else {
            // Default to 30 days ago.
            // @todo Remove this else branch when we have a generic
            //   HistoryRepository service in https://www.drupal.org/i/3267011.
            $timestamp = COMMENT_NEW_LIMIT;
          }
        }
      }
      $timestamp = ($timestamp > HISTORY_READ_LIMIT ? $timestamp : HISTORY_READ_LIMIT);

      // Use the timestamp to retrieve the number of new comments.
      $query = $this->entityTypeManager->getStorage('comment')->getQuery()
        ->accessCheck(TRUE)
        ->condition('entity_type', $entity->getEntityTypeId())
        ->condition('entity_id', $entity->id())
        ->condition('created', $timestamp, '>')
        ->condition('status', CommentInterface::PUBLISHED);
      if ($field_name) {
        // Limit to a particular field.
        $query->condition('field_name', $field_name);
      }

      return $query->count()->execute();
    }
    @trigger_error(__FUNCTION__ . '() is deprecated in drupal:11.3.0 and is removed from drupal:12.0.0. Use \Drupal\history\HistoryManager::getCountNewComments() instead. See https://www.drupal.org/project/drupal/issues/3551729', 'E_USER_DEPRECATED');
    if (!$this->moduleHandler->moduleExists('history')) {
      return FALSE;
    }
    return \Drupal::service('Drupal\history\HistoryManager')->getCountNewComments($entity, $field_name, $timestamp);
  }

}
+5 −0
Original line number Diff line number Diff line
@@ -73,6 +73,11 @@ public function forbiddenMessage(EntityInterface $entity, $field_name);
   * @return int|false
   *   The number of new comments or FALSE if the user is not authenticated or
   *   if the History module is not installed.
   *
   * @deprecated in drupal:11.3.0 and is removed from drupal:12.0.0. Use
   *   \Drupal\history\HistoryManager::getCountNewComments instead.
   *
   * @see https://www.drupal.org/project/drupal/issues/3551729
   */
  public function getCountNewComments(EntityInterface $entity, $field_name = NULL, $timestamp = 0);

+0 −8
Original line number Diff line number Diff line
@@ -41,10 +41,6 @@ public function tokenInfo(): array {
          'name' => $this->t("Comment count"),
          'description' => $this->t("The number of comments posted on an entity."),
        ];
        $tokens[$token_type]['comment-count-new'] = [
          'name' => $this->t("New comment count"),
          'description' => $this->t("The number of comments posted on an entity since the reader last viewed it."),
        ];
      }
    }
    // Core comment tokens.
@@ -249,10 +245,6 @@ public function tokens($type, $tokens, array $data, array $options, BubbleableMe
            }
            $replacements[$original] = $count;
            break;

          case 'comment-count-new':
            $replacements[$original] = \Drupal::service('comment.manager')->getCountNewComments($entity);
            break;
        }
      }
    }
+4 −1
Original line number Diff line number Diff line
@@ -5,4 +5,7 @@ services:
    class: Drupal\history\HistoryCommentLinkBuilder
    decorates: 'comment.link_builder'
    decoration_on_invalid: ignore
    arguments: ['@history.comment_link_builder.inner', '@comment.manager', '@current_user', '@entity_type.manager']
    arguments: ['@history.comment_link_builder.inner', '@comment.manager', '@current_user', '@entity_type.manager', '@Drupal\history\HistoryManager']
  Drupal\history\HistoryManager:
    class: Drupal\history\HistoryManager
    autowire: true
+2 −1
Original line number Diff line number Diff line
@@ -3,6 +3,7 @@
namespace Drupal\history\Controller;

use Drupal\Core\Url;
use Drupal\history\HistoryManager;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\JsonResponse;
use Symfony\Component\HttpKernel\Exception\AccessDeniedHttpException;
@@ -88,7 +89,7 @@ public function renderNewCommentsNodeLinks(Request $request): JsonResponse {
    $links = [];
    foreach ($nids as $nid) {
      $node = $this->entityTypeManager()->getStorage('node')->load($nid);
      $new = \Drupal::service('comment.manager')->getCountNewComments($node);
      $new = \Drupal::service(HistoryManager::class)->getCountNewComments($node);
      $page_number = $this->entityTypeManager()->getStorage('comment')
        ->getNewCommentPageNumber($node->{$field_name}->comment_count, $new, $node, $field_name);
      $query = $page_number ? ['page' => $page_number] : NULL;
Loading