Same filename and directory in other branches
- 8.9.x core/lib/Drupal/Core/Entity/EntityCreateAnyAccessCheck.php
- 9 core/lib/Drupal/Core/Entity/EntityCreateAnyAccessCheck.php
Namespace
Drupal\Core\Entity
File
core/lib/Drupal/Core/Entity/EntityCreateAnyAccessCheck.php
View source
<?php
namespace Drupal\Core\Entity;
use Drupal\Core\Access\AccessResult;
use Drupal\Core\Access\AccessResultReasonInterface;
use Drupal\Core\Routing\Access\AccessInterface;
use Drupal\Core\Routing\RouteMatchInterface;
use Drupal\Core\Session\AccountInterface;
use Symfony\Component\Routing\Route;
class EntityCreateAnyAccessCheck implements AccessInterface {
protected $entityTypeManager;
protected $entityTypeBundleInfo;
protected $requirementsKey = '_entity_create_any_access';
public function __construct(EntityTypeManagerInterface $entity_type_manager, EntityTypeBundleInfoInterface $entity_type_bundle_info) {
$this->entityTypeManager = $entity_type_manager;
$this->entityTypeBundleInfo = $entity_type_bundle_info;
}
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);
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));
if ($entity_type
->getBundleEntityType()) {
$access
->addCacheTags($this->entityTypeManager
->getDefinition($entity_type
->getBundleEntityType())
->getListCacheTags());
if (empty($route
->getOption('_ignore_create_bundle_access'))) {
$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;
}
}
}
foreach ($bundles as $bundle) {
$bundle_access = $access_control_handler
->createAccess($bundle, $account, [], TRUE);
$access
->inheritCacheability($bundle_access);
if ($bundle_access instanceof AccessResultReasonInterface && $bundle_access
->getReason() !== "" && $access
->getReason() === "") {
$access
->setReason($bundle_access
->getReason());
}
if ($bundle_access
->isAllowed()) {
return AccessResult::allowed()
->inheritCacheability($access);
}
}
return $access;
}
}
Classes