You are here

protected function FileUploadResource::validateAndLoadFieldDefinition in Drupal 10

Same name and namespace in other branches
  1. 8 core/modules/file/src/Plugin/rest/resource/FileUploadResource.php \Drupal\file\Plugin\rest\resource\FileUploadResource::validateAndLoadFieldDefinition()
  2. 9 core/modules/file/src/Plugin/rest/resource/FileUploadResource.php \Drupal\file\Plugin\rest\resource\FileUploadResource::validateAndLoadFieldDefinition()

Validates and loads a field definition instance.

Parameters

string $entity_type_id: The entity type ID the field is attached to.

string $bundle: The bundle the field is attached to.

string $field_name: The field name.

Return value

\Drupal\Core\Field\FieldDefinitionInterface The field definition.

Throws

\Symfony\Component\HttpKernel\Exception\BadRequestHttpException Thrown when the field does not exist.

\Symfony\Component\HttpFoundation\File\Exception\AccessDeniedException Thrown when the target type of the field is not a file, or the current user does not have 'edit' access for the field.

1 call to FileUploadResource::validateAndLoadFieldDefinition()
FileUploadResource::post in core/modules/file/src/Plugin/rest/resource/FileUploadResource.php
Creates a file from an endpoint.

File

core/modules/file/src/Plugin/rest/resource/FileUploadResource.php, line 431

Class

FileUploadResource
File upload resource.

Namespace

Drupal\file\Plugin\rest\resource

Code

protected function validateAndLoadFieldDefinition($entity_type_id, $bundle, $field_name) {
  $field_definitions = $this->entityFieldManager
    ->getFieldDefinitions($entity_type_id, $bundle);
  if (!isset($field_definitions[$field_name])) {
    throw new NotFoundHttpException(sprintf('Field "%s" does not exist', $field_name));
  }

  /** @var \Drupal\Core\Field\FieldDefinitionInterface $field_definition */
  $field_definition = $field_definitions[$field_name];
  if ($field_definition
    ->getSetting('target_type') !== 'file') {
    throw new AccessDeniedHttpException(sprintf('"%s" is not a file field', $field_name));
  }
  $entity_access_control_handler = $this->entityTypeManager
    ->getAccessControlHandler($entity_type_id);
  $bundle = $this->entityTypeManager
    ->getDefinition($entity_type_id)
    ->hasKey('bundle') ? $bundle : NULL;
  $access_result = $entity_access_control_handler
    ->createAccess($bundle, NULL, [], TRUE)
    ->andIf($entity_access_control_handler
    ->fieldAccess('edit', $field_definition, NULL, NULL, TRUE));
  if (!$access_result
    ->isAllowed()) {
    throw new AccessDeniedHttpException($access_result
      ->getReason());
  }
  return $field_definition;
}