From c10a6abed9d5e731ec3a7d59a44e1c33c6bebddf Mon Sep 17 00:00:00 2001 From: catch Date: Thu, 2 Mar 2023 10:53:31 +0000 Subject: [PATCH] Issue #3218103 by phenaproxima, rpayanm, joachim: media module creates fields with the field prefix hardcoded and ignores the configuration --- core/modules/media/src/MediaSourceBase.php | 7 +++++- .../tests/src/Kernel/MediaSourceTest.php | 23 +++++++++++++++++++ 2 files changed, 29 insertions(+), 1 deletion(-) diff --git a/core/modules/media/src/MediaSourceBase.php b/core/modules/media/src/MediaSourceBase.php index df21b94c383..063706d4a1c 100644 --- a/core/modules/media/src/MediaSourceBase.php +++ b/core/modules/media/src/MediaSourceBase.php @@ -301,9 +301,14 @@ abstract class MediaSourceBase extends PluginBase implements MediaSourceInterfac * returned. Otherwise, a new, unused one is generated. */ protected function getSourceFieldName() { + // If the Field UI module is installed, and has a specific prefix + // configured, use that. Otherwise, just default to using 'field_' as + // a prefix, which is the default that Field UI ships with. + $prefix = $this->configFactory->get('field_ui.settings') + ->get('field_prefix') ?? 'field_'; // Some media sources are using a deriver, so their plugin IDs may contain // a separator (usually ':') which is not allowed in field names. - $base_id = 'field_media_' . str_replace(static::DERIVATIVE_SEPARATOR, '_', $this->getPluginId()); + $base_id = $prefix . 'media_' . str_replace(static::DERIVATIVE_SEPARATOR, '_', $this->getPluginId()); $tries = 0; $storage = $this->entityTypeManager->getStorage('field_storage_config'); diff --git a/core/modules/media/tests/src/Kernel/MediaSourceTest.php b/core/modules/media/tests/src/Kernel/MediaSourceTest.php index b6e67c897d4..1be735f7684 100644 --- a/core/modules/media/tests/src/Kernel/MediaSourceTest.php +++ b/core/modules/media/tests/src/Kernel/MediaSourceTest.php @@ -15,6 +15,11 @@ use Drupal\media\Entity\MediaType; */ class MediaSourceTest extends MediaKernelTestBase { + /** + * {@inheritdoc} + */ + protected static $modules = ['field_ui']; + /** * Tests that metadata is correctly mapped irrespective of how media is saved. */ @@ -506,6 +511,24 @@ class MediaSourceTest extends MediaKernelTestBase { $this->assertTrue($field->isRequired(), 'Field is not required.'); $this->assertEquals('Test source with constraints', $field->label(), 'Incorrect label is used.'); $this->assertSame('test_constraints_type', $field->getTargetBundle(), 'Field is not targeting correct bundle.'); + + // Test that new source fields respect the configured field prefix, no + // prefix at all if that's what's configured. + $this->installConfig('field_ui'); + $this->config('field_ui.settings') + ->set('field_prefix', 'prefix_') + ->save(); + $type = MediaType::create([ + 'id' => $this->randomMachineName(), + 'label' => $this->randomString(), + 'source' => 'test', + ]); + $this->assertSame('prefix_media_test', $type->getSource()->createSourceField($type)->getName()); + + $this->config('field_ui.settings') + ->set('field_prefix', '') + ->save(); + $this->assertSame('media_test', $type->getSource()->createSourceField($type)->getName()); } /**