public function MediaTypePersistenceManager::save in Gutenberg 8
Same name and namespace in other branches
- 8.2 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);
$field_config = $media_type_entity
->getSource()
->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;
}