Commit c10a6abe authored by catch's avatar catch
Browse files

Issue #3218103 by phenaproxima, rpayanm, joachim: media module creates fields...

Issue #3218103 by phenaproxima, rpayanm, joachim: media module creates fields with the field prefix hardcoded and ignores the configuration
parent 68ba1bbe
Loading
Loading
Loading
Loading
+6 −1
Original line number Diff line number Diff line
@@ -301,9 +301,14 @@ public function createSourceField(MediaTypeInterface $type) {
   *   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');

+23 −0
Original line number Diff line number Diff line
@@ -15,6 +15,11 @@
 */
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 @@ public function testSourceFieldCreation() {
    $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());
  }

  /**