function FileUploadForm::createMediaFromValue
Creates a new, unsaved media item from a source field value.
Parameters
\Drupal\media\MediaTypeInterface $media_type: The media type of the media item.
\Drupal\Core\Entity\EntityStorageInterface $media_storage: The media storage.
string $source_field_name: The name of the media type's source field.
mixed $source_field_value: The value for the source field of the media item.
Return value
\Drupal\media\MediaInterface An unsaved media entity.
Overrides AddFormBase::createMediaFromValue
File
- 
              core/
modules/ media_library/ src/ Form/ FileUploadForm.php, line 324  
Class
- FileUploadForm
 - Creates a form to create media entities from uploaded files.
 
Namespace
Drupal\media_library\FormCode
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.