function EntityCreateAccessCheck::access

Same name in other branches
  1. 9 core/lib/Drupal/Core/Entity/EntityCreateAccessCheck.php \Drupal\Core\Entity\EntityCreateAccessCheck::access()
  2. 8.9.x core/lib/Drupal/Core/Entity/EntityCreateAccessCheck.php \Drupal\Core\Entity\EntityCreateAccessCheck::access()
  3. 11.x core/lib/Drupal/Core/Entity/EntityCreateAccessCheck.php \Drupal\Core\Entity\EntityCreateAccessCheck::access()

Checks access to create the entity type and bundle for the given route.

Parameters

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

\Drupal\Core\Routing\RouteMatchInterface $route_match: The parametrized route.

\Drupal\Core\Session\AccountInterface $account: The currently logged in account.

Return value

\Drupal\Core\Access\AccessResultInterface The access result.

File

core/lib/Drupal/Core/Entity/EntityCreateAccessCheck.php, line 53

Class

EntityCreateAccessCheck
Defines an access checker for entity creation.

Namespace

Drupal\Core\Entity

Code

public function access(Route $route, RouteMatchInterface $route_match, AccountInterface $account) {
    [
        $entity_type,
        $bundle,
    ] = explode(':', $route->getRequirement($this->requirementsKey) . ':');
    // The bundle argument can contain request argument placeholders like
    // {name}, loop over the raw variables and attempt to replace them in the
    // bundle name. If a placeholder does not exist, it won't get replaced.
    if ($bundle && str_contains($bundle, '{')) {
        foreach ($route_match->getRawParameters()
            ->all() as $name => $value) {
            $bundle = str_replace('{' . $name . '}', $value, $bundle);
        }
        // If we were unable to replace all placeholders, deny access.
        if (str_contains($bundle, '{')) {
            return AccessResult::neutral(sprintf("Could not find '%s' request argument, therefore cannot check create access.", $bundle));
        }
    }
    return $this->entityTypeManager
        ->getAccessControlHandler($entity_type)
        ->createAccess($bundle, $account, [], TRUE);
}

Buggy or inaccurate documentation? Please file an issue. Need support? Need help programming? Connect with the Drupal community.