You are here

protected function FileUpload::validateAndLoadFieldDefinition in Drupal 8

Same name and namespace in other branches
  1. 9 core/modules/jsonapi/src/Controller/FileUpload.php \Drupal\jsonapi\Controller\FileUpload::validateAndLoadFieldDefinition()
  2. 10 core/modules/jsonapi/src/Controller/FileUpload.php \Drupal\jsonapi\Controller\FileUpload::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\NotFoundHttpException 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.

2 calls to FileUpload::validateAndLoadFieldDefinition()
FileUpload::handleFileUploadForExistingResource in core/modules/jsonapi/src/Controller/FileUpload.php
Handles JSON:API file upload requests.
FileUpload::handleFileUploadForNewResource in core/modules/jsonapi/src/Controller/FileUpload.php
Handles JSON:API file upload requests.

File

core/modules/jsonapi/src/Controller/FileUpload.php, line 235

Class

FileUpload
Handles file upload requests.

Namespace

Drupal\jsonapi\Controller

Code

protected function validateAndLoadFieldDefinition($entity_type_id, $bundle, $field_name) {
  $field_definitions = $this->fieldManager
    ->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 AccessDeniedException(sprintf('"%s" is not a file field', $field_name));
  }
  return $field_definition;
}