Unverified Commit 4523ef68 authored by alexpott's avatar alexpott

Issue #2988622 by samuel.mortenson, bkosborne, phenaproxima, chr.fritsch:...

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 c6e67098)
parent cf7c5cd8
......@@ -509,8 +509,13 @@ protected static function getNewMediaItems(array $element, FormStateInterface $f
* - 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;
}
......
......@@ -235,6 +235,30 @@ public function testWidget() {
$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');
}
/**
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment