You are here

protected function ContactPopupBlock::blockAccess in Contact Form Popup 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/ContactPopupBlock.php, line 76

Class

ContactPopupBlock
Provides a 'ContactPopupBlock' block.

Namespace

Drupal\contact_poup\Plugin\Block

Code

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

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

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

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

    // Do not display the link if the user is on his profile page.
    if ($user
      ->id() == $account
      ->id()) {
      return AccessResult::forbidden();
    }
    return AccessResult::allowedIfHasPermission($account, 'access user contact forms');
  }

  // Access to other contact forms is equal to the permission of the
  // entity.contact_form.canonical route. Once https://www.drupal.org/node/2724503
  // has landed, see if we can support per form access permission.
  return $contact_form
    ->access('view', $account, TRUE);
}