diff -u b/src/Entity/MediaBundle.php b/src/Entity/MediaBundle.php --- b/src/Entity/MediaBundle.php +++ b/src/Entity/MediaBundle.php @@ -296,7 +296,6 @@ ->save(); } } - $field->save(); } } diff -u b/src/MediaTypeBase.php b/src/MediaTypeBase.php --- b/src/MediaTypeBase.php +++ b/src/MediaTypeBase.php @@ -7,7 +7,6 @@ use Drupal\Core\Config\Config; use Drupal\Core\Entity\EntityFieldManagerInterface; use Drupal\Core\Entity\EntityTypeManagerInterface; -use Drupal\Core\Field\FieldDefinitionInterface; use Drupal\Core\Form\FormStateInterface; use Drupal\Core\Plugin\ContainerFactoryPluginInterface; use Drupal\Core\StringTranslation\StringTranslationTrait; @@ -143,12 +142,9 @@ public function buildConfigurationForm(array $form, FormStateInterface $form_state) { $options = []; - /** @var \Drupal\media_entity\MediaBundleInterface $bundle */ - $bundle = $form_state->getFormObject()->getEntity(); - foreach ($this->entityFieldManager->getFieldDefinitions('media', $bundle->id()) as $field_name => $field) { + foreach ($this->entityFieldManager->getFieldStorageDefinitions('media') as $field_name => $field) { $allowed_type = in_array($field->getType(), $this->pluginDefinition['allowed_field_types']); - $is_base_field = $field->getFieldStorageDefinition()->isBaseField(); - if ($allowed_type && !$is_base_field) { + if ($allowed_type && !$field->isBaseField()) { $options[$field_name] = $field->getLabel(); } } @@ -189,6 +185,9 @@ * {@inheritdoc} */ public function getSourceField(MediaBundleInterface $bundle) { + if (empty($this->configuration['source_field'])) { + return $this->createSourceField($bundle); + } $id = 'media.' . $bundle->id() . '.' . $this->configuration['source_field']; return $this->entityTypeManager @@ -199,6 +198,33 @@ } /** + * Returns the source field storage definition. + * + * @return \Drupal\field\FieldStorageConfigInterface + * The field storage definition. Will be unsaved if new. + */ + protected function getSourceFieldStorage() { + if ($this->configuration['source_field']) { + $id = 'media.' . $this->configuration['source_field']; + + return $this->entityTypeManager + ->getStorage('field_storage_config') + ->load($id); + } + else { + return $this->createSourceFieldStorage(); + } + } + + /** + * Creates the source field storage definition. + * + * @return \Drupal\field\FieldStorageConfigInterface + * The unsaved field storage definition. + */ + abstract protected function createSourceFieldStorage(); + + /** * Creates the source field definition for a bundle. * * @param \Drupal\media_entity\MediaBundleInterface $bundle @@ -206,7 +232,7 @@ * * @return \Drupal\field\FieldConfigInterface * The unsaved field definition. The field storage definition, if new, - * should not be saved either. + * should also be unsaved. */ abstract protected function createSourceField(MediaBundleInterface $bundle); diff -u b/src/Plugin/MediaEntity/Type/Generic.php b/src/Plugin/MediaEntity/Type/Generic.php --- b/src/Plugin/MediaEntity/Type/Generic.php +++ b/src/Plugin/MediaEntity/Type/Generic.php @@ -13,7 +13,8 @@ * @MediaType( * id = "generic", * label = @Translation("Generic media"), - * description = @Translation("Generic media type.") + * description = @Translation("Generic media type."), + * allowed_field_types = {"string"} * ) */ class Generic extends MediaTypeBase { @@ -56,8 +57,8 @@ /** * {@inheritdoc} */ - protected function createSourceField(MediaBundleInterface $bundle) { - $field_storage = $this->entityTypeManager + protected function createSourceFieldStorage() { + return $this->entityTypeManager ->getStorage('field_storage_config') ->create([ 'entity_type' => 'media', @@ -66,12 +67,17 @@ // generic media type. 'type' => 'string', ]); + } + /** + * {@inheritdoc} + */ + protected function createSourceField(MediaBundleInterface $bundle) { /** @var \Drupal\field\FieldConfigInterface $field */ return $this->entityTypeManager ->getStorage('field_config') ->create([ - 'field_storage' => $field_storage, + 'field_storage' => $this->getSourceFieldStorage(), 'bundle' => $bundle->id(), ]); } 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 @@ -1,6 +1,7 @@ selectFieldOption('Type provider', 'test_type'); $this->assertSession()->assertWaitOnAjaxRequest(); - // Make sure the checkbox for creating the source field is there and save. - $this->assertSession() - ->checkboxChecked('type_configuration[test_type][create_source_field]'); $page->pressButton('Save media bundle'); // Check whether the source field was correctly created. @@ -64,17 +62,24 @@ } /** - * Test creation of media bundle without default field. + * Test creation of media bundle, reusing an existing source field. */ - public function testBundleCreationWithoutDefaultField() { - $label = 'Bundle without Default Field'; + public function testBundleCreationReuseSourceField() { + // Create a new bundle, which should create a new field we can reuse. + MediaBundle::create([ + 'id' => 'pastafazoul', + 'label' => 'Pastafazoul', + 'type' => 'generic', + ])->save(); + + $label = 'Bundle reusing Default Field'; $bundleMachineName = str_replace(' ', '_', strtolower($label)); $this->drupalGet('admin/structure/media/add'); $page = $this->getSession()->getPage(); // Fill in a label to the bundle. - $page->fillField('label', 'Bundle without Default Field'); + $page->fillField('label', $label); // Wait for machine name generation. Default: waitUntilVisible(), does not // work properly. @@ -88,13 +93,15 @@ $this->assertSession()->assertWaitOnAjaxRequest(); // Un-check creation of default source field. - $page->uncheckField('type_configuration[test_type][create_source_field]'); + $page->selectFieldOption('type_configuration[generic][source_field]', 'field_media_generic'); $page->pressButton('Save media bundle'); // Check that there are not fields created. $this->drupalGet("admin/structure/media/manage/{$bundleMachineName}/fields"); - $this->assertSession() - ->elementsCount('xpath', '//*[text()="No fields are present yet."]', 1); + // The reused field should be present... + $this->assertSession()->pageTextContains('field_media_generic'); + // ...not a new, unique one. + $this->assertSession()->pageTextNotContains('field_media_generic_1'); } }