Problem/Motivation

There are a number of bugs with the preview form and specifically fields with unlimited values.

The basic problem is that we only restore the field values but nothing else from the form state storage but field widgets rely on that. All fields rely on the item count, e.g. the file widget stores all data in there.

Steps to reproduce:

- Go to admin/structure/types/manage/article/fields to manage content type fields.
- Click on 'Add field' button.
- Add new field of 'Text(plain) field type.
- Make sure field's cardinality is set to unlimited.

- Go to http://local.d8/node/add/article to create a new article
- Fill out the required fields, add one value to the multi-value field, save
- Edit again
- Add at least two values additional values to the multi-value field
- Click 'Preview' button
- All values are displayed.
- On the new page click on 'Back to content editing button'
- All values are still displayed.
- press Save
- Only the first additional value (in total 2) was saved.

Same happens when editing content, already having e.g. one value and adding two more.

The reason is a mix of two things, first is the problem that we do not restore the form storage but only the values. But the other part is we immediately throw away the form state from key value for existing articles, so when the form is rebuild again for saving, we do not have the data to build enough form elements and throw away the additional ones.

Additionally, non-field values are also completely broken.

Proposed resolution

We restore the complete form state storage, not just the values. The second problem is solved by always relying on the UUID and only deleting the stored preview form_state after saving. So that it can be reliably restored for both form builds.

This is possibly a regression caused by the change to no longer cache forms with ajax elements on the first request.

Non-field values are restored by making sure to take over the user input.

Remaining tasks

User interface changes

The changed edit preview has one side effect. Previously, you could go to edit, then preview, then click away, then go back to the edit form and you you see the changes that you previewed again. The easiest way to see this is through another bug: click on edit on admin/content, make a change, then click on preview. You are redirect back to admin/content, when you click on edit again it looks as if your values were saved but it's actually just the preview.

I'd say that's actually an unexpected and confusing behaviour and not restoring them makes more sense.

API changes

Data model changes

CommentFileSizeAuthor
#76 cannot_add_more_items-2548713-76-interdiff.txt649 bytesberdir
#76 cannot_add_more_items-2548713-76.patch11.83 KBberdir
#72 cannot_add_more_items-2548713-72-interdiff.txt9.1 KBberdir
#72 cannot_add_more_items-2548713-72.patch11.75 KBberdir
#53 cannot_add_more_items-2548713-52-test-only.patch5.66 KBjohnchque
#52 interdiff-2548713-51-52.txt1.84 KBjohnchque
#52 cannot_add_more_items-2548713-52.patch8.85 KBjohnchque
#51 cannot_add_more_items-2548713-51-interdiff.txt1.03 KBberdir
#51 cannot_add_more_items-2548713-51.patch8.01 KBberdir
#49 interdiff-2548713-46-49.txt924 bytesjohnchque
#49 cannot_add_more_items-2548713-49.patch7.97 KBjohnchque
#46 interdiff-2548713-45-46.txt750 bytesjohnchque
#46 cannot_add_more_items-2548713-46.patch7.07 KBjohnchque
#45 interdiff-2548713-43-45.txt2.46 KBjohnchque
#45 cannot_add_more_items-2548713-45.patch6.93 KBjohnchque
#43 interdiff-2548713-39-43.txt879 bytesjohnchque
#43 cannot_add_more_items-2548713-43.patch5.99 KBjohnchque
#39 cannot_add_more_items-2548713-39.patch5.96 KBjohnchque
#11 add-more-preview-2548713.4.patch4.02 KBswentel
#11 interdiff.txt629 bytesswentel
#9 add-more-preview-2548713.3.patch4.08 KBlarowlan
#9 interdiff.txt1.94 KBlarowlan
#7 add-more-preview-2548713.2.patch3.28 KBlarowlan
#3 add-more-preview-2548713.failingtest.patch2.38 KBlarowlan
#21 cannot_add_more_items-2548713-21.patch3.48 KBsubhojit777
#23 cannot_add_more_items-2548713-23.patch3.47 KBsubhojit777
#23 interdiff.txt471 bytessubhojit777
#26 interdiff.txt861 byteslarowlan
#26 add-more-preview-2548713.26.patch4.84 KBlarowlan
#29 interdiff.txt1017 byteslarowlan
#29 add-more-preview-2548713.28.patch4.9 KBlarowlan
#34 interdiff.txt1.26 KBswentel
#34 add-more-preview-2548713.34.patch5.92 KBswentel

