function FieldReuseAccessCheck::access

Same name in other branches
  1. 11.x core/modules/field_ui/src/Access/FieldReuseAccessCheck.php \Drupal\field_ui\Access\FieldReuseAccessCheck::access()

Checks access to the reuse existing fields form.

Parameters

\Symfony\Component\Routing\Route $route: The route to check against.

\Drupal\Core\Routing\RouteMatchInterface $route_match: The parametrized route.

\Drupal\Core\Session\AccountInterface $account: The currently logged in account.

string|null $bundle: (optional) The bundle. Different entity types can have different names for their bundle key, so if not specified on the route via a {bundle} parameter, the access checker determines the appropriate key name, and gets the value from the corresponding request attribute. For example, for nodes, the bundle key is "node_type", so the value would be available via the {node_type} parameter rather than a {bundle} parameter.

Return value

\Drupal\Core\Access\AccessResultInterface The access result.

File

core/modules/field_ui/src/Access/FieldReuseAccessCheck.php, line 57

Class

FieldReuseAccessCheck
Defines an access check for the reuse existing fields form.

Namespace

Drupal\field_ui\Access

Code

public function access(Route $route, RouteMatchInterface $route_match, AccountInterface $account, ?string $bundle = NULL) : AccessResultInterface {
    $access = AccessResult::neutral();
    if ($entity_type_id = $route->getDefault('entity_type_id')) {
        if (empty($bundle)) {
            $entity_type = $this->entityTypeManager
                ->getDefinition($entity_type_id);
            $bundle = $route_match->getRawParameter($entity_type->getBundleEntityType());
        }
        $field_types = $this->fieldTypePluginManager
            ->getDefinitions();
        // Allows access if there are any existing fields and the user
        // correct permissions.
        foreach ($this->entityFieldManager
            ->getFieldStorageDefinitions($entity_type_id) as $field_storage) {
            // Do not include fields with
            // - non-configurable field storages,
            // - locked field storages,
            // - field storages that should not be added via user interface,
            // - field storages that already have a field in the bundle.
            $field_type = $field_storage->getType();
            $access->addCacheableDependency($field_storage);
            if ($field_storage instanceof FieldStorageConfigInterface && !$field_storage->isLocked() && empty($field_types[$field_type]['no_ui']) && !in_array($bundle, $field_storage->getBundles(), TRUE)) {
                $permission = $route->getRequirement('_field_ui_field_reuse_access');
                $access = $access->orIf(AccessResult::allowedIfHasPermission($account, $permission));
            }
        }
        $access->addCacheableDependency($this->entityFieldManager);
    }
    return $access;
}

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