Skip to main content
Drupal API
User account menu
  • Log in

Breadcrumb

  1. Drupal Core 11.1.x
  2. FileUpload.php

function 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.

1 call to FileUpload::validateAndLoadFieldDefinition()
FileUpload::handleFileUploadForResource in core/modules/jsonapi/src/Controller/FileUpload.php
Handles JSON:API file upload requests.

File

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

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;
}

API Navigation

  • Drupal Core 11.1.x
  • Topics
  • Classes
  • Functions
  • Constants
  • Globals
  • Files
  • Namespaces
  • Deprecated
  • Services
RSS feed
Powered by Drupal