diff -u b/src/Entity/MediaBundle.php b/src/Entity/MediaBundle.php --- b/src/Entity/MediaBundle.php +++ b/src/Entity/MediaBundle.php @@ -247,59 +247,54 @@ /** * {@inheritdoc} */ - public function postSave(EntityStorageInterface $storage, $update = TRUE) { - parent::postSave($storage, $update); + public function preSave(EntityStorageInterface $storage) { + parent::preSave($storage); - if (!$update) { - $type_plugin = $this->getType(); + $type_plugin = $this->getType(); + if ($type_plugin instanceof SourceFieldInterface) { + $storage = $type_plugin->getSourceField($this)->getFieldStorageDefinition(); + if ($storage instanceof FieldStorageConfigInterface && $storage->isNew()) { + $storage->save(); + } + $configuration = $type_plugin->getConfiguration(); + $configuration['source_field'] = $storage->getName(); + $this->setTypeConfiguration($configuration); + } + } - if ($type_plugin instanceof SourceFieldInterface) { - $field = $type_plugin->getSourceField($this); + /** + * {@inheritdoc} + */ + public function postSave(EntityStorageInterface $storage, $update = TRUE) { + parent::postSave($storage, $update); - $storage = $field->getFieldStorageDefinition(); - if ($storage instanceof FieldStorageConfigInterface) { - $storage->save(); + $type_plugin = $this->getType(); + if ($type_plugin instanceof SourceFieldInterface) { + $field = $type_plugin->getSourceField($this); + + if ($field->isNew()) { + $entity_type = $field->getTargetEntityTypeId(); + $bundle = $field->getTargetBundle(); + + if ($field->isDisplayConfigurable('form')) { + $component = \Drupal::service('plugin.manager.field.widget') + ->prepareConfiguration($field->getType(), []); + + entity_get_form_display($entity_type, $bundle, 'default') + ->setComponent($field->getName(), $component) + ->save(); } - - if ($field->isNew()) { - $entity_type = $field->getTargetEntityTypeId(); - $bundle = $field->getTargetBundle(); - - if ($field->isDisplayConfigurable('form')) { - $widget = \Drupal::service('plugin.manager.field.widget') - ->getInstance([ - 'field_definition' => $field, - 'form_mode' => 'default', - 'configuration' => [], - ]); - - entity_get_form_display($entity_type, $bundle, 'default') - ->setComponent($field->getName(), [ - 'type' => $widget->getPluginId(), - 'settings' => $widget->getSettings(), - ]) - ->save(); - } - if ($field->isDisplayConfigurable('view')) { - $formatter = \Drupal::service('plugin.manager.field.formatter') - ->getInstance([ - 'field_definition' => $field, - 'view_mode' => 'default', - 'configuration' => [], - ]); - - entity_get_display($entity_type, $bundle, 'default') - ->setComponent($field->getName(), [ - 'type' => $formatter->getPluginId(), - 'settings' => $formatter->getSettings(), - ]) - ->save(); - } + if ($field->isDisplayConfigurable('view')) { + $component = \Drupal::service('plugin.manager.field.formatter') + ->prepareConfiguration($field->getType(), []); + + entity_get_display($entity_type, $bundle, 'default') + ->setComponent($field->getName(), $component) + ->save(); } $field->save(); } } - } } diff -u b/tests/modules/media_entity_test_type/src/Plugin/MediaEntity/Type/TestType.php b/tests/modules/media_entity_test_type/src/Plugin/MediaEntity/Type/TestType.php --- b/tests/modules/media_entity_test_type/src/Plugin/MediaEntity/Type/TestType.php +++ b/tests/modules/media_entity_test_type/src/Plugin/MediaEntity/Type/TestType.php @@ -11,7 +11,8 @@ * @MediaType( * id = "test_type", * label = @Translation("Test type"), - * description = @Translation("Test media type.") + * description = @Translation("Test media type."), + * allowed_field_types = {"string"} * ) */ class TestType extends Generic { diff -u b/tests/src/FunctionalJavascript/BundleCreationTest.php b/tests/src/FunctionalJavascript/BundleCreationTest.php --- b/tests/src/FunctionalJavascript/BundleCreationTest.php +++ b/tests/src/FunctionalJavascript/BundleCreationTest.php @@ -91,9 +91,8 @@ $this->assertSession()->optionExists('Type provider', 'test_type'); $page->selectFieldOption('Type provider', 'test_type'); $this->assertSession()->assertWaitOnAjaxRequest(); - - // Un-check creation of default source field. - $page->selectFieldOption('type_configuration[generic][source_field]', 'field_media_generic'); + // Select the existing field for re-use. + $page->selectFieldOption('type_configuration[test_type][source_field]', 'field_media_generic'); $page->pressButton('Save media bundle'); // Check that there are not fields created. only in patch2: unchanged: --- a/tests/src/FunctionalJavascript/MediaUiJavascriptTest.php +++ b/tests/src/FunctionalJavascript/MediaUiJavascriptTest.php @@ -140,6 +140,11 @@ class MediaUiJavascriptTest extends MediaEntityJavascriptTestBase { $this->assertFalse($loaded_bundle->getStatus()); $this->assertEquals($loaded_bundle->field_map, ['field_1' => 'name']); + // We need to clear the statically cached field definitions to account for + // fields that have been created by API calls in this test, since they exist + // in a separate memory space from the web server. + $this->container->get('entity_field.manager')->clearCachedFieldDefinitions(); + // Test that a media being created with default status to "FALSE" will be // created unpublished. /** @var \Drupal\media_entity\MediaInterface $unpublished_media */ only in patch2: unchanged: --- a/tests/src/Kernel/BasicCreationTest.php +++ b/tests/src/Kernel/BasicCreationTest.php @@ -79,7 +79,7 @@ class BasicCreationTest extends KernelTestBase { $this->assertEquals($test_bundle->get('label'), 'Test bundle', 'Could not assure the correct bundle label.'); $this->assertEquals($test_bundle->get('description'), 'Test bundle.', 'Could not assure the correct bundle description.'); $this->assertEquals($test_bundle->get('type'), 'generic', 'Could not assure the correct bundle plugin type.'); - $this->assertEquals($test_bundle->get('type_configuration'), [], 'Could not assure the correct plugin configuration.'); + $this->assertEquals($test_bundle->get('type_configuration'), ['source_field' => 'field_media_generic_1'], 'Could not assure the correct plugin configuration.'); $this->assertEquals($test_bundle->get('field_map'), [], 'Could not assure the correct field map.'); }