I saw that the controller's entityForm() method has $entity_form[#op]. However, this is 'edit' in both cases, editing an inline entity and adding a new one.

Or is it in fact reflecting the operation on the outer form?

A way of knowing the op in the inner form is necessary I think.

Also, I assume from the way this works that there's sort of a third state: editing an entity that's now saved in the form (so not new, as in the form is populated with data) but hasn't yet been added to the database (so still sort of new, as in, don't do any database queries based on the data).

Comments

bojanz’s picture

Also, I assume from the way this works that there's sort of a third state: editing an entity that's now saved in the form (so not new, as in the form is populated with data) but hasn't yet been added to the database (so still sort of new, as in, don't do any database queries based on the data).

That falls under "edit".

A normal add (node page just loaded, add form opened), should have #op sets to "add" normally.

joachim’s picture

Right, but the inline entity being edited is in a no-mans-land, right?

This is the steps to get there:

1. Either add or edit a node, doesn't matter which
2. Go to add a new product ( ie inline entity)
3. Click the 'save product' button but don't save the main form
(optional: 4. Do something else such as edit node form element, or edit an existing product)
5. Click 'edit product' on the first product.

It's being edited... but AFAICT it's not saved in the database, but stored in the form values. So if an entity was going to assume 'edit' meant it could go digging in the database, that could be problematic.

But eh, this really is an edge case and by the by.

bojanz’s picture

It's still an edit.
In general, I don't think the entity form should ever care about #op, or go digging in the db. But I won't try to prevent anyone from shooting themselves in the foot :)

Do we have any issue here then? Or was it just about the edge case?

joachim’s picture

Yup -- is $entity_form[#op] as found in EntityInlineEntityFormController::entityForm about the outer form, or the inner form being defined in that method?

I was hoping to use the op -- if it's about the inner form -- to pass on to a regular entity form, as part of #1660802: add a controller for 'standard' EntityAPI entities.

Basically, if it does describe the inner form, it's got a bug.
If it doesn't describe the inner form, should it be renamed / documented / both?

bojanz’s picture

It's about the inner form.

And I don't know how it can be wrong:

if ($form_state['inline_entity_form'][$ief_id]['form'] == 'add') {
        $element['form']['#op'] = 'add';
joachim’s picture

Status: Active » Closed (cannot reproduce)

Hmm, nor can I. Just checked again and it's fine. I must have got confused in all the debug output I had flying around. Apologies for the noise.

guypaddock’s picture

Status: Closed (cannot reproduce) » Active

Re-opening. I'm still experiencing this issue, even in dev.

Editing and saving another variation while the form to add a new variation is open can cause the new variation to be saved prematurely.

Steps to Reproduce

  1. Open the node add form for product displays (/node/add/product-display).
  2. Add a new variation to the product display, either by creating one or selecting an existing variation.
  3. Add a new variation by clicking "Add a new variation".
  4. Fill-out all of the variation details but do not save the new variation.
  5. Click the "Edit" button next to the variation added in #2 for editing.
  6. Click the "Save variation" button on the variation added in #2 that was opened in #5.
  7. Click the "Save variation" button on the variation added in #3.

Expected Results
The product display will have two variations -- the one added in #2, and the one started in #3.

Actual Results
The product display has three variations -- the one added in #2, and two copies of the one added in #3. Saving the product display results in a SQL constraint violation.

If I debug the code, it looks like inline_entity_form_process_entity_form() is invoked twice when the edit form is saved, once for the edit form, and a second time for the add form, even though only the edit form was really saved. And, in each case, "op" matches the button of the form being processed rather than the name of the real button that was clicked (i.e. when the function is processing the edit form, "op" is "edit"; then, when it processes the add form in the same request, "op" is suddenly "add").

guypaddock’s picture

Title: $entity_form[#op] is inaccurate » $entity_form[#op] does not reflect the operation actually being performed
Status: Active » Needs review
StatusFileSize
new1.13 KB

Attached is a patch that corrects the following issues:

  • Having both a variation edit form and variation add form open would cause the variation in the add form to be saved prematurely when the edit form was saved.
  • Having multiple variation edit forms open would cause all variations to be saved prematurely when any edit form was saved.
guypaddock’s picture

One thought: my patch doesn't actually make #op more useful, it just doesn't rely on it as the only way to tell what form is being submitted.

guypaddock’s picture

Revised the patch -- IEF wasn't saving any entities that were open for editing when the overall node edit form was saved.

guypaddock’s picture

One more try: this fixes an issue where variations open for editing could be saved prematurely when a new variation was added.

This version also makes the conditional a bit easier to read by storing values in variables.

bojanz’s picture

Status: Needs review » Closed (duplicate)