You are here

protected function Upload::prepareEntities in Media entity document 8

1 call to Upload::prepareEntities()
Upload::submit in src/Plugin/EntityBrowser/Widget/Upload.php

File

src/Plugin/EntityBrowser/Widget/Upload.php, line 53

Class

Upload
Uses upload to create media entity documents.

Namespace

Drupal\media_entity_document\Plugin\EntityBrowser\Widget

Code

protected function prepareEntities(array $form, FormStateInterface $form_state) {
  $files = parent::prepareEntities($form, $form_state);

  /** @var \Drupal\media_entity\MediaBundleInterface $bundle */
  $bundle = $this->entityTypeManager
    ->getStorage('media_bundle')
    ->load($this->configuration['media_bundle']);
  $documents = [];
  foreach ($files as $file) {

    /** @var \Drupal\media_entity\MediaInterface $document */
    $document = $this->entityTypeManager
      ->getStorage('media')
      ->create([
      'bundle' => $bundle
        ->id(),
      $bundle
        ->getTypeConfiguration()['source_field'] => $file,
    ]);
    $documents[] = $document;
  }
  return $documents;
}