You are here

class CommentManager in Drupal 8

Same name and namespace in other branches
  1. 9 core/modules/comment/src/CommentManager.php \Drupal\comment\CommentManager
  2. 10 core/modules/comment/src/CommentManager.php \Drupal\comment\CommentManager

Comment manager contains common functions to manage comment fields.

Hierarchy

Expanded class hierarchy of CommentManager

1 file declares its use of CommentManager
CommentManagerTest.php in core/modules/comment/tests/src/Unit/CommentManagerTest.php
1 string reference to 'CommentManager'
comment.services.yml in core/modules/comment/comment.services.yml
core/modules/comment/comment.services.yml
1 service uses CommentManager
comment.manager in core/modules/comment/comment.services.yml
Drupal\comment\CommentManager

File

core/modules/comment/src/CommentManager.php, line 26

Namespace

Drupal\comment
View source
class CommentManager implements CommentManagerInterface {
  use StringTranslationTrait;
  use DeprecatedServicePropertyTrait;

  /**
   * {@inheritdoc}
   */
  protected $deprecatedProperties = [
    'entityManager' => 'entity.manager',
  ];

  /**
   * The entity field manager.
   *
   * @var \Drupal\Core\Entity\EntityFieldManagerInterface
   */
  protected $entityFieldManager;

  /**
   * The entity display repository.
   *
   * @var \Drupal\Core\Entity\EntityDisplayRepositoryInterface
   */
  protected $entityDisplayRepository;

  /**
   * The entity type manager.
   *
   * @var \Drupal\Core\Entity\EntityTypeManagerInterface
   */
  protected $entityTypeManager;

  /**
   * Whether the \Drupal\user\RoleInterface::AUTHENTICATED_ID can post comments.
   *
   * @var bool
   */
  protected $authenticatedCanPostComments;

  /**
   * The user settings config object.
   *
   * @var \Drupal\Core\Config\Config
   */
  protected $userConfig;

  /**
   * The module handler service.
   *
   * @var \Drupal\Core\Extension\ModuleHandlerInterface
   */
  protected $moduleHandler;

  /**
   * The current user.
   *
   * @var \Drupal\Core\Session\AccountInterface
   */
  protected $currentUser;

  /**
   * Construct the CommentManager object.
   *
   * @param \Drupal\Core\Entity\EntityTypeManagerInterface $entity_type_manager
   *   The entity type manager service.
   * @param \Drupal\Core\Config\ConfigFactoryInterface $config_factory
   *   The config factory.
   * @param \Drupal\Core\StringTranslation\TranslationInterface $string_translation
   *   The string translation service.
   * @param \Drupal\Core\Extension\ModuleHandlerInterface $module_handler
   *   The module handler service.
   * @param \Drupal\Core\Session\AccountInterface $current_user
   *   The current user.
   * @param \Drupal\Core\Entity\EntityFieldManagerInterface $entity_field_manager
   *   The entity field manager service.
   * @param \Drupal\Core\Entity\EntityDisplayRepositoryInterface $entity_display_repository
   *   The entity display repository service.
   */
  public function __construct(EntityTypeManagerInterface $entity_type_manager, ConfigFactoryInterface $config_factory, TranslationInterface $string_translation, ModuleHandlerInterface $module_handler, AccountInterface $current_user, EntityFieldManagerInterface $entity_field_manager = NULL, EntityDisplayRepositoryInterface $entity_display_repository = NULL) {
    $this->entityTypeManager = $entity_type_manager;
    $this->userConfig = $config_factory
      ->get('user.settings');
    $this->stringTranslation = $string_translation;
    $this->moduleHandler = $module_handler;
    $this->currentUser = $current_user;
    if (!$entity_field_manager) {
      @trigger_error('The entity_field.manager service must be passed to CommentManager::__construct(), it is required before Drupal 9.0.0. See https://www.drupal.org/node/2549139.', E_USER_DEPRECATED);
      $entity_field_manager = \Drupal::service('entity_field.manager');
    }
    $this->entityFieldManager = $entity_field_manager;
    if (!$entity_display_repository) {
      @trigger_error('The entity_display.repository service must be passed to CommentManager::__construct(), it is required before Drupal 9.0.0. See https://www.drupal.org/node/2835616.', E_USER_DEPRECATED);
      $entity_display_repository = \Drupal::service('entity_display.repository');
    }
    $this->entityDisplayRepository = $entity_display_repository;
  }

  /**
   * {@inheritdoc}
   */
  public function getFields($entity_type_id) {
    $entity_type = $this->entityTypeManager
      ->getDefinition($entity_type_id);
    if (!$entity_type
      ->entityClassImplements(FieldableEntityInterface::class)) {
      return [];
    }
    $map = $this->entityFieldManager
      ->getFieldMapByFieldType('comment');
    return isset($map[$entity_type_id]) ? $map[$entity_type_id] : [];
  }

