function MediaTypeCreationTrait::createMediaType

Same name and namespace in other branches
  1. 8.9.x core/modules/media/tests/src/Traits/MediaTypeCreationTrait.php \Drupal\Tests\media\Traits\MediaTypeCreationTrait::createMediaType()
  2. 10 core/modules/media/tests/src/Traits/MediaTypeCreationTrait.php \Drupal\Tests\media\Traits\MediaTypeCreationTrait::createMediaType()
  3. 11.x core/modules/media/tests/src/Traits/MediaTypeCreationTrait.php \Drupal\Tests\media\Traits\MediaTypeCreationTrait::createMediaType()

Create a media type for a source plugin.

Parameters

string $source_plugin_id: The media source plugin ID.

mixed[] $values: (optional) Additional values for the media type entity:

  • id: The ID of the media type. If none is provided, a random value will be used.
  • label: The human-readable label of the media type. If none is provided, a random value will be used.

See \Drupal\media\MediaTypeInterface and \Drupal\media\Entity\MediaType for full documentation of the media type properties.

Return value

\Drupal\media\MediaTypeInterface A media type.

See also

\Drupal\media\MediaTypeInterface

\Drupal\media\Entity\MediaType

53 calls to MediaTypeCreationTrait::createMediaType()
ContentEntityTest::testMediaSource in core/modules/migrate_drupal/tests/src/Kernel/Plugin/migrate/source/ContentEntityTest.php
Tests media source plugin.
ContentModerationTest::setUp in core/modules/media_library/tests/src/FunctionalJavascript/ContentModerationTest.php
EntityQueryAccessTest::testMediaEntityQueryAccess in core/modules/views/tests/src/Functional/Entity/EntityQueryAccessTest.php
Tests that the 'media_access' query tag is respected by Views.
EntityReferenceSelectionAccessTest::testMediaHandler in core/modules/system/tests/src/Functional/Entity/EntityReferenceSelection/EntityReferenceSelectionAccessTest.php
Tests the selection handler for the media entity type.
JSTranslationTest::setUp in core/modules/ckeditor5/tests/src/FunctionalJavascript/JSTranslationTest.php

... See full list

File

core/modules/media/tests/src/Traits/MediaTypeCreationTrait.php, line 34

Class

MediaTypeCreationTrait
Provides methods to create a media type from given values.

Namespace

Drupal\Tests\media\Traits

Code

protected function createMediaType($source_plugin_id, array $values = []) {
    $values += [
        'id' => $this->randomMachineName(),
        'label' => $this->randomString(),
        'source' => $source_plugin_id,
    ];
    
    /** @var \Drupal\media\MediaTypeInterface $media_type */
    $media_type = MediaType::create($values);
    $source = $media_type->getSource();
    $source_field = $source->createSourceField($media_type);
    $source_configuration = $source->getConfiguration();
    $source_configuration['source_field'] = $source_field->getName();
    $source->setConfiguration($source_configuration);
    $this->assertSame(SAVED_NEW, $media_type->save());
    // The media type form creates a source field if it does not exist yet. The
    // same must be done in a kernel test, since it does not use that form.
    // @see \Drupal\media\MediaTypeForm::save()
    $source_field->getFieldStorageDefinition()
        ->save();
    // The source field storage has been created, now the field can be saved.
    $source_field->save();
    // Add the source field to the form display for the media type.
    $form_display = \Drupal::service('entity_display.repository')->getFormDisplay('media', $media_type->id(), 'default');
    $source->prepareFormDisplay($media_type, $form_display);
    $form_display->save();
    return $media_type;
}

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