Skip to content
Snippets Groups Projects
Verified Commit b3c0ee60 authored by Lee Rowlands's avatar Lee Rowlands
Browse files

Issue #3276845 by shalini_jha, chris burge, smustgrave, bramdriesen, larowlan:...

Issue #3276845 by shalini_jha, chris burge, smustgrave, bramdriesen, larowlan: MediaSourceBase::getSourceFieldName() doesn't check character max

(cherry picked from commit 702aba79)
parent d5f7540e
No related branches found
No related tags found
22 merge requests!12353Issue #3529265: Fix empty plural translations being incorrectly treated as valid due to delimiter,!12227Issue #3181946 by jonmcl, mglaman,!12079Issue #3523476 by matthiasm11: Add empty check on operator,!12024Fix: DocBlock comment for return value of Drupal\Core\Database\Connection::transactionManager(),!11974Draft: Issue #3495165 by catch, joeyroth, berdir, texas-bronius: Better warning...,!11934Issue #3520997: DefaultLazyPluginCollection unnecessarily instantiates plugins when sorting collection,!11887Issue #3520065: The migrate Row class API is incomplete,!11636Draft: Issue #3515643 by macsim: fieldNameExists method is inconsistent,!11515Issue #3480419 by mondrake, smustgrave, catch: Method...,!11380Issue #3490698 by catch, spokje: Bump MINIMUM_STABILITY back to 'stable' when...,!11281Use Drupal Core Leadership terminology in MAINTAINERS.txt,!11239Issue #3507548: Allow workspace changes listing to show all items, without a pager,!11238Fix issue #3051797,!11213Issue #3506743 by tomislav.matokovic: Increasing the color contrast for the navigation block title against the background of the navigation sidebar to at least 4.5:1,!11147Draft: Try to avoid manually setting required cache contexts,!11108Issue #3490298 by nicxvan: Profiles can be missed in OOP hooks,!11093Drupal on MongoDB 11.1.x,!11017Issue #3502540: Add date filter for moderated content.,!11009Issue #3486972 migrate feed icon,!10999Cleaning up Taxonomy hooks and updating baseline.,!10977Issue #3501457: Fix path used in a A11y Test Admin,!10881Issue #3489329 by mfb, casey: symfony/http-foundation commit 32310ff breaks PathValidator
Pipeline #384482 canceled
Pipeline: drupal

#384487

    ......@@ -6,6 +6,7 @@
    use Drupal\Core\Entity\Display\EntityFormDisplayInterface;
    use Drupal\Core\Entity\Display\EntityViewDisplayInterface;
    use Drupal\Core\Entity\EntityFieldManagerInterface;
    use Drupal\Core\Entity\EntityTypeInterface;
    use Drupal\Core\Entity\EntityTypeManagerInterface;
    use Drupal\Core\Field\FieldTypePluginManagerInterface;
    use Drupal\Core\Form\FormStateInterface;
    ......@@ -314,10 +315,16 @@ protected function getSourceFieldName() {
    // Iterate at least once, until no field with the generated ID is found.
    do {
    $id = $base_id;
    // Limit the base field name to the maximum allowed length.
    $id = (strlen($base_id) > EntityTypeInterface::ID_MAX_LENGTH) ? substr($base_id, 0, EntityTypeInterface::ID_MAX_LENGTH) : $base_id;
    // If we've tried before, increment and append the suffix.
    if ($tries) {
    $id .= '_' . $tries;
    // Ensure the suffixed field name does not exceed the maximum allowed length.
    if (strlen($id) > EntityTypeInterface::ID_MAX_LENGTH) {
    $id = substr($base_id, 0, (EntityTypeInterface::ID_MAX_LENGTH - strlen('_' . $tries))) . '_' . $tries;
    }
    }
    $field = $storage->load('media.' . $id);
    $tries++;
    ......
    ......@@ -21,3 +21,7 @@ media.source.test_hidden_source_field:
    media.source.test_different_displays:
    type: media.source.test
    label: 'Test media source with different source field displays'
    media.source.test_source_with_a_really_long_name:
    type: media.source.test
    label: 'Test source with a really long name'
    <?php
    declare(strict_types=1);
    namespace Drupal\media_test_source\Plugin\media\Source;
    use Drupal\Core\StringTranslation\TranslatableMarkup;
    use Drupal\media\Attribute\MediaSource;
    /**
    * Provides test media source.
    */
    #[MediaSource(
    id: 'test_source_with_a_really_long_name',
    label: new TranslatableMarkup('Test source with a really long name'),
    description: new TranslatableMarkup('Test source with a really long name.'),
    allowed_field_types: ['string'],
    )]
    class TestSourceWithAReallyLongName extends Test {
    }
    ......@@ -516,6 +516,43 @@ public function testSourceFieldCreation(): void {
    $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 a source with a long machine name.
    $type = MediaType::create([
    'id' => 'test_type_fail',
    'label' => 'Test type - Fail',
    'source' => 'test_source_with_a_really_long_name',
    ]);
    $type->save();
    /** @var \Drupal\field\Entity\FieldConfig $field */
    $field = $type->getSource()->createSourceField($type);
    /** @var \Drupal\field\Entity\FieldStorageConfig $field_storage */
    $field_storage = $field->getFieldStorageDefinition();
    $field_storage->save();
    // Field configuration depends on the field storage, which must be saved first.
    $field->save();
    // Test long field name is truncated.
    $this->assertSame('field_media_test_source_with_a_r', $field_storage->getName(), 'Incorrect field name is used.');
    $type = MediaType::create([
    'id' => 'test_type_fail_2',
    'label' => 'Test type - Fail 2',
    'source' => 'test_source_with_a_really_long_name',
    ]);
    $type->save();
    /** @var \Drupal\field\Entity\FieldConfig $field */
    $field = $type->getSource()->createSourceField($type);
    /** @var \Drupal\field\Entity\FieldStorageConfig $field_storage */
    $field_storage = $field->getFieldStorageDefinition();
    $field_storage->save();
    // Field configuration depends on the field storage, which must be saved first.
    $field->save();
    // Test long field name is truncated.
    $this->assertSame('field_media_test_source_with_a_1', $field_storage->getName(), 'Incorrect field name is used.');
    // Test that new source fields respect the configured field prefix, no
    // prefix at all if that's what's configured.
    $this->installConfig('field_ui');
    ......
    0% Loading or .
    You are about to add 0 people to the discussion. Proceed with caution.
    Please register or to comment