Updated: Comment #12
When EntityFormController was first written, it had both submit() and save() methods, and they were attached directly to each button.
Then when EntityFormController was moved to implement FormInterface, it gained a submitForm() method.
Proposal
We tried moving all submit and save logic to submitForm()
, and remove save()
and submit()
but this did not work because preview and save often require common code to run before they work. For an example see CommentFormController
.
The current approach is to remove submit()
and refactor into save()
and submitForm()
where appropriate. This means that for entity confirm forms the actions have been changed to not call the save()
submit handler and only call submitForm()
.
Comment | File | Size | Author |
---|---|---|---|
#69 | drupal_2022875_69.patch | 60.63 KB | Berdir |
#68 | drupal_2022875_68-interdiff.txt | 2.85 KB | Berdir |
#68 | drupal_2022875_68.patch | 60.62 KB | Berdir |
#66 | drupal_2022875_66-interdiff.txt | 1.79 KB | Berdir |
#66 | drupal_2022875_66.patch | 60.4 KB | Berdir |
Comments
Comment #1
tim.plunkettTagging.
Comment #2
fagook, so what would be the usual submit-form behaviour of an entity form then? I guess it would just update $form_state['entity']?
Comment #3
plachYep, we had the
submit()
method to recreate an entity object based on the submitted value ans update the wrapped entity object. The idea was that every action would need thesubmit()
and then provide a specific submission handler for its task.Comment #4
tim.plunkettIt's a really nice distinction that hasn't been used much, at least on the ConfigEntity side. Everything is crammed into submit().
Comment #5
plachWe could make the
submit()
method final and enforce a new submit handler for each action :)Comment #6
tim.plunkettNo, nothing in Drupal should be final, we just need to be consistent and provide a good example.
Comment #7
alexpottTalking to @tim.plunkett on IRC
I'm +1 to this. Three methods become 1 :)
Comment #8
alexpottSo here's a first take on make the 3 methods one. As you can see from the patch there has been confusion about which method to use - some people preferring
save()
and othersubmit()
. So bumping this to major.This patch changes everything to
submitForm()
which means every save method now calls parent::submitForm(). Perhaps we still want to have a save method but we should actively discourage people from overriding the EntityFormController::submitForm() by making EntityFormController abstract and moving save() to the interface but not implementing it in EntityFormController.So lets see what's broken :)
Comment #10
alexpottSo it appears that there is some value in having a submitForm and save method on
EntityFormController
since this allow for common actions for preview and save - seeCommentFormController
for an example.The issue though is that
EntityConfirmFormBase
andContentEntityConfirmFormBase
both extendEntityFormController
and on these having the submit handler call bothsubmitForm()
andsave()
makes no sense. Plus current head would make it impossible to provide any default code in thesave()
method as it is getting called on every entity confirm form submission :)The patch attached tries to rectify this by not calling
parent::actions()
inEntityConfirmFormBase
andContentEntityConfirmFormBase
.It also adds documentation indicating that if you are extending
EntityFormController
you probably do not need to overridesubmitForm()
but the specialised submit handler methods for the actions -delete()
andsave()
.Comment #12
alexpottSo it appears the EntityFormController::submitForm breaks the add view wizard.
Patch should fix remaining fails.
Comment #12.0
alexpottUpdate
Comment #13
alexpottTagging appropiately
Comment #14
catchTagging.
Comment #14.0
catchUpdate
Comment #15
Berdir12: 2022875.12.patch queued for re-testing.
Comment #17
vladan.me CreditAttribution: vladan.me commentedRe-roll #12
Comment #18
vladan.me CreditAttribution: vladan.me commentedComment #20
alexpottRerolled again, from #12 and fixed some spelling mistakes
Comment #22
alexpottNew form
ImageStyleFlushForm
...Comment #23
alexpottComment #24
tim.plunkettI think going from 3 to 2 methods is a big enough win here, especially since the differences between submitForm() and save() are better explained now.
Comment #25
tim.plunkettRerolled after #2125253: Convert $form_state['redirect'] to $form_state['redirect_route']. No changes.
Comment #26
Dries CreditAttribution: Dries commentedI'm rather confused by this phpDoc. I'm not sure what to make of it as a developer reading the API documentation. It reads as a '@todo: remove me' or something.
Comment #27
catchComment #28
areke CreditAttribution: areke commentedThe patch no longer applies; it should be re-rolled.
Comment #29
XanoRe-roll.
Comment #32
jibranNeeds reroll.
Comment #33
BerdirRe-rolled. That wasn't too ugly for a 2 month old patch of this size, let's see what the testbot thinks.
Comment #35
swentel CreditAttribution: swentel commentedShould be green, we still need to address #26.
Comment #37
swentel CreditAttribution: swentel commentedOh book ...
Comment #38
Berdir37: drupal_2022875_37.patch queued for re-testing.
Comment #40
BerdirLet's see how bad I messed up this re-roll.
Comment #42
BerdirStrange.
Comment #45
BerdirRe-roll. How are we going to get this in?
Comment #47
XanoRe-roll.
// Edit: not sure if I need some more re-rolling practice or if the patch should really be 150% as big as before the re-roll.
Comment #48
tim.plunkettNope, you're reintroducing deleted files.
I would double check to see that we're converting whatever code was moved out of those.
Comment #49
XanoUgh. Will go through this tomorrow morning. I didn't notice the file size until after the upload, that's why it's even in this issue.
Comment #51
XanoForget I ever uploaded #47. This should be a better/proper re-roll.
Comment #53
XanoComment #54
BerdirRe-roll after #2309323: Allow #submit and #validate to be specified as methods of the form object.
Comment #56
BerdirWrong merge.
Comment #57
tim.plunkettShould we have save() on EntityFormInterface?
Other than that, I think this is fine.
Comment #58
sunHm. Not sure whether we've arrived at a clean improvement over status quo just yet — the currently proposed patch introduces new inconsistencies:
Why is
validate
notvalidateForm
?It's no longer clear whether
submitForm()
performs the final operation or not:This
submitForm()
does not perform the final operation.This
submitForm()
does perform the final operation.This
submitForm()
does not perform the final operation.Comment #59
BerdirI was wondering about validate too.
I think it's an improvement over HEAD, we got rid of submit() and submitForm() isn't the one that is final step, but it's a step that is almost always called (operation/confirmation forms are sometimes a bit different).
Comment #61
dawehnerGreat work!
This might be a usecase for final, just guessing though.
Did you considered to alternative document $this->entity itself? It could be available in more places then
Did we considered that the parent save method returns the result of $this->entity->save() so we can easily call the parent?
Don't we call it duplicate now everywhere?
Comment #62
BerdirThanks for the review.
#56:
1. Added to the interface.
#58: I would prefer to look into validate()/validateForm() in a separate issue. This has been re-rolled often enough.
#61:
1. Hm. Not sure. Maybe, but that could be messy with unit tests if someone wants to unit test (parts of) a form.
2. Yes, documented the property now, I think that's a pretty good pattern. Also noticed some bogus documentation there, there is no Next button or wizard there anywhere.
3. Yes, I thought that before as well, did that now. Not too many overrides call into the parent, but what's funny is that ImageStyleEditForm actually expected it to work like that but it didn't. And now that it actually worked, there were duplicated save messages. Cleaned that form up a bit.
4. renamed to submitForm(), like most (all?) other confirm forms.
Comment #64
BerdirThis should fix the fails. Was a temporary state while cleaning up edit forms, no longer necessary to pass it through.
Comment #65
swentel CreditAttribution: swentel commentedExtreme nitpick: superfluous new line
I'm wondering whether we still need to address #26
Comment #66
BerdirRemoved that. Tried to improve those descriptions.
Comment #67
swentel CreditAttribution: swentel commentedHmm, this should be delete() right ?
Comment #68
BerdirYep and I missed one form.
Comment #69
BerdirRe-roll.
Comment #70
swentel CreditAttribution: swentel commentedChecked this twice now, let's move forward here.
Comment #71
catchCommitted/pushed to 8.0.x, thanks!
Comment #73
ArlaCould we get a change record for this?
Comment #74
ArlaSubmitted a draft change record at https://www.drupal.org/node/2336747
Comment #76
effulgentsia CreditAttribution: effulgentsia at Acquia commentedSee #2453175-59: Remove EntityFormInterface::validate() and stop using button-level validation by default in entity forms. Note: that issue title doesn't currently match the patch; not sure yet if we'll want to retitle that issue or revert to that issue's earlier direction.
Comment #77
quietone CreditAttribution: quietone at PreviousNext commentedPublished the CR