protected function MediaBulkUploadForm::processFile in Media Bulk Upload 8
Process a file upload.
Will create a file entity and prepare a media entity with data.
Parameters
\Drupal\media_bulk_upload\Entity\MediaBulkConfigInterface $mediaBulkConfig: Media Bulk Config.
array $file: File upload data.
Return value
\Drupal\media\MediaInterface The unsaved media entity that is created.
Throws
\Drupal\Component\Plugin\Exception\InvalidPluginDefinitionException
\Drupal\Component\Plugin\Exception\PluginNotFoundException
\Drupal\Core\Entity\EntityStorageException
\Exception
1 call to MediaBulkUploadForm::processFile()
- MediaBulkUploadForm::submitForm in src/
Form/ MediaBulkUploadForm.php - Submit handler to create the file entities and media entities.
File
- src/
Form/ MediaBulkUploadForm.php, line 358
Class
- MediaBulkUploadForm
- Class BulkMediaUploadForm.
Namespace
Drupal\media_bulk_upload\FormCode
protected function processFile(MediaBulkConfigInterface $mediaBulkConfig, array $file) {
$fileInfo = pathinfo($file['filename']);
$filename = $fileInfo['basename'];
if (!$this
->validateFilename($fileInfo)) {
$this
->messenger()
->addError($this
->t('File :filename does not have a valid extension or filename.', [
':filename' => $filename,
]));
throw new \Exception("File {$filename} does not have a valid extension or filename.");
}
$allowedMediaTypes = $this->mediaSubFormManager
->getMediaTypeManager()
->getBulkMediaTypes($mediaBulkConfig);
$matchingMediaTypes = $this->mediaSubFormManager
->getMediaTypeManager()
->getMediaTypeIdsByFileExtension($fileInfo['extension']);
$mediaTypes = array_intersect_key($matchingMediaTypes, $allowedMediaTypes);
$mediaType = reset($mediaTypes);
if (!$this
->validateFileSize($mediaType, $file['path'])) {
$fileSizeSetting = $this->mediaSubFormManager
->getMediaTypeManager()
->getTargetFieldMaxSize($mediaType);
$mediaTypeLabel = $mediaType
->label();
$this
->messenger()
->addError($this
->t('File :filename exceeds the maximum file size of :file_size for media type :media_type exceeded.', [
':filename' => $filename,
':file_size' => $fileSizeSetting,
':media_type' => $mediaTypeLabel,
]));
throw new \Exception("File {$filename} exceeds the maximum file size of {$fileSizeSetting} for media type {$mediaTypeLabel} exceeded.");
}
$uri_scheme = $this->mediaSubFormManager
->getTargetFieldDirectory($mediaType);
$destination = $uri_scheme . '/' . $file['filename'];
$file_default_scheme = \Drupal::config('system.file')
->get('default_scheme') . '://';
if ($uri_scheme === $file_default_scheme) {
$destination = $uri_scheme . $file['filename'];
}
/** @var \Drupal\file\FileInterface $fileEntity */
$fileEntity = $this->fileStorage
->create([
'uri' => $file['path'],
'uid' => $this->currentUser
->id(),
'status' => FILE_STATUS_PERMANENT,
]);
$fileEntity
->save();
file_move($fileEntity, $destination);
if (!$fileEntity) {
$this
->messenger()
->addError($this
->t('File :filename could not be created.', [
':filename' => $filename,
]), 'error');
throw new \Exception('File entity could not be created.');
}
$values = $this
->getNewMediaValues($mediaType, $fileInfo, $fileEntity);
/** @var \Drupal\media\MediaInterface $media */
$media = $this->mediaStorage
->create($values);
return $media;
}