Comments

alx_benjamin created an issue. See original summary.

larowlan’s picture

Issue tags: +Entity Field API
larowlan’s picture

Status: Active » Needs review
StatusFileSize
new2.38 KB

here's a failing test

Status: Needs review » Needs work

The last submitted patch, 3: add-more-preview-2548713.failingtest.patch, failed testing.

larowlan’s picture

The issue here is $preview = $form_state loses the $form_state->getStorage('field_storage') when the NodeForm::form method returns.

No idea why

larowlan’s picture

So $form_state = $preview is invalid as it doesn't persist outside NodeForm::form (not passed by reference).

So we need to copy the values over.

larowlan’s picture

Assigned: Unassigned » swentel
StatusFileSize
new3.28 KB

This fixes the issue *but* breaks the reload feature (seen as test fails)
But I don't think we can have both, as allowing reload means we keep loading the stale form state from the tempstore using the UUID.

I think the fix is to remove those three asserts that check reloading retains the data.

@swentel - is there a reason we support reloading for the unsaved node (but not for saved nodes)?

swentel’s picture

Assigned: swentel » Unassigned

Hmm, the only reason I can think of (right now) is that if you reload node/add/{x}, you'd lose all entered data. However, that's only the case if you have gone to the preview at least once since we have an entry in the tempstore at that point - or at least in current HEAD.

I'm fine with removing that 'feature'. Reloading is a deliberate action by either clicking on the reload button in your browser or using a keyboard shortcut. Also, in case you'd click away and hit the back button, you're also still fine.

larowlan’s picture

Status: Needs work » Needs review
StatusFileSize
new1.94 KB
new4.08 KB

Ok removes tests for that feature

swentel’s picture

Issue tags: +Needs manual testing

Code looks fine to me - added manual testing just for a double confirmation, didn't get to that yet.

+++ b/core/modules/node/src/Tests/PagePreviewTest.php
@@ -149,13 +178,7 @@ function testPagePreview() {
+    ¶

Extreme nitpick - spaces :)

swentel’s picture

StatusFileSize
new4.02 KB
new629 bytes

Removed the newline

swentel’s picture

Did some manual testing on the node article which I made unlimited.
It has some weird side effects like losing the alternate text, or even a second image when coming back to the node form - unless that is already in HEAD, ran out of time to test that.

kattekrab’s picture

Confirming that alt text getting lost after previewing is in head.
But if you add the alt text again, preview for a 2nd time, it is still there when going back.

Quick chat with @larowlan - going to send that to a new issue.

webchick’s picture

Lee asked me about this in IRC. I had no idea that you could hit reload in your browser and get your old form contents back. I would never in my life expect that to happen, since it doesn't happen literally anywhere else on the entire internet. :) Reload = destructive. (Honestly, by design. It's the "oh crap, abort abort!" button.) So I'm fine to kill that feature, as long as we don't screw Views UI in the process, in case people were counting on that for that UI. (I don't see any hunks outside of Node so I think we're fine there.)

swentel’s picture

Status: Needs review » Reviewed & tested by the community
Issue tags: -Needs manual testing

Tested it again - looks good now. #2551217: Data loss using Preview on content types with image field. fixes the actual data loss then.

The last submitted patch, 7: add-more-preview-2548713.2.patch, failed testing.

The last submitted patch, 11: add-more-preview-2548713.4.patch, failed testing.

subhojit777’s picture

Assigned: Unassigned » subhojit777
Status: Reviewed & tested by the community » Needs work
Issue tags: +Needs reroll
subhojit777’s picture

Assigned: subhojit777 » Unassigned
Status: Needs work » Needs review
Issue tags: -Needs reroll
StatusFileSize
new3.48 KB

Status: Needs review » Needs work

The last submitted patch, 21: cannot_add_more_items-2548713-21.patch, failed testing.

subhojit777’s picture

