You are here

public function CommentManager::forbiddenMessage in Zircon Profile 8

Same name and namespace in other branches
  1. 8.0 core/modules/comment/src/CommentManager.php \Drupal\comment\CommentManager::forbiddenMessage()

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 CommentManagerInterface::forbiddenMessage

File

core/modules/comment/src/CommentManager.php, line 150
Contains \Drupal\comment\CommentManager.

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->entityManager
      ->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 = array(
        'destination' => $this
          ->url('comment.reply', $comment_reply_parameters, array(
          'fragment' => 'comment-form',
        )),
      );
    }
    else {
      $destination = array(
        'destination' => $entity
          ->url('canonical', array(
          'fragment' => 'comment-form',
        )),
      );
    }
    if ($this->userConfig
      ->get('register') != USER_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', array(
        ':login' => $this->urlGenerator
          ->generateFromRoute('user.login', array(), array(
          'query' => $destination,
        )),
        ':register' => $this->urlGenerator
          ->generateFromRoute('user.register', array(), array(
          'query' => $destination,
        )),
      ));
    }
    else {

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