Problem:

Entities edited via IEF do not get updated during the buildEntity() phase of the main entity. This can cause various issues with form workflows that involve a form rebuild.

The issue where I encountered it was two similar issues having the same root cause (this):
- Liveblog module does a preview of posts which reloads the form and adds in the preview, similar as node previews in d7. However, the preview of inline edited entities (media entity tweets) are not updated due to this bug.
- Paragraphs module can show the preview of collapsed paragraphs during editing. The preview of inline edited media entity tweets does not reflect updates - I assume because of the same issue.

Suggested solution

IEF needs to separate entity-building phase and invoke it via the main forms #entity_builder callback. Then the main entity will be updated when needed always.
Finally, the saving of the changes need to be done on the final button. It's a bit hard to detect when this would be the case generically, but as proposed in https://www.drupal.org/node/2830136#comment-11798483 we could start trying to detect the ::save() method.
However, detecting the ::save method would not be a 100% thing as it would still miss buttions which call save() manually from some other callback. Given this use-case, I think core should be improved to allow the extensions of the entity-save process of an entity form, just as it is already possible with #entity_builders in the build process.

Comments

fago created an issue. See original summary.

fago’s picture

StatusFileSize
new3.17 KB

I took a look at this:

IEF needs to separate entity-building phase and invoke it via the main forms #entity_builder callback. Then the main entity will be updated when needed always.

This does not work as the update entity is mapped back by the widget during the initial entity building of the main entity already. However, the widget can easily invoke entity building when extracting form values what is done during entity building. That way, everything works as it should.

Attached is a patch which fixes the problem for the simple widget. However, I did not adapt the complex widget as I would have to understand its flow first. (Where/when are entities update?) So this is todo.

With that patch my preview use case works, such that the preview rendering gets an updated entity with all the changes. The previewing has issues with more bugs during the rendering phase but those are not IEF issues (I managed to identify all of them).

fago’s picture

Status: Active » Needs review
nicrodgers’s picture

Status: Needs review » Needs work

Patch applies cleanly and fixes the issue for the simple type. Thanks @Fago, this really helped me out today as I had the same problem when previewing media entity reference fields.

I'll set this to needs work, because a similar solution is needed for the complex widget.

nicrodgers’s picture

Issue tags: +Needs tests
volkerk’s picture

Enable paragraphs and inline_entity_form modules.
Add node type, add paragraphs field to the node type.
Add taxonomy vocabulary, add paragraphs field to terms.
Add paragraphs type with an entity reference field pointing at terms of created vocab.
Use inline_entity_form simple widget for the entity reference field.

Create a new node, add a paragraph, fill term name, add paragraph in term, fill in another term name, save the node.

This results in an php error:

TypeError: Argument 2 passed to Drupal\Component\Utility\NestedArray::getValue() must be of the type array, null given, called in InlineParagraphsWidget.php on line 1267 
dunebl’s picture

Nearly the same problem described in #6 but with a slighty different error:

TypeError: Argument 1 passed to Drupal\\Core\\Form\\FormState::setError() must be of the type array, null given, called in /core/lib/Drupal/Core/Field/WidgetBase.php on line 439 in /core/lib/Drupal/Core/Form/FormState.php on line 1093 

I have explained the problem there: https://www.drupal.org/project/inline_entity_form/issues/2627978#comment...

damienmckenna’s picture

A prototype was built by oknate to specifically add node preview integration to Inline Entity Form: https://www.drupal.org/sandbox/damienmckenna/2990314

I'm not entirely sure it works, evidently it did at one point, but there might have been some API changes as it wasn't working for me.

I'm going to write some test coverage for this and will see if we can get it to a point that we're properly describing the issue, then we might be able to better

damienmckenna’s picture

Status: Needs work » Needs review
StatusFileSize
new2.79 KB

An initial test to check how node previews work; this does not include the patches above, it's a test-only patch.

damienmckenna’s picture

StatusFileSize
new5.87 KB

Putting the two together, i.e. fago's patch from #1 and my test from #9.

The last submitted patch, 9: inline_entity_form-n2830829-9-testonly.patch, failed testing. View results

damienmckenna’s picture

damienmckenna’s picture

This adds test coverage for the complex_simple content type / field structure to see if the preview functionality works there. Right now that test fails, which suggests the patch from #2 is insufficient to cover nested entities.

Status: Needs review » Needs work

The last submitted patch, 13: inline_entity_form-n2830829-13.patch, failed testing. View results
- codesniffer_fixes.patch Interdiff of automated coding standards fixes only.

damienmckenna’s picture

These assertions fail:

  • fail: [Other] Line 101 of modules/contrib/inline_entity_form/tests/src/Functional/NodePreviewTest.php:
    "Child node" found
  • fail: [Other] Line 101 of modules/contrib/inline_entity_form/tests/src/Functional/NodePreviewTest.php:
    "Grandchild node" found

