public function MediaTypePersistenceManager::save in Gutenberg 8.2
Same name and namespace in other branches
- 8 src/Persistence/MediaTypePersistenceManager.php \Drupal\gutenberg\Persistence\MediaTypePersistenceManager::save()
Perform saving process of an uploaded file as media entity.
Parameters
string $media_type: Media type machine name.
\Drupal\file\Entity\File $file_entity: File entity instance.
Return value
\Drupal\media\Entity\Media|null Created media type or null if failed.
Throws
\Exception
Overrides MediaTypePersistenceManagerInterface::save
File
- src/
Persistence/ MediaTypePersistenceManager.php, line 47
Class
- MediaTypePersistenceManager
- Provides management for all media types.
Namespace
Drupal\gutenberg\PersistenceCode
public function save(string $media_type, File $file_entity) {
$media_type_entity = $this->entityTypeManager
->getStorage('media_type')
->load($media_type);
$source = $media_type_entity
->getSource();
if (!$source) {
return NULL;
}
$field_config = $source
->getSourceFieldDefinition($media_type_entity);
$field_name = $field_config
->getName();
$media_entity = $this->entityTypeManager
->getStorage('media')
->create([
'bundle' => $media_type_entity
->id(),
'name' => $file_entity
->getFilename(),
'uid' => $this->account
->id(),
$field_name => [
'target_id' => $file_entity
->id(),
],
]);
try {
$media_entity
->save();
} catch (EntityStorageException $exception) {
return NULL;
}
return $media_entity;
}