diff --git a/core/modules/media/src/MediaHandlerBase.php b/core/modules/media/src/MediaHandlerBase.php index d8a1f5d..9ac14c01 100644 --- a/core/modules/media/src/MediaHandlerBase.php +++ b/core/modules/media/src/MediaHandlerBase.php @@ -26,14 +26,14 @@ /** * The entity type manager service. * - * @var \Drupal\Core\Entity\EntityTypeManagerInterface; + * @var \Drupal\Core\Entity\EntityTypeManagerInterface */ protected $entityTypeManager; /** * The entity field manager service. * - * @var \Drupal\Core\Entity\EntityFieldManagerInterface; + * @var \Drupal\Core\Entity\EntityFieldManagerInterface */ protected $entityFieldManager; @@ -215,12 +215,28 @@ protected function getSourceFieldStorage() { } /** + * Provide source field type for creation of default source field. + * + * @return string + * Returns source field type. + */ + abstract protected function getSourceFieldType(); + + /** * Creates the source field storage definition. * * @return \Drupal\field\FieldStorageConfigInterface * The unsaved field storage definition. */ - abstract protected function createSourceFieldStorage(); + protected function createSourceFieldStorage() { + return $this->entityTypeManager + ->getStorage('field_storage_config') + ->create([ + 'entity_type' => 'media', + 'field_name' => $this->getSourceFieldName(), + 'type' => $this->getSourceFieldType(), + ]); + } /** * Creates the source field definition for a type. @@ -232,7 +248,15 @@ protected function getSourceFieldStorage() { * The unsaved field definition. The field storage definition, if new, * should also be unsaved. */ - abstract protected function createSourceField(MediaTypeInterface $type); + protected function createSourceField(MediaTypeInterface $type) { + /** @var \Drupal\field\FieldConfigInterface $field */ + return $this->entityTypeManager + ->getStorage('field_config') + ->create([ + 'field_storage' => $this->getSourceFieldStorage(), + 'bundle' => $type->id(), + ]); + } /** * Determine the name of the source field. @@ -259,8 +283,7 @@ protected function getSourceFieldName() { } $field = $storage->load('media.' . $id); $tries++; - } - while ($field); + } while ($field); return $id; } diff --git a/core/modules/media/tests/modules/media_test_handler/src/Plugin/media/Handler/Test.php b/core/modules/media/tests/modules/media_test_handler/src/Plugin/media/Handler/Test.php index cb3af62..de54fe3 100644 --- a/core/modules/media/tests/modules/media_test_handler/src/Plugin/media/Handler/Test.php +++ b/core/modules/media/tests/modules/media_test_handler/src/Plugin/media/Handler/Test.php @@ -70,29 +70,8 @@ public function buildConfigurationForm(array $form, FormStateInterface $form_sta /** * {@inheritdoc} */ - protected function createSourceFieldStorage() { - return $this->entityTypeManager - ->getStorage('field_storage_config') - ->create([ - 'entity_type' => 'media', - 'field_name' => $this->getSourceFieldName(), - // Strings are harmless, inoffensive puppies: a good choice for a - // generic media type. - 'type' => 'string', - ]); - } - - /** - * {@inheritdoc} - */ - protected function createSourceField(MediaTypeInterface $type) { - /** @var \Drupal\field\FieldConfigInterface $field */ - return $this->entityTypeManager - ->getStorage('field_config') - ->create([ - 'field_storage' => $this->getSourceFieldStorage(), - 'bundle' => $type->id(), - ]); + protected function getSourceFieldType() { + return 'string'; } }