function FileUploadForm::createMediaFromValue

Same name in other branches
  1. 8.9.x core/modules/media_library/src/Form/FileUploadForm.php \Drupal\media_library\Form\FileUploadForm::createMediaFromValue()
  2. 10 core/modules/media_library/src/Form/FileUploadForm.php \Drupal\media_library\Form\FileUploadForm::createMediaFromValue()
  3. 11.x core/modules/media_library/src/Form/FileUploadForm.php \Drupal\media_library\Form\FileUploadForm::createMediaFromValue()

Overrides AddFormBase::createMediaFromValue

File

core/modules/media_library/src/Form/FileUploadForm.php, line 323

Class

FileUploadForm
Creates a form to create media entities from uploaded files.

Namespace

Drupal\media_library\Form

Code

protected function createMediaFromValue(MediaTypeInterface $media_type, EntityStorageInterface $media_storage, $source_field_name, $file) {
    if (!$file instanceof FileInterface) {
        throw new \InvalidArgumentException('Cannot create a media item without a file entity.');
    }
    // Create a file item to get the upload location.
    $item = $this->createFileItem($media_type);
    $upload_location = $item->getUploadLocation();
    if (!$this->fileSystem
        ->prepareDirectory($upload_location, FileSystemInterface::CREATE_DIRECTORY)) {
        throw new FileWriteException("The destination directory '{$upload_location}' is not writable");
    }
    $file = $this->fileRepository
        ->move($file, $upload_location);
    if (!$file) {
        throw new \RuntimeException("Unable to move file to '{$upload_location}'");
    }
    return parent::createMediaFromValue($media_type, $media_storage, $source_field_name, $file);
}

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