You are here

class MediaTypePersistenceManager in Gutenberg 8

Same name and namespace in other branches
  1. 8.2 src/Persistence/MediaTypePersistenceManager.php \Drupal\gutenberg\Persistence\MediaTypePersistenceManager

Provides management for all media types.

@package Drupal\gutenberg\Persistence

Hierarchy

Expanded class hierarchy of MediaTypePersistenceManager

1 file declares its use of MediaTypePersistenceManager
MediaService.php in src/Service/MediaService.php
1 string reference to 'MediaTypePersistenceManager'
gutenberg.services.yml in ./gutenberg.services.yml
gutenberg.services.yml
1 service uses MediaTypePersistenceManager
gutenberg.media_type.persistence_manager in ./gutenberg.services.yml
Drupal\gutenberg\Persistence\MediaTypePersistenceManager

File

src/Persistence/MediaTypePersistenceManager.php, line 15

Namespace

Drupal\gutenberg\Persistence
View 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

Namesort descending Modifiers Type Description Overrides
MediaTypePersistenceManager::$account protected property The current user.
MediaTypePersistenceManager::$entityTypeManager protected property The entity type manager.
MediaTypePersistenceManager::save public function Perform saving process of an uploaded file as media entity. Overrides MediaTypePersistenceManagerInterface::save
MediaTypePersistenceManager::__construct public function MediaTypePersistenceManager constructor.