You are here

protected function ApplicableTransactionAccess::guessTargetEntity in Transaction 8

Tries to determine the target entity type from request and route.

Parameters

\Symfony\Component\Routing\Route $route: The route to check access for.

\Symfony\Component\HttpFoundation\Request $request: The current request.

Return value

\Drupal\Core\Entity\ContentEntityInterface|null The target entity type, NULL if cannot be determined.

1 call to ApplicableTransactionAccess::guessTargetEntity()
ApplicableTransactionAccess::access in src/Access/ApplicableTransactionAccess.php
Check if the transaction type is applicable to the content entity.

File

src/Access/ApplicableTransactionAccess.php, line 145

Class

ApplicableTransactionAccess
Checks access of applicable entity to transaction type.

Namespace

Drupal\transaction\Access

Code

protected function guessTargetEntity(Route $route, Request $request) {

  // First try from request argument.
  $entity = $request
    ->get('target_entity');
  if (!$entity instanceof ContentEntityInterface) {

    // Try from request argument named as the target entity type.
    $target_entity_type = $request
      ->get('target_entity_type');
    if ($target_entity_type instanceof EntityTypeInterface) {
      $target_entity_type_id = $target_entity_type
        ->id();
    }
    else {
      $target_entity_type_id = is_string($target_entity_type) ? $target_entity_type : $route
        ->getOption('_transaction_target_entity_type_id');
    }
    if ($target_entity_type_id) {
      $entity = $request
        ->get($target_entity_type_id) ?: $entity;
      if (is_numeric($entity)) {
        $entity = $this->entityTypeManager
          ->getStorage($target_entity_type_id)
          ->load($entity);
      }
    }
  }
  return $entity instanceof ContentEntityInterface ? $entity : NULL;
}