You are here

class CommentManager in Comment Permissions 8

Overrides CommentManager service class.

Hierarchy

Expanded class hierarchy of CommentManager

File

src/CommentManager.php, line 15

Namespace

Drupal\comment_perm
View source
class CommentManager extends CommentManagerBase {

  /**
   * {@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.
      $auth_role = $this->entityTypeManager
        ->getStorage('user_role')
        ->load(RoleInterface::AUTHENTICATED_ID);
      if ($auth_role
        ->hasPermission('post comments') || $auth_role
        ->hasPermission("post {$entity->bundle()} comments")) {
        $this->authenticatedCanPostComments = TRUE;
      }
    }
    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 '';
  }

}

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 CommentManager::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.