function CommentManager::forbiddenMessage

Same name in other branches
  1. 9 core/modules/comment/src/CommentManager.php \Drupal\comment\CommentManager::forbiddenMessage()
  2. 8.9.x core/modules/comment/src/CommentManager.php \Drupal\comment\CommentManager::forbiddenMessage()
  3. 11.x core/modules/comment/src/CommentManager.php \Drupal\comment\CommentManager::forbiddenMessage()

File

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

Class

CommentManager
Comment manager contains common functions to manage comment fields.

Namespace

Drupal\comment

Code

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 '';
}

Buggy or inaccurate documentation? Please file an issue. Need support? Need help programming? Connect with the Drupal community.