StatusFileSize
new3.47 KB
new471 bytes

By mistake I had removed the code changes that are already committed. Still the tests will fail (tested on local). Will look into them later.

larowlan’s picture

Assigned: Unassigned » larowlan

looking

larowlan’s picture

Component: field system » node system
larowlan’s picture

Assigned: larowlan » Unassigned
Status: Needs work » Needs review
StatusFileSize
new861 bytes
new4.84 KB

So the issue here is that FileWidget::submit stores the uploaded items in storage.

Which takes precedence over $items taken from FormStateInterface::getValues() in \Drupal\file\Plugin\Field\FieldWidget\FileWidget::formMultipleElements

    // Load the items for form rebuilds from the field state as they might not
    // be in $form_state->getValues() because of validation limitations. Also,
    // they are only passed in as $items when editing existing entities.
    $field_state = static::getWidgetState($parents, $field_name, $form_state);
    if (isset($field_state['items'])) {
      $items->setValue($field_state['items']);
    }

and in \Drupal\file\Plugin\Field\FieldWidget\FileWidget::submit

    // Update items.
    $field_state = static::getWidgetState($parents, $field_name, $form_state);
    $field_state['items'] = $submitted_values;
    static::setWidgetState($parents, $field_name, $form_state, $field_state);

So let's see what if this breaks anything.

Status: Needs review » Needs work

The last submitted patch, 26: add-more-preview-2548713.26.patch, failed testing.

larowlan’s picture

Assigned: Unassigned » larowlan
larowlan’s picture

Status: Needs work » Needs review
StatusFileSize
new1017 bytes
new4.9 KB

Ok, instead of clearing it, we should update it.

larowlan’s picture

Assigned: larowlan » Unassigned

In my books this is ready now

swentel’s picture

Assigned: Unassigned » swentel

Will look and do manual test today

swentel’s picture

Status: Needs review » Needs work

So there's still a problem here - unless we have a different issue for that ?

1) Set image to unlimited
2) Add an image
3) Click preview
4) Go back to node edit
5) Add another image
6) it won't work
7) Try to add again, now it will work
8) Click preview and you'll see the second image
9) Go back to node edit
10) Hit preview again, second image will be gone.

Tricky one :/

swentel’s picture

Assigned: swentel » Unassigned

Will try to check later today to write a test for #32

swentel’s picture

Status: Needs work » Needs review
StatusFileSize
new5.92 KB
new1.26 KB

Patch that mimics the scenario from #32 until step 6 proving the fail to upload the second image after coming back from preview.

The last submitted patch, 34: add-more-preview-2548713.34.patch, failed testing.

The last submitted patch, 34: add-more-preview-2548713.34.patch, failed testing.

mgifford’s picture

Status: Needs review » Needs work

Version: 8.0.x-dev » 8.1.x-dev

Drupal 8.0.6 was released on April 6 and is the final bugfix release for the Drupal 8.0.x series. Drupal 8.0.x will not receive any further development aside from security fixes. Drupal 8.1.0-rc1 is now available and sites should prepare to update to 8.1.0.

Bug reports should be targeted against the 8.1.x-dev branch from now on, and new development or disruptive changes should be targeted against the 8.2.x-dev branch. For more information see the Drupal 8 minor version schedule and the Allowed changes during the Drupal 8 release cycle.

johnchque’s picture

Status: Needs work » Needs review
StatusFileSize
new5.96 KB

Extended tests. Added a new approach, it seems the node is not saved with the change. Let's see with testbot.

berdir’s picture

