You are here

protected function Upload::prepareEntities in Media entity audio 8

Same name and namespace in other branches
  1. 8.2 src/Plugin/EntityBrowser/Widget/Upload.php \Drupal\media_entity_audio\Plugin\EntityBrowser\Widget\Upload::prepareEntities()
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 audios.

Namespace

Drupal\media_entity_audio\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']);
  $audios = [];
  foreach ($files as $file) {

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