Issue #3218103 by phenaproxima, rpayanm, joachim: media module creates fields with the field prefix hardcoded and ignores the configuration

merge-requests/3574/head^2
catch 2023-03-02 10:53:31 +00:00
parent 68ba1bbeb1
commit c10a6abed9
2 changed files with 29 additions and 1 deletions

View File

@ -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');

View File

@ -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());
}
/**