  /**
   * {@inheritdoc}
   */
  public function addBodyField($comment_type_id) {
    if (!FieldConfig::loadByName('comment', $comment_type_id, 'comment_body')) {

      // Attaches the body field by default.
      $field = $this->entityTypeManager
        ->getStorage('field_config')
        ->create([
        'label' => 'Comment',
        'bundle' => $comment_type_id,
        'required' => TRUE,
        'field_storage' => FieldStorageConfig::loadByName('comment', 'comment_body'),
      ]);
      $field
        ->save();

      // Assign widget settings for the default form mode.
      $this->entityDisplayRepository
        ->getFormDisplay('comment', $comment_type_id)
        ->setComponent('comment_body', [
        'type' => 'text_textarea',
      ])
        ->save();

      // Assign display settings for the default view mode.
      $this->entityDisplayRepository
        ->getViewDisplay('comment', $comment_type_id)
        ->setComponent('comment_body', [
        'label' => 'hidden',
        'type' => 'text_default',
        'weight' => 0,
      ])
        ->save();
    }
  }

  /**
   * {@inheritdoc}
   */
  public function forbiddenMessage(EntityInterface $entity, $field_name) {
    if (!isset($this->authenticatedCanPostComments)) {

      // We only output a link if we are certain that users will get the
      // permission to post comments by logging in.
      $this->authenticatedCanPostComments = $this->entityTypeManager
        ->getStorage('user_role')
        ->load(RoleInterface::AUTHENTICATED_ID)
        ->hasPermission('post comments');
    }
    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) {
        $comment_reply_parameters = [
          'entity_type' => $entity
            ->getEntityTypeId(),
          'entity' => $entity
            ->id(),
          'field_name' => $field_name,
        ];
        $destination = [
          'destination' => Url::fromRoute('comment.reply', $comment_reply_parameters, [
            'fragment' => 'comment-form',
          ])
            ->toString(),
        ];
      }
      else {
        $destination = [
          'destination' => $entity
            ->toUrl('canonical', [
            'fragment' => 'comment-form',
          ])
            ->toString(),
        ];
      }
      if ($this->userConfig
        ->get('register') != UserInterface::REGISTER_ADMINISTRATORS_ONLY) {

        // Users can register themselves.
        return $this
          ->t('<a href=":login">Log in</a> or <a href=":register">register</a> to post comments', [
          ':login' => Url::fromRoute('user.login', [], [
            'query' => $destination,
          ])
            ->toString(),
          ':register' => Url::fromRoute('user.register', [], [
            'query' => $destination,
          ])
            ->toString(),
        ]);
      }
      else {

        // Only admins can add new users, no public registration.
        return $this
          ->t('<a href=":login">Log in</a> to post comments', [
          ':login' => Url::fromRoute('user.login', [], [
            'query' => $destination,
          ])
            ->toString(),
        ]);
      }
    }
    return '';
  }

  /**
   * {@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 once https://www.drupal.org/node/1029708 lands.
            $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()
        ->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();
    }
    return FALSE;
  }

}

Members

Namesort descending Modifiers Type Description Overrides
CommentManager::$authenticatedCanPostComments protected property Whether the \Drupal\user\RoleInterface::AUTHENTICATED_ID can post comments.
CommentManager::$currentUser protected property The current user.
CommentManager::$deprecatedProperties protected property
CommentManager::$entityDisplayRepository protected property The entity display repository.
CommentManager::$entityFieldManager protected property The entity field manager.
CommentManager::$entityTypeManager protected property The entity type manager.
CommentManager::$moduleHandler protected property The module handler service.
CommentManager::$userConfig protected property The user settings config object.
CommentManager::addBodyField public function Creates a comment_body field. Overrides CommentManagerInterface::addBodyField
CommentManager::forbiddenMessage public function Provides a message if posting comments is forbidden. Overrides CommentManagerInterface::forbiddenMessage
CommentManager::getCountNewComments public function Returns the number of new comments available on a given entity for a user. Overrides CommentManagerInterface::getCountNewComments
CommentManager::getFields public function Utility function to return an array of comment fields. Overrides CommentManagerInterface::getFields
CommentManager::__construct public function Construct the CommentManager object.
CommentManagerInterface::COMMENT_MODE_FLAT constant Comments are displayed in a flat list - expanded.
CommentManagerInterface::COMMENT_MODE_THREADED constant Comments are displayed as a threaded list - expanded.
DeprecatedServicePropertyTrait::__get public function Allows to access deprecated/removed properties.
StringTranslationTrait::$stringTranslation protected property The string translation service. 1
StringTranslationTrait::formatPlural protected function Formats a string containing a count of items.
StringTranslationTrait::getNumberOfPlurals protected function Returns the number of plurals supported by a given language.
StringTranslationTrait::getStringTranslation protected function Gets the string translation service.
StringTranslationTrait::setStringTranslation public function Sets the string translation service to use. 2
StringTranslationTrait::t protected function Translates a string to the current language or to a given language.