You are here

public function PreviewLinkAccessCheck::access in Preview Link 2.0.x

Same name and namespace in other branches
  1. 8 src/Access/PreviewLinkAccessCheck.php \Drupal\preview_link\Access\PreviewLinkAccessCheck::access()
  2. 2.x src/Access/PreviewLinkAccessCheck.php \Drupal\preview_link\Access\PreviewLinkAccessCheck::access()

Checks access to the node add page for the node type.

Parameters

\Drupal\Core\Entity\EntityInterface $entity: The entity.

string $preview_token: The preview token.

Return value

\Drupal\Core\Access\AccessResult A \Drupal\Core\Access\AccessInterface value.

File

src/Access/PreviewLinkAccessCheck.php, line 46

Class

PreviewLinkAccessCheck
Preview link access check.

Namespace

Drupal\preview_link\Access

Code

public function access(EntityInterface $entity = NULL, string $preview_token = NULL) : AccessResultInterface {
  $neutral = AccessResult::neutral()
    ->addCacheableDependency($entity)
    ->addCacheContexts([
    'preview_link_route',
  ]);
  if (!$preview_token || !$entity) {
    return $neutral;
  }

  // If we can't find a valid preview link then ignore this.
  if (!$this->previewLinkHost
    ->hasPreviewLinks($entity)) {
    return $neutral
      ->setReason('This entity does not have a preview link.');
  }

  // If an entity has a preview link and it doesnt match up, then explicitly
  // deny access.
  if (!$this->previewLinkHost
    ->isToken($entity, [
    $preview_token,
  ])) {
    return AccessResult::forbidden('Preview token is invalid.')
      ->addCacheableDependency($entity)
      ->addCacheContexts([
      'preview_link_route',
    ]);
  }
  return AccessResult::allowed()
    ->addCacheableDependency($entity)
    ->addCacheContexts([
    'preview_link_route',
  ]);
}