public function MediaUploader::upload in Gutenberg 8.2
Same name and namespace in other branches
- 8 src/MediaUploader.php \Drupal\gutenberg\MediaUploader::upload()
Upload media to the filesystem.
Parameters
string $form_field_name: A string that is the associative array key of the upload form element in the form array.
\Symfony\Component\HttpFoundation\File\UploadedFile $uploaded_file: Uploaded file instance.
\Drupal\editor\Entity\Editor $editor: Editor entity.
array $file_settings: A list of file configurations. e.g. 'file_extensions' etc.
Return value
\Drupal\file\Entity\File|null File entity or null on failure.
Overrides MediaUploaderInterface::upload
File
- src/
MediaUploader.php, line 38
Class
- MediaUploader
- Upload files from Gutenberg editor upload method.
Namespace
Drupal\gutenbergCode
public function upload(string $form_field_name, UploadedFile $uploaded_file, Editor $editor, array $file_settings = []) {
$image_settings = $editor
->getImageUploadSettings();
// @todo use the relevant media file settings over the Editor's "Image" plugin one?
// Or ultimately just use Drupal's builtin file_managed in a modal to
// handle all of this logic like CKeditor does.
$destination = $image_settings['scheme'] . '://' . $image_settings['directory'];
if (!$this->fileSystem
->prepareDirectory($destination, FileSystemInterface::CREATE_DIRECTORY)) {
return NULL;
}
$validators = [];
if (explode('/', $uploaded_file
->getClientMimeType())[0] === 'image') {
// Validate file type.
// Image type.
if (!empty($image_settings['max_dimensions']['width']) || !empty($image_settings['max_dimensions']['height'])) {
$max_dimensions = $image_settings['max_dimensions']['width'] . 'x' . $image_settings['max_dimensions']['height'];
}
else {
$max_dimensions = 0;
}
if (version_compare(\Drupal::VERSION, '9.1', '<')) {
// @see https://www.drupal.org/node/3162663
$max_filesize = min(Bytes::toInt($image_settings['max_size']), Environment::getUploadMaxSize());
}
else {
$max_filesize = min(Bytes::toNumber($image_settings['max_size']), Environment::getUploadMaxSize());
}
$validators['file_validate_size'] = [
$max_filesize,
];
$validators['file_validate_image_resolution'] = [
$max_dimensions,
];
}
if (!empty($file_settings['file_extensions'])) {
// Validate the media file extensions.
$validators['file_validate_extensions'] = [
$file_settings['file_extensions'],
];
}
// Upload a temporary file.
$result = file_save_upload($form_field_name, $validators, $destination);
if (is_array($result) && $result[0]) {
/** @var \Drupal\file\Entity\File $file */
$file = $result[0];
return $file;
}
return NULL;
}