3 namespace Drupal\Core\Entity;
5 use Drupal\Core\Access\AccessResult;
6 use Drupal\Core\Routing\Access\AccessInterface;
7 use Drupal\Core\Routing\RouteMatchInterface;
8 use Drupal\Core\Session\AccountInterface;
9 use Symfony\Component\Routing\Route;
12 * Defines an access checker for creating an entity of any bundle.
14 class EntityCreateAnyAccessCheck implements AccessInterface {
17 * The entity type manager.
19 * @var \Drupal\Core\Entity\EntityTypeManagerInterface
21 protected $entityTypeManager;
24 * The entity type bundle info.
26 * @var \Drupal\Core\Entity\EntityTypeBundleInfoInterface
28 protected $entityTypeBundleInfo;
31 * The key used by the routing requirement.
35 protected $requirementsKey = '_entity_create_any_access';
38 * Constructs a EntityCreateAnyAccessCheck object.
40 * @param \Drupal\Core\Entity\EntityTypeManagerInterface $entity_type_manager
41 * The entity type manager.
42 * @param \Drupal\Core\Entity\EntityTypeBundleInfoInterface $entity_type_bundle_info
43 * The entity type bundle info.
45 public function __construct(EntityTypeManagerInterface $entity_type_manager, EntityTypeBundleInfoInterface $entity_type_bundle_info) {
46 $this->entityTypeManager = $entity_type_manager;
47 $this->entityTypeBundleInfo = $entity_type_bundle_info;
51 * Checks access to create an entity of any bundle for the given route.
53 * @param \Symfony\Component\Routing\Route $route
54 * The route to check against.
55 * @param \Drupal\Core\Routing\RouteMatchInterface $route_match
56 * The parameterized route.
57 * @param \Drupal\Core\Session\AccountInterface $account
58 * The currently logged in account.
60 * @return \Drupal\Core\Access\AccessResultInterface
63 public function access(Route $route, RouteMatchInterface $route_match, AccountInterface $account) {
64 $entity_type_id = $route->getRequirement($this->requirementsKey);
65 $entity_type = $this->entityTypeManager->getDefinition($entity_type_id);
66 $access_control_handler = $this->entityTypeManager->getAccessControlHandler($entity_type_id);
68 // In case there is no "bundle" entity key, check create access with no
70 if (!$entity_type->hasKey('bundle')) {
71 return $access_control_handler->createAccess(NULL, $account, [], TRUE);
74 $access = AccessResult::neutral();
75 $bundles = array_keys($this->entityTypeBundleInfo->getBundleInfo($entity_type_id));
77 // Include list cache tag as access might change if more bundles are added.
78 if ($entity_type->getBundleEntityType()) {
79 $access->addCacheTags($this->entityTypeManager->getDefinition($entity_type->getBundleEntityType())->getListCacheTags());
81 // Check if the user is allowed to create new bundles. If so, allow
82 // access, so the add page can show a link to create one.
83 // @see \Drupal\Core\Entity\Controller\EntityController::addPage()
84 $bundle_access_control_handler = $this->entityTypeManager->getAccessControlHandler($entity_type->getBundleEntityType());
85 $access = $access->orIf($bundle_access_control_handler->createAccess(NULL, $account, [], TRUE));
86 if ($access->isAllowed()) {
91 // Check whether an entity of any bundle may be created.
92 foreach ($bundles as $bundle) {
93 $access = $access->orIf($access_control_handler->createAccess($bundle, $account, [], TRUE));
94 // In case there is a least one bundle user can create entities for,
96 if ($access->isAllowed()) {