function EntityCreateAnyAccessCheck::access

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

Checks access to create an entity of any bundle for the given route.

Parameters

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

\Drupal\Core\Routing\RouteMatchInterface $route_match: The parameterized 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/EntityCreateAnyAccessCheck.php, line 63

Class

EntityCreateAnyAccessCheck
Defines an access checker for creating an entity of any bundle.

Namespace

Drupal\Core\Entity

Code

public function access(Route $route, RouteMatchInterface $route_match, AccountInterface $account) {
    $entity_type_id = $route->getRequirement($this->requirementsKey);
    $entity_type = $this->entityTypeManager
        ->getDefinition($entity_type_id);
    $access_control_handler = $this->entityTypeManager
        ->getAccessControlHandler($entity_type_id);
    // In case there is no "bundle" entity key, check create access with no
    // bundle specified.
    if (!$entity_type->hasKey('bundle')) {
        return $access_control_handler->createAccess(NULL, $account, [], TRUE);
    }
    $access = AccessResult::neutral();
    $bundles = array_keys($this->entityTypeBundleInfo
        ->getBundleInfo($entity_type_id));
    // Include list cache tag as access might change if more bundles are added.
    if ($entity_type->getBundleEntityType()) {
        $access->addCacheTags($this->entityTypeManager
            ->getDefinition($entity_type->getBundleEntityType())
            ->getListCacheTags());
        if (empty($route->getOption('_ignore_create_bundle_access'))) {
            // Check if the user is allowed to create new bundles. If so, allow
            // access, so the add page can show a link to create one.
            // @see \Drupal\Core\Entity\Controller\EntityController::addPage()
            $bundle_access_control_handler = $this->entityTypeManager
                ->getAccessControlHandler($entity_type->getBundleEntityType());
            $access = $access->orIf($bundle_access_control_handler->createAccess(NULL, $account, [], TRUE));
            if ($access->isAllowed()) {
                return $access;
            }
        }
    }
    // Check whether an entity of any bundle may be created.
    foreach ($bundles as $bundle) {
        $access = $access->orIf($access_control_handler->createAccess($bundle, $account, [], TRUE));
        // In case there is a least one bundle user can create entities for,
        // access is allowed.
        if ($access->isAllowed()) {
            break;
        }
    }
    return $access;
}

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