Currently, new inline entities will only be saved if the 'submit', 'publish' or 'unpublish' actions are performed.
From ElementSubmit.php:
foreach (['submit', 'publish', 'unpublish'] as $action) {
If a new submit button (in our case 'Save and Continue') is added to the form, IEF is not adding the necessary submit handlers to that action.
IEF could instead add its necessary submit handlers to all actions.
i.e.
foreach (Element::children($form['actions']) as $action) {
Patch attached.
| Comment | File | Size | Author |
|---|---|---|---|
| #17 | ief-add_trigger_callback_to_submit_elements_with_save_action-2.patch | 804 bytes | guenole |
| #7 | ief-add_trigger_callback_to_submit_elements_with_save_action.patch | 797 bytes | DevOz |
Issue fork inline_entity_form-2830136
Show commands
Start within a Git clone of the project using the version control instructions.
Or, if you do not have SSH keys set up on git.drupalcode.org:
Comments
Comment #2
dimilias commentedI find this issue really interesting. Especially now that the content moderation is in core, the states can be set to be action buttons.
Still, a form upload button or a 'Back' button, in case of a multistep form, are also a form submit button, but if you press them, you wouldn't want to trigger the validation, right?
Comment #3
fagoI ran into the same issue, but I do not think the suggested solution is right. So let's start with re-categorizing this and re-phasing the issue title to reflect the problem, then discuss solutions.
Comment #4
fagoI do not think attaching to all submit buttons is something we can do: E.g. an entity might add a preview button there. Saving the inline edited entities is obviously not what you want then. I think the best guess that we could make here would be to save inline entities when the '::save' callback is added to the submit button.
Comment #5
fagoAdded #2830829: Entities are not updated during buildEntity() phase for the problem of some buttons missing the entity building / processing. Re-phrasing this for missing the final save/submission.
Comment #6
joachim commentedI think the best fix here might be to make ElementSubmit pluggable according to the outer form that the IEF form element is being shown in, in the same sort of way that the form handler class is pluggable based on the entity type being shown in the IEF element.
Comment #7
DevOz commentedHi guys,
Thanks @drasgardian for identifying the origin of the problem and for providing the initial patch.
As @fago say, attaching to all submit buttons can create undesirable behavior, and saving inline entities when the '::save' callback is added to the submit button could be a good improvement.
I notice than it can't be a final solution as explained in the other issue #2830829: Entities are not updated during buildEntity() phase that @fago has opened: "However, detecting the ::save method would not be a 100% thing as it would still miss buttons which call save() manually from some other callback.".
But here is the patch I did anyway, because it fits my need:
Note: my callback functions are prefixed by :: because there are member functions.
Best regards,
Olivier Blanc, Oz Conseil developper
Comment #8
DevOz commentedHi,
The last patch I proposed works well if the code to customize
::savebuttons is in a buildForm function, that is called before IEFElementSubmit::attach, but not if the code to customize::savebuttons is in a HOOK_form_alter, which is is called after IEFElementSubmit::attach.So it is needed to call
\Drupal\inline_entity_form\ElementSubmit\ElementSubmit::attach($form, $form_state);in HOOK_form_alter after having customized the::savebuttons.But it is not enough, because of the following code in
ElementSubmit::attach:ElementSubmit::attachhas been called before, so when calling it from HOOK_form_alter, it will return without attaching any trigger.So I propose to remove this return code and to attach IEF trigger conditionally instead, checking if the trigger has already been attached or not. So when calling
ElementSubmit::attachfrom HOOK_form_alter, an existing::savebutton won't have its trigger attached more than once, but a new button will have its trigger attached:It works well for my custom drop button (3 buttons that use the '::save' action):
A better solution would be that
ElementSubmit::attach($form, $form_state);be called after HOOK_form_alter, but I don't know how to do...Please find ief-add_trigger_callback_to_submit_elements_with_save_action-2.patch attached.
Best regards,
Olivier Blanc, Oz Conseil developper
Comment #9
jantoine commentedThe patch from #8 is too intrusive IMO as you have two easier options to ensure the callback is added to your custom buttons:
The patch from #7 is the minimum required to fix the issue and works great! Hiding other patches and setting to needs review, hoping it will run tests on the latest "visible" patch and not the hidden patch from #8. If not, that patch from #7 will need to be re-uploaded.
Comment #10
geek-merlinWhy can't a custom module that wants e.g. a "save and continue" button not simply copy and adjust the save button (or its callbacks) in a form alter?
Comment #11
jantoine commented@geek-merlin,
The issue here is that the Inline Entity Form is doing something, but doing it inconsistently. It's already adding these callbacks to some submit buttons, but not all submit buttons. I didn't even know these callbacks existed until I began loosing data (uploaded images) inconsistently across a site because custom buttons weren't moving those images from temporary to permanent storage, but core buttons were. It's unrealistic to expect every other module that adds a button to a form to know what callbacks it needs to add for other contrib modules enabled on the site.
Comment #12
ocastle commentedNeither Patch #7 or #8 solved the issue for me when using Save & Edit.
With regards to the comment about submit handlers, i would tend to agree with @jantoine that its the responsibility for IEF to handle and determine a true form submission.
Comment #13
geek-merlinOK thanks i think i got the idea and it makes sense to me now.
I do not have a good idea for this yet. Maybe someone(tm) study \Drupal\Core\Entity\ContentEntityForm to find a way we can hook in...
Comment #14
ocastle commentedComment #15
andyg5000I built a custom form that embeds IEF elements and fortunately named my submit element "submit" initially. However, when I went to add another submit button I realized things weren't getting saved.
For others that might stumble on this until a solution is found, make sure you have the following to your submit element:
Comment #16
maxmendez commentedThanks @andyg5000 your suggestion fixed my problem with a custom action button.
Comment #17
guenole commentedIn some cases $form['actions'] is not set and some notices are triggered.
This patch Just fix it, checking if the key exists in $form
Comment #20
shivam_tiwari commentedComment #22
podarokComment #24
geek-merlinBulk reopen.
Comment #25
geek-merlinReview: #8 states why the current code needs work.
Comment #26
tvalimaa commentedHi I had also this issue that my custom draft-button didn't save entity references which is added on ief.
Thanks to #15 advice that code example looks working correctly. Also I got my code working on #17 patch but I will keep #15 code changes and not patch module.