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 entity creation.
14 class EntityCreateAccessCheck implements AccessInterface {
19 * @var \Drupal\Core\Entity\EntityManagerInterface
21 protected $entityManager;
24 * The key used by the routing requirement.
28 protected $requirementsKey = '_entity_create_access';
31 * Constructs a EntityCreateAccessCheck object.
33 * @param \Drupal\Core\Entity\EntityManagerInterface $entity_manager
36 public function __construct(EntityManagerInterface $entity_manager) {
37 $this->entityManager = $entity_manager;
41 * Checks access to create the entity type and bundle for the given route.
43 * @param \Symfony\Component\Routing\Route $route
44 * The route to check against.
45 * @param \Drupal\Core\Routing\RouteMatchInterface $route_match
46 * The parametrized route.
47 * @param \Drupal\Core\Session\AccountInterface $account
48 * The currently logged in account.
50 * @return \Drupal\Core\Access\AccessResultInterface
53 public function access(Route $route, RouteMatchInterface $route_match, AccountInterface $account) {
54 list($entity_type, $bundle) = explode(':', $route->getRequirement($this->requirementsKey) . ':');
56 // The bundle argument can contain request argument placeholders like
57 // {name}, loop over the raw variables and attempt to replace them in the
58 // bundle name. If a placeholder does not exist, it won't get replaced.
59 if ($bundle && strpos($bundle, '{') !== FALSE) {
60 foreach ($route_match->getRawParameters()->all() as $name => $value) {
61 $bundle = str_replace('{' . $name . '}', $value, $bundle);
63 // If we were unable to replace all placeholders, deny access.
64 if (strpos($bundle, '{') !== FALSE) {
65 return AccessResult::neutral();
68 return $this->entityManager->getAccessControlHandler($entity_type)->createAccess($bundle, $account, [], TRUE);