You are here

protected function ContactBlock::blockAccess in Contact Block 8

Indicates whether the block should be shown.

Blocks with specific access checking should override this method rather than access(), in order to avoid repeating the handling of the $return_as_object argument.

Parameters

\Drupal\Core\Session\AccountInterface $account: The user session for which to check access.

Return value

\Drupal\Core\Access\AccessResult The access result.

Overrides BlockPluginTrait::blockAccess

See also

self::access()

File

src/Plugin/Block/ContactBlock.php, line 131

Class

ContactBlock
Provides a 'ContactBlock' block.

Namespace

Drupal\contact_block\Plugin\Block

Code

protected function blockAccess(AccountInterface $account) {
  $contact_form = $this
    ->getContactForm();
  $contact_message = $this
    ->createContactMessage();

  // Deny access when the configured contact form has been deleted.
  if (empty($contact_form)) {
    return AccessResult::forbidden();
  }
  if ($contact_message
    ->isPersonal()) {

    /** @var \Drupal\user\Entity\User $user */
    $user = $this->routeMatch
      ->getParameter('user');

    // Deny access to the contact form if we are not on a user related page
    // or we have no access to that page.
    if (empty($user)) {
      return AccessResult::forbidden();
    }

    // Use the regular personal contact access service to check.
    return $this->checkContactPageAccess
      ->access($user, $account);
  }

  // Access to other contact forms is equal to the permission of the
  // entity.contact_form.canonical route.
  return $contact_form
    ->access('view', $account, TRUE);
}