function MediaSourceTestBase::createMediaTypeField

Same name in other branches
  1. 9 core/modules/media/tests/src/FunctionalJavascript/MediaSourceTestBase.php \Drupal\Tests\media\FunctionalJavascript\MediaSourceTestBase::createMediaTypeField()
  2. 10 core/modules/media/tests/src/FunctionalJavascript/MediaSourceTestBase.php \Drupal\Tests\media\FunctionalJavascript\MediaSourceTestBase::createMediaTypeField()
  3. 11.x core/modules/media/tests/src/FunctionalJavascript/MediaSourceTestBase.php \Drupal\Tests\media\FunctionalJavascript\MediaSourceTestBase::createMediaTypeField()

Creates storage and field instance, attached to a given media type.

Parameters

string $field_name: The field name.

string $field_type: The field type.

string $media_type_id: The media type config entity ID.

1 call to MediaSourceTestBase::createMediaTypeField()
MediaSourceTestBase::createMediaTypeFields in core/modules/media/tests/src/FunctionalJavascript/MediaSourceTestBase.php
Create a set of fields in a media type.

File

core/modules/media/tests/src/FunctionalJavascript/MediaSourceTestBase.php, line 40

Class

MediaSourceTestBase
Base class for media source tests.

Namespace

Drupal\Tests\media\FunctionalJavascript

Code

protected function createMediaTypeField($field_name, $field_type, $media_type_id) {
    $storage = FieldStorageConfig::create([
        'field_name' => $field_name,
        'entity_type' => 'media',
        'type' => $field_type,
    ]);
    $storage->save();
    FieldConfig::create([
        'field_storage' => $storage,
        'bundle' => $media_type_id,
    ])->save();
    // Make the field widget visible in the form display.
    $component = \Drupal::service('plugin.manager.field.widget')->prepareConfiguration($field_type, []);
    
    /** @var \Drupal\Core\Entity\EntityDisplayRepositoryInterface $display_repository */
    $display_repository = \Drupal::service('entity_display.repository');
    $entity_form_display = $display_repository->getFormDisplay('media', $media_type_id, 'default');
    $entity_form_display->setComponent($field_name, $component)
        ->save();
    // Use the default formatter and settings.
    $component = \Drupal::service('plugin.manager.field.formatter')->prepareConfiguration($field_type, []);
    $entity_display = $display_repository->getViewDisplay('media', $media_type_id);
    $entity_display->setComponent($field_name, $component)
        ->save();
}

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