class EntityCreateAccessCheck

Same name and namespace in other branches
  1. 9 core/lib/Drupal/Core/Entity/EntityCreateAccessCheck.php \Drupal\Core\Entity\EntityCreateAccessCheck
  2. 10 core/lib/Drupal/Core/Entity/EntityCreateAccessCheck.php \Drupal\Core\Entity\EntityCreateAccessCheck
  3. 11.x core/lib/Drupal/Core/Entity/EntityCreateAccessCheck.php \Drupal\Core\Entity\EntityCreateAccessCheck

Defines an access checker for entity creation.

Hierarchy

Expanded class hierarchy of EntityCreateAccessCheck

1 file declares its use of EntityCreateAccessCheck
EntityCreateAccessCheckTest.php in core/tests/Drupal/Tests/Core/Entity/EntityCreateAccessCheckTest.php
1 string reference to 'EntityCreateAccessCheck'
core.services.yml in core/core.services.yml
core/core.services.yml
1 service uses EntityCreateAccessCheck
access_check.entity_create in core/core.services.yml
Drupal\Core\Entity\EntityCreateAccessCheck

File

core/lib/Drupal/Core/Entity/EntityCreateAccessCheck.php, line 15

Namespace

Drupal\Core\Entity
View source
class EntityCreateAccessCheck implements AccessInterface {
    use DeprecatedServicePropertyTrait;
    
    /**
     * {@inheritdoc}
     */
    protected $deprecatedProperties = [
        'entityManager' => 'entity.manager',
    ];
    
    /**
     * The entity type manager service.
     *
     * @var \Drupal\Core\Entity\EntityTypeManagerInterface
     */
    protected $entityTypeManager;
    
    /**
     * The key used by the routing requirement.
     *
     * @var string
     */
    protected $requirementsKey = '_entity_create_access';
    
    /**
     * Constructs a EntityCreateAccessCheck object.
     *
     * @param \Drupal\Core\Entity\EntityTypeManagerInterface $entity_type_manager
     *   The entity type manager service.
     */
    public function __construct(EntityTypeManagerInterface $entity_type_manager) {
        if ($entity_type_manager instanceof EntityManagerInterface) {
            @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);
            $this->entityTypeManager = \Drupal::entityTypeManager();
        }
        else {
            $this->entityTypeManager = $entity_type_manager;
        }
        $this->entityTypeManager = $entity_type_manager;
    }
    
    /**
     * Checks access to create the entity type and bundle for the given route.
     *
     * @param \Symfony\Component\Routing\Route $route
     *   The route to check against.
     * @param \Drupal\Core\Routing\RouteMatchInterface $route_match
     *   The parametrized route.
     * @param \Drupal\Core\Session\AccountInterface $account
     *   The currently logged in account.
     *
     * @return \Drupal\Core\Access\AccessResultInterface
     *   The access result.
     */
    public function access(Route $route, RouteMatchInterface $route_match, AccountInterface $account) {
        list($entity_type, $bundle) = explode(':', $route->getRequirement($this->requirementsKey) . ':');
        // The bundle argument can contain request argument placeholders like
        // {name}, loop over the raw variables and attempt to replace them in the
        // bundle name. If a placeholder does not exist, it won't get replaced.
        if ($bundle && strpos($bundle, '{') !== FALSE) {
            foreach ($route_match->getRawParameters()
                ->all() as $name => $value) {
                $bundle = str_replace('{' . $name . '}', $value, $bundle);
            }
            // If we were unable to replace all placeholders, deny access.
            if (strpos($bundle, '{') !== FALSE) {
                return AccessResult::neutral(sprintf("Could not find '%s' request argument, therefore cannot check create access.", $bundle));
            }
        }
        return $this->entityTypeManager
            ->getAccessControlHandler($entity_type)
            ->createAccess($bundle, $account, [], TRUE);
    }

}

Members

Title Sort descending Modifiers Object type Summary
DeprecatedServicePropertyTrait::__get public function Allows to access deprecated/removed properties.
EntityCreateAccessCheck::$deprecatedProperties protected property
EntityCreateAccessCheck::$entityTypeManager protected property The entity type manager service.
EntityCreateAccessCheck::$requirementsKey protected property The key used by the routing requirement.
EntityCreateAccessCheck::access public function Checks access to create the entity type and bundle for the given route.
EntityCreateAccessCheck::__construct public function Constructs a EntityCreateAccessCheck object.

Buggy or inaccurate documentation? Please file an issue. Need support? Need help programming? Connect with the Drupal community.