View source
<?php
namespace Drupal\Tests\media_migration\Traits;
use Drupal\field\FieldConfigInterface;
use Drupal\media\Entity\MediaType;
use Drupal\media\MediaSourceInterface;
use Drupal\media\MediaTypeInterface;
use Drupal\Tests\media\Traits\MediaTypeCreationTrait;
trait MediaMigrationTestTrait {
use MediaTypeCreationTrait;
protected function getFixtureFilePath() {
return drupal_get_path('module', 'media_migration') . '/tests/fixtures/drupal7_media.php';
}
protected function createStandardMediaTypes(bool $only_allow_default_extensions = FALSE) {
$this
->createMediaType('image', [
'id' => 'image',
]);
$media_type = MediaType::create([
'id' => 'document',
'label' => 'Document',
'source' => 'file',
]);
assert($media_type instanceof MediaTypeInterface);
$source = $media_type
->getSource();
$source_field = $source
->createSourceField($media_type);
$source_field
->getFieldStorageDefinition()
->set('field_name', 'field_media_document');
$source_field
->set('field_name', 'field_media_document');
$source_field
->set('label', 'Document');
if (!$only_allow_default_extensions) {
$source_field
->setSetting('file_extensions', 'txt rtf doc docx ppt pptx xls xlsx pdf odf odg odp ods odt fodt fods fodp fodg key numbers pages');
}
$source_configuration = $source
->getConfiguration();
$source_configuration['source_field'] = $source_field
->getName();
$source
->setConfiguration($source_configuration);
$this
->assertSame(SAVED_NEW, $media_type
->save());
$source_field
->getFieldStorageDefinition()
->save();
$source_field
->save();
$form_display = \Drupal::service('entity_display.repository')
->getFormDisplay('media', $media_type
->id(), 'default');
$source
->prepareFormDisplay($media_type, $form_display);
$form_display
->save();
$this
->createMediaType('video_file', [
'id' => 'video',
]);
$this
->createMediaType('audio_file', [
'id' => 'audio',
]);
$this
->createMediaType('oembed:video', [
'id' => 'remote_video',
]);
foreach ([
'image',
'document',
] as $media_type_id) {
$media_type = MediaType::load($media_type_id);
$source = $media_type
->getSource();
assert($source instanceof MediaSourceInterface);
$source_field_name = $source
->getConfiguration()['source_field'];
$source_field_id = implode('.', [
'media',
$media_type_id,
$source_field_name,
]);
$source_field = $this->container
->get('entity_type.manager')
->getStorage('field_config')
->load($source_field_id);
assert($source_field instanceof FieldConfigInterface);
$dependencies = $source_field
->getDependencies() + [
'enforced' => [
'module' => [
'media',
],
],
];
$source_field
->set('dependencies', $dependencies)
->save();
}
}
}