+++ b/core/modules/node/src/NodeForm.php
@@ -89,7 +94,12 @@ public function form(array $form, FormStateInterface $form_state) {
+          // We need to store the uuid in a new key to take it from there on
+          // preview.
+          $store->set('post_' . $uuid, $preview);

not on preview, on form submission.

Status: Needs review » Needs work

The last submitted patch, 39: cannot_add_more_items-2548713-39.patch, failed testing.

berdir’s picture

diff --git a/core/modules/node/src/NodeForm.php b/core/modules/node/src/NodeForm.php
index 6165d74..6d13454 100644
--- a/core/modules/node/src/NodeForm.php
+++ b/core/modules/node/src/NodeForm.php
@@ -76,7 +76,7 @@ public function form(array $form, FormStateInterface $form_state) {
     }
 
     $key = $uuid;
-    if (\Drupal::request()->isMethod('POST')) {
+    if (\Drupal::request()->isMethod('POST') && !$this->entity->isNew()) {
       $key = 'post_' . $uuid;
     }
 

Add that, then adding content works again.

johnchque’s picture

Status: Needs work » Needs review
StatusFileSize
new5.99 KB
new879 bytes

True, test passes locally with it, let's see about test bot. :)

berdir’s picture

Status: Needs review » Needs work

Turns out my idea with post_$uuid doesn't work, it would still be used if you refresh the page and then submit.

Discussed with @swentel and I have a different idea:

(12:48:08) berdir: swentel: the post_$uuid trick is passing both the existing and our new tests in https://www.drupal.org/node/2548713#comment-11085423. However, it occured to me that would also use that key if you refresh the page and then submit :-/
(12:50:25) swentel: berdir, hmm, and can we remove the refresh 'feature' ? Because that's fine for me, also for webchick.
(12:50:40) berdir: swentel: what if we also use the ?uuid= argument for edit?
(12:50:48) berdir: swentel: and only load from state there, but if present, always do it
(12:50:56) berdir: and support refreshing, which then kind of makes sense..
(12:51:28) swentel: berdir, works for me, add is using that too

So.... in the back to content link, always append the uuid argument. And in the code, only do it if that argument is present. And then remove the delete completely, then a refresh will keep the data, but given that we have that special argument, that makes sense to me.

johnchque’s picture

Status: Needs work » Needs review
StatusFileSize
new6.93 KB
new2.46 KB

Thank you for the feedback! :D
Seems to be working, let's see about testbot.

johnchque’s picture

Discussed with @Berdir this will help with consistency.

swentel’s picture

Love the cleanup, makes the code more consistent in general.

+++ b/core/modules/node/src/NodeForm.php
@@ -73,9 +73,8 @@ public function form(array $form, FormStateInterface $form_state) {
+      $form_state->setStorage($preview->getStorage());

We'll need a test for this line. Talked with berdir in IRC, and probably a simple form alter on the node form which puts something in the storage should do the trick for testing this.

Status: Needs review » Needs work

The last submitted patch, 46: cannot_add_more_items-2548713-46.patch, failed testing.

johnchque’s picture

Status: Needs work » Needs review
StatusFileSize
new7.97 KB
new924 bytes

Added code of a previous patch. Could not manage to fix the tests already written.

Status: Needs review » Needs work

The last submitted patch, 49: cannot_add_more_items-2548713-49.patch, failed testing.

berdir’s picture

Status: Needs work » Needs review
StatusFileSize
new8.01 KB
new1.03 KB

Ok, the problem with the previous patch was that we always took the storage now and replaced it if it was updated already. Now we skip that if we are rebuilding and have our own storage already.

Also, as far as I can see, setting the form values is not necessary, the test passes without that and manual testing is fine too.

We still need test coverage that form storage works in the first place.

johnchque’s picture

Thank you so much @Berdir for helping with the patch! Added test coverage. If it passes should be Ok now. :)

johnchque’s picture

Sorry, here we go with the test-only patch.

Status: Needs review » Needs work

The last submitted patch, 53: cannot_add_more_items-2548713-52-test-only.patch, failed testing.

berdir’s picture

Status: Needs work » Needs review

Test fail was just the test-only patch.

The last submitted patch, 23: cannot_add_more_items-2548713-23.patch, failed testing.

larowlan’s picture

Status: Needs review » Reviewed & tested by the community
  1. +++ b/core/modules/node/src/NodeForm.php
    @@ -67,31 +67,21 @@ protected function prepareEntity() {
    +    $request_uuid = \Drupal::request()->query->get('uuid');
    

    Nit: We can inject the request stack although I note that this is existing in HEAD

  2. +++ b/core/modules/node/src/NodeForm.php
    @@ -67,31 +67,21 @@ protected function prepareEntity() {
    -        $store->delete($uuid);
    

    For other reviewers note that this is cleaned up when the form is submitted and the node is saved (see \Drupal\node\NodeForm::save)

alexpott’s picture

+++ b/core/modules/file/src/Plugin/Field/FieldWidget/FileWidget.php
@@ -288,6 +288,14 @@ public function massageFormValues(array $values, array $form, FormStateInterface
+    // Update reference to 'items' stored during upload to take into account
+    // changes to values like 'alt' etc.
+    // @see \Drupal\file\Plugin\Field\FieldWidget\FileWidget::submit()
+    $field_name = $this->fieldDefinition->getName();
+    $field_state = static::getWidgetState($form['#parents'], $field_name, $form_state);
+    $field_state['items'] = $new_values;
+    static::setWidgetState($form['#parents'], $field_name, $form_state, $field_state);
+

Is it possible some contrib widgets will need similar fixes?

alexpott’s picture

Status: Reviewed & tested by the community » Needs review

Setting back to needs review to get an answer to #58

berdir’s picture

Yes, I guess it is possible.

But I don't see an alternative. We have to restore form storage. Other things are broken right now because that's missing, we're just not seeing it.

To be honest, I don't really understand what the code in FileWidget is even doing. Why is it the only widget affected by this. How is it not a problem for all other widgets? Why is that even needed?

The only other two modules that I have locally that use those widget state methods are Paragraphs and Inline Entity Form. I've just run all paragraphs tests and they are passing with this patch. We do have preview tests, quite a few actually. In fact, we even have a commented out assertion that is blocked on this issue because things are pretty broken without this fix.

And IEF doesn't support previews at all right now.

xjm’s picture

Issue tags: +Triaged core major

The core committers and Entity and Field maintainers agreed that this issue was a major bug. See #60 for some of the impacts of this; the issue is apparently quite disruptive when combined with a workflow that involves a lot of previewing (as is common with paragraph).

bojanz’s picture

berdir pinged me to look at this issue. The patch looks sane, I agree with the general approach.

It makes sense for a widget that uses widget state to keep that state up to date with changes to $items, but I'm unsure why the logic in FileWidget::submit() isn't enough (perhaps submit() doesn't trigger when needed?)

swentel’s picture

I've also had a quick look on the widget state code that this patch adds. As @bojanz said, the same routine happens in ::submit, but it isn't triggered, but I have no clue why yet either. Makes me wonder whether we can remove that code from the submit method if this now would live in massageFormValues() too ?

Version: 8.1.x-dev » 8.2.x-dev

Drupal 8.1.9 was released on September 7 and is the final bugfix release for the Drupal 8.1.x series. Drupal 8.1.x will not receive any further development aside from security fixes. Drupal 8.2.0-rc1 is now available and sites should prepare to upgrade to 8.2.0.

Bug reports should be targeted against the 8.2.x-dev branch from now on, and new development or disruptive changes should be targeted against the 8.3.x-dev branch. For more information see the Drupal 8 minor version schedule and the Allowed changes during the Drupal 8 release cycle.

berdir’s picture

Status: Needs review » Reviewed & tested by the community

Re #58.

I think it is pretty unlikely that another widget needs this. File upload is very special, it is the only one in core that uses widget state for this, everything else just works. It has to do with the very strange #type file_managed and how it is processing values.

The reason we do need it now is that there can be case where a file is uploaded and ::submit() is not called, for example when having JS disabled and directly clicking on preview.

IMHO, even if we would break some widget for preview, this is fixing worse bugs in core that affect pretty much all field types than breaking a widget. So setting back to RTBC, lets discuss if you're not convinced.

effulgentsia’s picture

Status: Reviewed & tested by the community » Needs review

Manually testing HEAD (8.3.x) with the steps in the current IS, I'm not experiencing the bug described.

Looking at the test in the patch, I'm not seeing those steps either. Instead I'm seeing this:

+++ b/core/modules/node/src/Tests/PagePreviewTest.php
@@ -260,6 +284,52 @@ function testPagePreview() {
+    $this->drupalPostForm(NULL, $edit, t('Preview'));
+    $this->clickLink(t('Back to content editing'));
+    $this->drupalPostForm(NULL, $edit, t('Preview'));
+    $this->clickLink(t('Back to content editing'));

Which is testing what happens when you go to preview and back multiple times. Which is a great test, and is uncovering a real bug in HEAD, but it's different than this issue's title and summary.

I'm not sure if this needs to be set to "Needs work" and "Needs issue summary update", or if the issue title and summary are correct, and there's something I'm doing wrong in testing this. Therefore, setting to "Needs review" for feedback on that.

berdir’s picture

Title: Cannot add more items to unlimited field after previewing created content » Only one additional new value saved unlimited field after preview
Issue summary: View changes
Status: Needs review » Reviewed & tested by the community

Sorry about the old isse summary. I updated it and also slightly changed the title to better explain the problem.

berdir’s picture

Issue summary: View changes
berdir’s picture

Status: Reviewed & tested by the community » Needs work

This breaks values from non-fields, e.g. menu_ui. We apparently have no test coverage for that, so we need to add that as well.

effulgentsia’s picture

  1. +++ b/core/modules/node/src/NodeForm.php
    @@ -67,31 +67,21 @@ protected function prepareEntity() {
    -      foreach ($preview->getValues() as $name => $value) {
    -        $form_state->setValue($name, $value);
    -      }
    

    This is likely the reason for #69.

  2. +++ b/core/modules/file/src/Plugin/Field/FieldWidget/FileWidget.php
    @@ -288,6 +288,14 @@ public function massageFormValues(array $values, array $form, FormStateInterface
    +    // Update reference to 'items' stored during upload to take into account
    +    // changes to values like 'alt' etc.
    +    // @see \Drupal\file\Plugin\Field\FieldWidget\FileWidget::submit()
    +    $field_name = $this->fieldDefinition->getName();
    +    $field_state = static::getWidgetState($form['#parents'], $field_name, $form_state);
    +    $field_state['items'] = $new_values;
    +    static::setWidgetState($form['#parents'], $field_name, $form_state, $field_state);
    

    massageFormValues() shouldn't have a side-effect like this. I think this makes more sense in extractFormValues(), where WidgetBase already does similar state manipulation. I wonder if we should split this whole bit out into a separate issue? Is it possible to trigger the problem this is solving independently of what the rest of this patch is doing? Seems to me like it should be, but I haven't delved into trying.

    Related: it bothers me that with this patch, we would have this code duplicated between FileWidget::extractFormValues() and FileWidget::submit(). In fact, looks like FileWidget::submit() already duplicates some of WidgetBase::extractFormValues(). I wonder if we could/should somehow make FileWidget::submit() invoke extractFormValues() rather than duplicate it? Possibly tricky due to the former being a static function, and maybe a bad idea for other reasons, just throwing it out there as a first impression.

effulgentsia’s picture

Also, thank you for the updated IS, but:

- press Save
- Only the first additional value (in total 2) was saved.

The test in the patch is not testing this. It's testing something similar, via multiple successive previews, but I think it's worth having an explicit test for the steps in the issue summary, since that appears a more direct pathway to data loss.

berdir’s picture

Title: Only one additional new value saved unlimited field after preview » Only one additional new value saved unlimited field and no non-field values are restored after preview
Issue summary: View changes
Status: Needs work » Needs review
StatusFileSize
new11.75 KB
new9.1 KB

Yeah, it is a bit more complicated than just restoring that code, because that actually don't really do anything. We restore form values from user input, not values. And restoring that makes it want to cache, so I have to trick it into believing it is a post request. Not sure if that has side effects.

One side effect of this is that the exact submitted values are restored and we don't rebuild based on the built entity. That means it requires a small change in the existing test coverage, because the term field is exactly as it was entered, without the (1) part.

I'm also sure that this is *not* a regression from HEAD, as setting form state values doesn't do anything.

I also added test coverage for actually saving and ensuring the value is there. And I moved the widget state stuff.

berdir’s picture

Still looking for reviews here :)

Also, I'm still surprised myself, but I actually *understand* FileWidget now I think. See #2811841-32: Add test coverage ensuring user input is mapped on the correct form elements when elements are reordered and the issue in general for a lot of fun facts about entity forms, widgets, widget state and more.

swentel’s picture

+++ b/core/modules/file/src/Plugin/Field/FieldWidget/FileWidget.php
@@ -288,15 +288,19 @@ public function massageFormValues(array $values, array $form, FormStateInterface
+
+  public function extractFormValues(FieldItemListInterface $items, array $form, FormStateInterface $form_state) {

Nit: {@inheritdoc}

I didn't know about getUserInput() this far, so would be nice if someone else could confirm that this is ok (although tests prove if of course).

tim.plunkett’s picture

  1. +++ b/core/modules/node/src/NodeForm.php
    @@ -67,31 +67,28 @@ protected function prepareEntity() {
    -      foreach ($preview->getValues() as $name => $value) {
    -        $form_state->setValue($name, $value);
    -      }
    ...
    +      $form_state->setUserInput($preview->getUserInput());
    ...
           $form_state->setRebuild();
    ...
    +      $form_state->setRequestMethod('POST');
    

    This will be the first usage of setUserInput() outside of Views code :)
    It also happens to be correct usage here.

  2. +++ b/core/modules/node/src/NodeForm.php
    @@ -67,31 +67,28 @@ protected function prepareEntity() {
    +      $form_state->setStorage($preview->getStorage());
    

    Looking at \Drupal\Core\Form\FormState::getCacheableArray(), pretty sure that storage is the only part we care about, so this looks correct.

berdir’s picture

Fixed the nitpick. This should be ready then?

swentel’s picture

Status: Needs review » Reviewed & tested by the community

Yes!

alexpott’s picture

Status: Reviewed & tested by the community » Fixed

I've manually tested this and it fixes the bug. There are no changes that contradict our BC policy.

Committed and pushed f403ccb to 8.3.x and 5d0814d to 8.2.x. Thanks!

diff --git a/core/modules/node/tests/modules/node_test/node_test.module b/core/modules/node/tests/modules/node_test/node_test.module
index 55149f4..866be19 100644
--- a/core/modules/node/tests/modules/node_test/node_test.module
+++ b/core/modules/node/tests/modules/node_test/node_test.module
@@ -10,9 +10,11 @@
 
 use Drupal\Core\Entity\EntityInterface;
 use Drupal\Core\Entity\Display\EntityViewDisplayInterface;
+use Drupal\Core\Form\FormStateInterface;
 use Drupal\Core\Session\AccountInterface;
 use Drupal\node\NodeInterface;
 
+
 /**
  * Implements hook_ENTITY_TYPE_view() for node entities.
  */
@@ -179,7 +181,7 @@ function node_test_node_insert(NodeInterface $node) {
 /**
  * Implements hook_form_alter().
  */
-function node_test_form_alter(&$form, \Drupal\Core\Form\FormStateInterface $form_state, $form_id) {
+function node_test_form_alter(&$form, FormStateInterface $form_state, $form_id) {
   if (!$form_state->get('node_test_form_alter')) {
     drupal_set_message('Storage is not set');
     $form_state->set('node_test_form_alter', TRUE);

Fixed on commit.

  • alexpott committed f403ccb on 8.3.x
    Issue #2548713 by yongt9412, larowlan, Berdir, swentel, subhojit777,...

  • alexpott committed 5d0814d on 8.2.x
    Issue #2548713 by yongt9412, larowlan, Berdir, swentel, subhojit777,...
berdir’s picture

Argh, I did find one regression here, actually identifed by a paragraph test that I didn't run when working on this (ParagraphsAccessTest) but took me a while to track it down.

Given the following:

* An Image field (probably file as well), does not need to be on paragraph/nested form, the one on article works to reproduce this
* JS disabled
* alt/title not required

When you select a file, and then directly go to Preview, without clicking "Upload" first, and then go back to content edit then the image field is in a weird, messed up state. Somehow I think we get a mess between user input and stored widget state so that the fids input overrides the actual data.

Saving works fine without clicking upload, it's really just Preview.

I *think* this is rare enough to not require that this is reverted but can be looked into in a follow-up, thoughts?

swentel’s picture

I'm fine with follow up

berdir’s picture

Status: Fixed » Closed (fixed)

Automatically closed - issue fixed for 2 weeks with no activity.