Chris@0: 'entity.manager']; Chris@18: Chris@18: /** Chris@18: * The entity type manager service. Chris@0: * Chris@18: * @var \Drupal\Core\Entity\EntityTypeManagerInterface Chris@0: */ Chris@18: protected $entityTypeManager; Chris@0: Chris@0: /** Chris@0: * The key used by the routing requirement. Chris@0: * Chris@0: * @var string Chris@0: */ Chris@0: protected $requirementsKey = '_entity_create_access'; Chris@0: Chris@0: /** Chris@0: * Constructs a EntityCreateAccessCheck object. Chris@0: * Chris@18: * @param \Drupal\Core\Entity\EntityTypeManagerInterface $entity_type_manager Chris@18: * The entity type manager service. Chris@0: */ Chris@18: public function __construct(EntityTypeManagerInterface $entity_type_manager) { Chris@18: if ($entity_type_manager instanceof EntityManagerInterface) { Chris@18: @trigger_error('Passing the entity.manager service to EntityCreateAccessCheck::__construct() is deprecated in Drupal 8.7.0 and will be removed before Drupal 9.0.0. Pass the new dependencies instead. See https://www.drupal.org/node/2549139.', E_USER_DEPRECATED); Chris@18: $this->entityTypeManager = \Drupal::entityTypeManager(); Chris@18: } Chris@18: else { Chris@18: $this->entityTypeManager = $entity_type_manager; Chris@18: } Chris@18: $this->entityTypeManager = $entity_type_manager; Chris@0: } Chris@0: Chris@0: /** Chris@0: * Checks access to create the entity type and 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 parametrized 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: list($entity_type, $bundle) = explode(':', $route->getRequirement($this->requirementsKey) . ':'); Chris@0: Chris@0: // The bundle argument can contain request argument placeholders like Chris@0: // {name}, loop over the raw variables and attempt to replace them in the Chris@0: // bundle name. If a placeholder does not exist, it won't get replaced. Chris@0: if ($bundle && strpos($bundle, '{') !== FALSE) { Chris@0: foreach ($route_match->getRawParameters()->all() as $name => $value) { Chris@0: $bundle = str_replace('{' . $name . '}', $value, $bundle); Chris@0: } Chris@0: // If we were unable to replace all placeholders, deny access. Chris@0: if (strpos($bundle, '{') !== FALSE) { Chris@17: return AccessResult::neutral(sprintf("Could not find '%s' request argument, therefore cannot check create access.", $bundle)); Chris@0: } Chris@0: } Chris@18: return $this->entityTypeManager->getAccessControlHandler($entity_type)->createAccess($bundle, $account, [], TRUE); Chris@0: } Chris@0: Chris@0: }