This is because the nested entities are not working in the preview, though they do save if you click the save button instead of the preview button.

damienmckenna’s picture

So, the failure makes complete sense, given that the patch doesn't currently touch InlineEntityFormComplex.php, just InlineEntityFormSimple.php.

damienmckenna’s picture

Status: Needs work » Needs review
StatusFileSize
new1.2 KB
new8.97 KB

WIP, added the changes from InlineEntityFormSimple.php to InlineEntityFormComplex.php.

Status: Needs review » Needs work

The last submitted patch, 17: inline_entity_form-n2830829-17.patch, failed testing. View results
- codesniffer_fixes.patch Interdiff of automated coding standards fixes only.

damienmckenna’s picture

Patch #17 has the same assertion failures as #13.

Anyone have any idea where to take this next?

damienmckenna’s picture

I realized that extractFormValues() wasn't being triggered for the preview button, so this triggers it. Of course now there's the problem that the nested nodes are actually saved instead of previewed. Whoops.

(kinda fumbling around here..)

berdir’s picture

I'm not sure if you can do this just in IEF to any entity type.

In paragraphs, we did get previews working, but it relies on \Drupal\entity_reference_revisions\EntityNeedsSaveInterface, which paragraphs implements and the ERR field supports by serializing referenced entities that need saving, so they can be stored with their changes for the preview and restored when going back to edit.

#2824097: Deep serialization for content entities to make an exact snapshot of an entity object's structure based on its current state is the core issue that is tying to generalize this idea, so maybe combined with that...

damienmckenna’s picture

Status: Needs work » Needs review
StatusFileSize
new13.56 KB
new5.58 KB

I extended the testing to see what happens when the node is saved and then edited again. Now the preview of the simple form stops working :-\

Status: Needs review » Needs work

The last submitted patch, 22: inline_entity_form-n2830829-23.patch, failed testing. View results
- codesniffer_fixes.patch Interdiff of automated coding standards fixes only.

oknate’s picture

OMG, I'm back to this issue again on a new project. I tested out the IEF_Preview sandbox module and found that it fixed one issue for me, but not another.

It fixed a missing image on preview (an entity reference) that was missing by adding it back from the form data on the preview route. So I have a node with field_image, field_image is a reference field. Currently with inline_entity_form, the image isn't available on the preview route, but with the sandbox module it was displaying. So there's some value there. I haven't tried the patches on this issue yet, so I'll compare this approach.

I'm experiencing a different issue, related to several I've seen on inline_entity_form, the form fails validation when the field_image is required and it won't let me get to the preview route. Because inline entity form doesn't play nice with entity constraints. So whatever the solution is, it can't be just on the preview route, it needs to figure out how to work with required fields.

Since Paragraphs has gotten this working, as Berdir pointed out, we could possibly pull some ideas from that solution.

I'm not sure we need to serialize the data, as it's already being stored in form storage. If you hit the back to editing button, you don't lose your changes.

oknate’s picture

I have added a patch on this issue: https://www.drupal.org/project/inline_entity_form/issues/2932492

The patch adds entity builder and build the fields after the "preview" button has been clicked on node forms.

This is somewhat based on the work in the sandbox module mentioned above.
patch

It may be that we can expand this beyond just when the "Preview" button is pressed. But I wanted to limit the scope of the change, because I needed a fix for a specific use case, and don't have the time today to delve into expanding it beyond that scope.

I'd like to get the unit tests that Damien McKenna set up back up and running, but it looks like they need to be moved over to BrowserTestBase.

tonytheferg’s picture

I am having an issue using complex IEF that are nested in a node. I have a 1:1 product and variation in an inline entity form, and when you attempt to edit the form, you can update the product (parent), but but you cant update the child (variation). If you save the node with the IEF open in edit mode, the child updates as you would expect it to by simply updating the IEF.

It sounds like this is a common problem, unrelated to products from this on Drupal Answers, so I am wondering if this is the right issue. Or is there a different thread for the issue i am describing?

geek-merlin’s picture

This at least needs tests rerolled.

albertosilva’s picture

Please see this patch I added on this issue: https://www.drupal.org/project/inline_entity_form/issues/2932492#comment...

I think it solves all problems between IEF and preview, even with nested complex widgets.

albertosilva’s picture

Status: Needs work » Needs review
haitran51196’s picture

Because we use the inline entity form to add a variation right in the add product form so we try not to save the variation right away, as we can edit it later, maybe it would help someone having the same problem.

haitran51196’s picture

StatusFileSize
new1.75 KB

In review i realized that i overdue the deletion so i decided just to delete the condition of the save entity and delete entity.

drupalninja99’s picture

@Damien et al, is there any reason why we can't get this patch committed?

damienmckenna’s picture

It needs test coverage. And we need to manually test the latest patches, which are very different to #22.

podarok’s picture

podarok’s picture

Status: Needs review » Needs work