You are here

protected function MediaLibraryTestBase::createMediaItems in Drupal 10

Same name and namespace in other branches
  1. 8 core/modules/media_library/tests/src/FunctionalJavascript/MediaLibraryTestBase.php \Drupal\Tests\media_library\FunctionalJavascript\MediaLibraryTestBase::createMediaItems()
  2. 9 core/modules/media_library/tests/src/FunctionalJavascript/MediaLibraryTestBase.php \Drupal\Tests\media_library\FunctionalJavascript\MediaLibraryTestBase::createMediaItems()

Create media items.

Parameters

array $media_items: A nested array of media item names keyed by media type.

Return value

\Drupal\media\MediaInterface[] An array of media entities keyed by the names passed in.

File

core/modules/media_library/tests/src/FunctionalJavascript/MediaLibraryTestBase.php, line 32

Class

MediaLibraryTestBase
Base class for functional tests of Media Library functionality.

Namespace

Drupal\Tests\media_library\FunctionalJavascript

Code

protected function createMediaItems(array $media_items) {
  $created_items = [];
  $time = time();
  foreach ($media_items as $type => $names) {
    foreach ($names as $name) {

      /** @var \Drupal\media\MediaInterface $media */
      $media = Media::create([
        'name' => $name,
        'bundle' => $type,
      ]);
      $source_field = $media
        ->getSource()
        ->getSourceFieldDefinition($media->bundle->entity)
        ->getName();
      $media
        ->set($source_field, $name)
        ->setCreatedTime(++$time)
        ->save();
      $created_items[$name] = $media;
    }
  }
  return $created_items;
}