You are here

public function CommentManager::forbiddenMessage in Comment Permissions 8

Provides a message if posting comments is forbidden.

If authenticated users can post comments, a message is returned that prompts the anonymous user to log in (or register, if applicable) that redirects to entity comment form. Otherwise, no message is returned.

Parameters

\Drupal\Core\Entity\EntityInterface $entity: The entity to which comments are attached to.

string $field_name: The field name on the entity to which comments are attached to.

Return value

string HTML for a "you can't post comments" notice.

Overrides CommentManager::forbiddenMessage

File

src/CommentManager.php, line 20

Class

CommentManager
Overrides CommentManager service class.

Namespace

Drupal\comment_perm

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