You are here

public function ApplicableTransactionAccess::access in Transaction 8

Check if the transaction type is applicable to the content entity.

Parameters

\Drupal\Core\Entity\ContentEntityInterface|null $entity: (optional) The involved content entity, determined from request if NULL.

\Drupal\transaction\TransactionTypeInterface|null $transaction_type: (optional) The transaction type, determined from the request or the route options if NULL.

\Symfony\Component\Routing\Route|null $route: (optional) The route to check access for.

\Symfony\Component\HttpFoundation\Request|null $request: (optional) The current request.

Return value

\Drupal\Core\Access\AccessResultInterface Allowed if the transaction type is applicable to the content entity.

File

src/Access/ApplicableTransactionAccess.php, line 74

Class

ApplicableTransactionAccess
Checks access of applicable entity to transaction type.

Namespace

Drupal\transaction\Access

Code

public function access(ContentEntityInterface $entity = NULL, TransactionTypeInterface $transaction_type = NULL, Route $route = NULL, Request $request = NULL) {

  // Check access for the current route if none was given.
  if (!$route) {
    $route = $this->currentRouteMatch
      ->getRouteObject();
  }
  if (!$request) {
    $request = $this->requestStack
      ->getCurrentRequest();
  }

  // Try to determine the transaction type if it was not given.
  $transaction_type = $transaction_type ? $transaction_type : $this
    ->guessTransactionType($route, $request);
  if (!$transaction_type) {

    // Unable to determine the transaction type.
    return AccessResult::forbidden();
  }

  // Try to determine the target entity.
  $entity = $entity ? $entity : $this
    ->guessTargetEntity($route, $request);
  if (!$entity) {

    // Unable to determine the target entity.
    return AccessResult::forbidden();
  }
  $result = $transaction_type
    ->isApplicable($entity) ? AccessResult::allowed() : AccessResult::forbidden();
  return $result
    ->addCacheableDependency($entity)
    ->addCacheableDependency($transaction_type);
}