3 namespace Drupal\field_ui\Access;
5 use Drupal\Core\Access\AccessResult;
6 use Drupal\Core\Entity\EntityManagerInterface;
7 use Drupal\Core\Routing\Access\AccessInterface;
8 use Drupal\Core\Routing\RouteMatchInterface;
9 use Drupal\Core\Session\AccountInterface;
10 use Symfony\Component\Routing\Route;
13 * Defines an access check for entity form mode routes.
15 * @see \Drupal\Core\Entity\Entity\EntityFormMode
17 class FormModeAccessCheck implements AccessInterface {
22 * @var \Drupal\Core\Entity\EntityManagerInterface
24 protected $entityManager;
27 * Creates a new FormModeAccessCheck.
29 * @param \Drupal\Core\Entity\EntityManagerInterface $entity_manager
32 public function __construct(EntityManagerInterface $entity_manager) {
33 $this->entityManager = $entity_manager;
37 * Checks access to the form mode.
39 * @param \Symfony\Component\Routing\Route $route
40 * The route to check against.
41 * @param \Drupal\Core\Routing\RouteMatchInterface $route_match
42 * The parametrized route.
43 * @param \Drupal\Core\Session\AccountInterface $account
44 * The currently logged in account.
45 * @param string $form_mode_name
46 * (optional) The form mode. Defaults to 'default'.
47 * @param string $bundle
48 * (optional) The bundle. Different entity types can have different names
49 * for their bundle key, so if not specified on the route via a {bundle}
50 * parameter, the access checker determines the appropriate key name, and
51 * gets the value from the corresponding request attribute. For example,
52 * for nodes, the bundle key is "node_type", so the value would be
53 * available via the {node_type} parameter rather than a {bundle}
56 * @return \Drupal\Core\Access\AccessResultInterface
59 public function access(Route $route, RouteMatchInterface $route_match, AccountInterface $account, $form_mode_name = 'default', $bundle = NULL) {
60 $access = AccessResult::neutral();
61 if ($entity_type_id = $route->getDefault('entity_type_id')) {
63 $entity_type = $this->entityManager->getDefinition($entity_type_id);
64 $bundle = $route_match->getRawParameter($entity_type->getBundleEntityType());
68 if ($form_mode_name == 'default') {
71 elseif ($entity_display = $this->entityManager->getStorage('entity_form_display')->load($entity_type_id . '.' . $bundle . '.' . $form_mode_name)) {
72 $visibility = $entity_display->status();
75 if ($form_mode_name != 'default' && $entity_display) {
76 $access->addCacheableDependency($entity_display);
80 $permission = $route->getRequirement('_field_ui_form_mode_access');
81 $access = $access->orIf(AccessResult::allowedIfHasPermission($account, $permission));