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