class MediaTypePersistenceManager in Gutenberg 8
Same name and namespace in other branches
- 8.2 src/Persistence/MediaTypePersistenceManager.php \Drupal\gutenberg\Persistence\MediaTypePersistenceManager
Provides management for all media types.
@package Drupal\gutenberg\Persistence
Hierarchy
- class \Drupal\gutenberg\Persistence\MediaTypePersistenceManager implements MediaTypePersistenceManagerInterface
Expanded class hierarchy of MediaTypePersistenceManager
1 file declares its use of MediaTypePersistenceManager
- MediaService.php in src/
Service/ MediaService.php
1 string reference to 'MediaTypePersistenceManager'
1 service uses MediaTypePersistenceManager
File
- src/
Persistence/ MediaTypePersistenceManager.php, line 15
Namespace
Drupal\gutenberg\PersistenceView source
class MediaTypePersistenceManager implements MediaTypePersistenceManagerInterface {
/**
* The entity type manager.
*
* @var \Drupal\Core\Entity\EntityTypeManagerInterface
*/
protected $entityTypeManager;
/**
* The current user.
*
* @var \Drupal\Core\Session\AccountInterface
*/
protected $account;
/**
* MediaTypePersistenceManager constructor.
*
* @param \Drupal\Core\Entity\EntityTypeManagerInterface $entity_type_manager
* The entity type manager.
* @param \Drupal\Core\Session\AccountInterface $account
* The current user.
*/
public function __construct(EntityTypeManagerInterface $entity_type_manager, AccountInterface $account) {
$this->entityTypeManager = $entity_type_manager;
$this->account = $account;
}
/**
* {@inheritDoc}
*/
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;
}
}
Members
Name | Modifiers | Type | Description | Overrides |
---|---|---|---|---|
MediaTypePersistenceManager:: |
protected | property | The current user. | |
MediaTypePersistenceManager:: |
protected | property | The entity type manager. | |
MediaTypePersistenceManager:: |
public | function |
Perform saving process of an uploaded file as media entity. Overrides MediaTypePersistenceManagerInterface:: |
|
MediaTypePersistenceManager:: |
public | function | MediaTypePersistenceManager constructor. |