function MediaLibraryTestBase::createMediaItems

Same name in other branches
  1. 8.9.x core/modules/media_library/tests/src/FunctionalJavascript/MediaLibraryTestBase.php \Drupal\Tests\media_library\FunctionalJavascript\MediaLibraryTestBase::createMediaItems()
  2. 10 core/modules/media_library/tests/src/FunctionalJavascript/MediaLibraryTestBase.php \Drupal\Tests\media_library\FunctionalJavascript\MediaLibraryTestBase::createMediaItems()
  3. 11.x 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.

6 calls to MediaLibraryTestBase::createMediaItems()
EntityReferenceWidgetTest::setUp in core/modules/media_library/tests/src/FunctionalJavascript/EntityReferenceWidgetTest.php
MediaOverviewTest::setUp in core/modules/media_library/tests/src/FunctionalJavascript/MediaOverviewTest.php
ViewsUiIntegrationTest::setUp in core/modules/media_library/tests/src/FunctionalJavascript/ViewsUiIntegrationTest.php
WidgetAccessTest::testWidgetAccess in core/modules/media_library/tests/src/FunctionalJavascript/WidgetAccessTest.php
Tests that the widget access works as expected.
WidgetAnonymousTest::setUp in core/modules/media_library/tests/src/FunctionalJavascript/WidgetAnonymousTest.php

... See full list

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;
}

Buggy or inaccurate documentation? Please file an issue. Need support? Need help programming? Connect with the Drupal community.