EntityBundleAccessCheck.php 1.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051
  1. <?php
  2. namespace Drupal\Core\Entity;
  3. use Drupal\Core\Access\AccessResult;
  4. use Drupal\Core\Routing\Access\AccessInterface;
  5. use Drupal\Core\Routing\RouteMatchInterface;
  6. use Drupal\Core\Session\AccountInterface;
  7. use Symfony\Component\Routing\Route;
  8. /**
  9. * Provides an entity bundle checker for the _entity_bundles route requirement.
  10. */
  11. class EntityBundleAccessCheck implements AccessInterface {
  12. /**
  13. * Checks entity bundle match based on the _entity_bundles route requirement.
  14. *
  15. * @code
  16. * example.route:
  17. * path: foo/{example_entity_type}/{other_parameter}
  18. * requirements:
  19. * _entity_bundles: 'example_entity_type:example_bundle|other_example_bundle'
  20. * @endcode
  21. *
  22. * @param \Symfony\Component\Routing\Route $route
  23. * The route to check against.
  24. * @param \Drupal\Core\Routing\RouteMatchInterface $route_match
  25. * The parametrized route.
  26. * @param \Drupal\Core\Session\AccountInterface $account
  27. * The currently logged in account.
  28. *
  29. * @return \Drupal\Core\Access\AccessResultInterface
  30. * The access result.
  31. */
  32. public function access(Route $route, RouteMatchInterface $route_match, AccountInterface $account) {
  33. if ($route->hasRequirement('_entity_bundles')) {
  34. list($entity_type, $bundle_definition) = explode(':', $route->getRequirement('_entity_bundles'));
  35. $bundles = explode('|', $bundle_definition);
  36. $parameters = $route_match->getParameters();
  37. if ($parameters->has($entity_type)) {
  38. $entity = $parameters->get($entity_type);
  39. if ($entity instanceof EntityInterface && in_array($entity->bundle(), $bundles, TRUE)) {
  40. return AccessResult::allowed()->addCacheableDependency($entity);
  41. }
  42. }
  43. }
  44. return AccessResult::neutral('The entity bundle does not match the route _entity_bundles requirement.');
  45. }
  46. }