You are here

public function PreviewLinkAccessCheck::access in Preview Link 8

Same name and namespace in other branches
  1. 2.x src/Access/PreviewLinkAccessCheck.php \Drupal\preview_link\Access\PreviewLinkAccessCheck::access()
  2. 2.0.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 43

Class

PreviewLinkAccessCheck
Preview link access check.

Namespace

Drupal\preview_link\Access

Code

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

  /** @var \Drupal\preview_link\Entity\PreviewLinkInterface $preview_link */
  $preview_link = $this->entityTypeManager
    ->getStorage('preview_link')
    ->getPreviewLink($entity);

  // If we can't find a valid preview link then ignore this.
  if (!$preview_link) {
    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 ($preview_token !== $preview_link
    ->getToken()) {
    return AccessResult::forbidden('Preview token is invalid.')
      ->addCacheableDependency($entity)
      ->addCacheContexts([
      'preview_link_route',
    ]);
  }
  return AccessResult::allowed()
    ->addCacheableDependency($entity)
    ->addCacheableDependency($preview_link)
    ->addCacheContexts([
    'preview_link_route',
  ]);
}