summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorAlex Pott2018-08-09 10:13:36 (GMT)
committerAlex Pott2018-08-09 10:13:44 (GMT)
commit4523ef6850d61f01239830ec9021f8f1bc701eac (patch)
tree2d2ce653e7afdad1fa519eaa018df8191146df9c
parentcf7c5cd81e1f94162003a5010b2d4657e1718ead (diff)
Issue #2988622 by samuel.mortenson, bkosborne, phenaproxima, chr.fritsch: Media library field widget loses pre-existing selection when editing entity to add another selection
(cherry picked from commit c6e6709878839e0639d73d7c1e4420a8008e8a33)
-rw-r--r--core/modules/media_library/src/Plugin/Field/FieldWidget/MediaLibraryWidget.php7
-rw-r--r--core/modules/media_library/tests/src/FunctionalJavascript/MediaLibraryTest.php24
2 files changed, 30 insertions, 1 deletions
diff --git a/core/modules/media_library/src/Plugin/Field/FieldWidget/MediaLibraryWidget.php b/core/modules/media_library/src/Plugin/Field/FieldWidget/MediaLibraryWidget.php
index e9362a0..22ac3ec 100644
--- a/core/modules/media_library/src/Plugin/Field/FieldWidget/MediaLibraryWidget.php
+++ b/core/modules/media_library/src/Plugin/Field/FieldWidget/MediaLibraryWidget.php
@@ -509,8 +509,13 @@ class MediaLibraryWidget extends WidgetBase implements ContainerFactoryPluginInt
* - weight: (int) A weight for the selection.
*/
protected static function getFieldState(array $element, FormStateInterface $form_state) {
+ // Default to using the current selection if the form is new.
+ $path = $element['#parents'];
+ $values = NestedArray::getValue($form_state->getValues(), $path);
+ $selection = isset($values['selection']) ? $values['selection'] : [];
+
$widget_state = static::getWidgetState($element['#field_parents'], $element['#field_name'], $form_state);
- $widget_state['items'] = isset($widget_state['items']) ? $widget_state['items'] : [];
+ $widget_state['items'] = isset($widget_state['items']) ? $widget_state['items'] : $selection;
return $widget_state;
}
diff --git a/core/modules/media_library/tests/src/FunctionalJavascript/MediaLibraryTest.php b/core/modules/media_library/tests/src/FunctionalJavascript/MediaLibraryTest.php
index 9ae45c3..da4ebeb 100644
--- a/core/modules/media_library/tests/src/FunctionalJavascript/MediaLibraryTest.php
+++ b/core/modules/media_library/tests/src/FunctionalJavascript/MediaLibraryTest.php
@@ -235,6 +235,30 @@ class MediaLibraryTest extends WebDriverTestBase {
$assert_session->pageTextContains('Bear');
$assert_session->pageTextContains('Horse');
$assert_session->pageTextContains('Turtle');
+
+ // Re-edit the content and make a new selection.
+ $this->drupalGet('node/1/edit');
+ $assert_session->pageTextNotContains('Dog');
+ $assert_session->pageTextContains('Cat');
+ $assert_session->pageTextContains('Bear');
+ $assert_session->pageTextContains('Horse');
+ $assert_session->pageTextContains('Turtle');
+ $unlimited_button = $assert_session->elementExists('css', '.media-library-open-button[href*="field_unlimited_media"]');
+ $unlimited_button->click();
+ $assert_session->assertWaitOnAjaxRequest();
+ $assert_session->pageTextContains('Media library');
+ // Select the first media items (should be Dog, again).
+ $checkbox_selector = '.media-library-view .js-click-to-select-checkbox input';
+ $checkboxes = $page->findAll('css', $checkbox_selector);
+ $checkboxes[0]->click();
+ $assert_session->elementExists('css', '.ui-dialog-buttonpane')->pressButton('Select media');
+ $assert_session->assertWaitOnAjaxRequest();
+ // "Dog" and the existing selection should still exist.
+ $assert_session->pageTextContains('Dog');
+ $assert_session->pageTextContains('Cat');
+ $assert_session->pageTextContains('Bear');
+ $assert_session->pageTextContains('Horse');
+ $assert_session->pageTextContains('Turtle');
}
/**