function QuickEditImageController::upload

Same name in this branch
  1. 9 core/modules/image/src/Controller/QuickEditImageController.php \Drupal\image\Controller\QuickEditImageController::upload()
Same name in other branches
  1. 8.9.x core/modules/image/src/Controller/QuickEditImageController.php \Drupal\image\Controller\QuickEditImageController::upload()

Returns JSON representing the new file upload, or validation errors.

Parameters

\Drupal\Core\Entity\EntityInterface $entity: The entity of which an image field is being rendered.

string $field_name: The name of the (image) field that is being rendered

string $langcode: The language code of the field that is being rendered.

string $view_mode_id: The view mode of the field that is being rendered.

Return value

\Symfony\Component\HttpFoundation\JsonResponse The JSON response.

1 string reference to 'QuickEditImageController::upload'
quickedit.routing.yml in core/modules/quickedit/quickedit.routing.yml
core/modules/quickedit/quickedit.routing.yml

File

core/modules/quickedit/src/Controller/QuickEditImageController.php, line 110

Class

QuickEditImageController
Returns responses for our image routes.

Namespace

Drupal\quickedit\Controller

Code

public function upload(EntityInterface $entity, $field_name, $langcode, $view_mode_id) {
    $field = $this->getField($entity, $field_name, $langcode);
    $field_validators = $field->getUploadValidators();
    $field_settings = $field->getFieldDefinition()
        ->getSettings();
    $destination = $field->getUploadLocation();
    // Add upload resolution validation.
    if ($field_settings['max_resolution'] || $field_settings['min_resolution']) {
        $field_validators['file_validate_image_resolution'] = [
            $field_settings['max_resolution'],
            $field_settings['min_resolution'],
        ];
    }
    // Create the destination directory if it does not already exist.
    if (isset($destination) && !$this->fileSystem
        ->prepareDirectory($destination, FileSystemInterface::CREATE_DIRECTORY)) {
        return new JsonResponse([
            'main_error' => $this->t('The destination directory could not be created.'),
            'errors' => '',
        ]);
    }
    // Attempt to save the image given the field's constraints.
    $result = file_save_upload('image', $field_validators, $destination);
    if (is_array($result) && $result[0]) {
        
        /** @var \Drupal\file\Entity\File $file */
        $file = $result[0];
        $image = $this->imageFactory
            ->get($file->getFileUri());
        // Set the value in the Entity to the new file.
        
        /** @var \Drupal\file\Plugin\Field\FieldType\FileFieldItemList $field_list */
        $value = $entity->{$field_name}
            ->getValue();
        $value[0]['target_id'] = $file->id();
        $value[0]['width'] = $image->getWidth();
        $value[0]['height'] = $image->getHeight();
        $entity->{$field_name}
            ->setValue($value);
        // Render the new image using the correct formatter settings.
        $entity_view_mode_ids = array_keys($this->entityDisplayRepository
            ->getViewModes($entity->getEntityTypeId()));
        if (in_array($view_mode_id, $entity_view_mode_ids, TRUE)) {
            $output = $entity->{$field_name}
                ->view($view_mode_id);
        }
        else {
            // Each part of a custom (non-Entity Display) view mode ID is separated
            // by a dash; the first part must be the module name.
            $mode_id_parts = explode('-', $view_mode_id, 2);
            $module = reset($mode_id_parts);
            $args = [
                $entity,
                $field_name,
                $view_mode_id,
                $langcode,
            ];
            $output = $this->moduleHandler()
                ->invoke($module, 'quickedit_render_field', $args);
        }
        // Save the Entity to tempstore.
        $this->tempStore
            ->set($entity->uuid(), $entity);
        $data = [
            'fid' => $file->id(),
            'html' => $this->renderer
                ->renderRoot($output),
        ];
        return new JsonResponse($data);
    }
    else {
        // Return a JSON object containing the errors from Drupal and our
        // "main_error", which is displayed inside the dropzone area.
        $messages = StatusMessages::renderMessages('error');
        return new JsonResponse([
            'errors' => $this->renderer
                ->render($messages),
            'main_error' => $this->t('The image failed validation.'),
        ]);
    }
}

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