Data loss using Preview on Article content type with unlimited images.

  1. Optionally edit Article content type and enable the title field
  2. Add image and alt(/title) text to node, click Preview.
  3. Go back, alt(/title) text is missing.
  4. Add alt(/title) text again, click preview.
  5. Go back, all is fine.

Beta phase evaluation

Reference: https://www.drupal.org/core/beta-changes
Issue category Bug because something is broken
Issue priority Major because data is lost during when it should be saved instead
Prioritized changes The main goal of this issue is bug fix
Disruption Not disruptive

Comments

kattekrab created an issue. See original summary.

larowlan’s picture

Component: field system » node system
Priority: Normal » Major
Issue tags: +Entity Field API

Data loss during preview is major in my books

NodeForm/preview is node module so I reckon stick it there for now?

kattekrab’s picture

Component: node system » field system
Priority: Major » Normal
kattekrab’s picture

Component: field system » node system
Priority: Normal » Major

Gah. sorry. save conflicts.

back to node system and major and adding related issue

#2548713: Only one additional new value saved unlimited field and no non-field values are restored after preview

jibran’s picture

Data loss means critical

kattekrab’s picture

Jibran - well the data hasn't actually been saved yet - so it's debateable whether that's critical.

This has always been the problem with preview. Nothing is saved until the user hits save.

legolasbo’s picture

Issue summary: View changes

It's not just the alt field, the title field is also emptied when enabled. However, i cannot reproduce the loss of images. These are my steps to reproduce on current head (commit 8ba635b)

1. Edit article content type and make image field unlimited.
2. Add images and alt text to node, click Preview.
3. Go back, alt text is missing.
4. Add alt text again, click preview.
5. Go back, all is fine.

legolasbo’s picture

Assigned: Unassigned » legolasbo

Trying to debug this.

legolasbo’s picture

Title: Data loss using Preview on Article content type with unlimited images. » Data loss using Preview on content types with image field.
Assigned: legolasbo » Unassigned
Issue summary: View changes
Status: Active » Needs review
StatusFileSize
new606 bytes

Issue also occurs on fields with a limited cardinality.

Attached patch fixes the issue.

haza’s picture

Issue tags: +Needs tests

I confirm that the issue is still present.

I've tested the patch, and it fixes the issue (even if I can't really understand why this simple foreach fixes the issue).

We should be able to add a test for this I think, then we should be able to RTBC.

legolasbo’s picture

Status: Needs review » Needs work

@Haza,

I agree tests should be added.

(even if I can't really understand why this simple foreach fixes the issue)

Hopefully the explanation below helps you understand.

  • +++ b/core/modules/node/src/NodeForm.php
    @@ -83,7 +83,10 @@ public function form(array $form, FormStateInterface $form_state) {
    -      $form_state = $preview;
    

    variable $form_state holds a reference to the form state object. In the code above it gets assigned the value of $preview, which causes the reference to the actual form_state object to be lost. This in turn causes any changes to $form_state to no longer propagate to the form state object that was originally passed in.

  • +++ b/core/modules/node/src/NodeForm.php
    @@ -83,7 +83,10 @@ public function form(array $form, FormStateInterface $form_state) {
    +      foreach ($preview->getValues() as $name => $value) {
    +        $form_state->setValue($name, $value);
    +      }
    

    By setting the values of $preview one by one, we are actually updating the form state object in stead of overwriting it's reference.

  • haza’s picture

    Ho, I see now. Thanks for the explanations !

    larowlan’s picture

    Assigned: Unassigned » larowlan

    Working on a test

    larowlan’s picture

    Assigned: larowlan » Unassigned
    Issue tags: -Needs tests
    StatusFileSize
    new3.98 KB
    new4.57 KB

    fail/pass

    legolasbo’s picture

    Status: Needs work » Needs review

    The last submitted patch, 15: preview-data-loss-2551217.fail_.patch, failed testing.

    jibran’s picture

    Issue tags: +Needs manual testing

    Code changes looks good just needs a manual testing and some minor feedback.

    +++ b/core/modules/node/src/NodeForm.php
    @@ -83,7 +83,10 @@ public function form(array $form, FormStateInterface $form_state) {
           /** @var $preview \Drupal\Core\Form\FormStateInterface */
    -      $form_state = $preview;
    +
    +      foreach ($preview->getValues() as $name => $value) {
    +        $form_state->setValue($name, $value);
    +      }
    

    A Comment here would help a lot.

    lauriii’s picture

    Issue summary: View changes

    Added beta-evaluation

    kattekrab’s picture

    Assigned: Unassigned » kattekrab

    /me testing.

    kattekrab’s picture

    Assigned: kattekrab » Unassigned
    Issue summary: View changes
    Status: Needs review » Reviewed & tested by the community
    Issue tags: -Needs manual testing
    StatusFileSize
    new89.05 KB

    Manually tested - alt and title text not lost with the patch. Reconfirmed they are lost against head.

    Nicely done!

    haza’s picture

    While testing the patch, I found an other "issue" that is also fixed by the patch.

    The issue

    - Fresh D8 install
    - Add Content > Article
    - Fill the title, do not add an image, clic "preview"
    - In the preview window, go back to the node edit page.
    - Try to add an image, it silently fails.

    Am I not able to reproduce this with the attached patch (that's why I do not create an other core issue), but maybe we could also add a test for this case ?

    What do you think (leaving this as RTBC) ?

    legolasbo’s picture

    @Haza,

    I think you should add a new issue for the issue you found and postpone it on this issue. We can then add the tests for it in that issue.

    larowlan’s picture

    haza’s picture

    Awsome, thanks the link to the other issue.

    catch’s picture

    Status: Reviewed & tested by the community » Fixed

    Committed/pushed to 8.0.x, thanks!

    • catch committed 0b132ec on 8.0.x
      Issue #2551217 by larowlan, legolasbo: Data loss using Preview on...

    Status: Fixed » Closed (fixed)

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