Problem/Motivation
We have *a lot* of duplicated, almost identical code in our delete confirmation forms. Every entity needs to copy that and make small adjustments, we also have small differences between them, some log messages, some don't, many have slightly different strings.
Proposed resolution
Add default delete forms for config and content entities, with a trait to share the code (they have different parent classes). Allow to override the deletion confirmation message and the log behavior, but do log by default.
Use that for all entity types in core, as far as possible. In some cases, I accepted the new messages and updated tests a bit, in others I added overrides. Quite a few delete forms can be removed completely.
For language deletions, two additional changes were made, access check to avoid deleting the default language is now done in the access control handler, which removes the need for a custom check in the form. Previously, that then did a redirect, now it is simply an access denied page. Updated the tests for that. Also removed a bogus check if the language really exists after it was already loaded.
Remaining tasks
User interface changes
API changes
New classes that can be used by default or with small overrides. Usage is completely optional, no existing API is changed.
Beta phase evaluation
| Issue category | Task because we can save 700 lines of stupid code and improve DX for defining entities |
|---|---|
| Issue priority | Currently on normal, but could probably be major? |
| Disruption | None, usage is completely optional. |
Original report by @plach
In #1499596: Introduce a basic entity form controller we unified the generation of entity forms for the default create/edit operation. Now we need to to the same for the deletion/deletion confirmation forms. We probably need to introduce a generic EntityDefaultFormController and a EntityDeleteFormController which would both extend a basic EntityBaseFormController.
| Comment | File | Size | Author |
|---|---|---|---|
| #52 | entity-delete-forms-1728804-52.patch | 79.3 KB | berdir |
| #47 | entity-delete-forms-1728804-47-interdiff.txt | 623 bytes | berdir |
| #47 | entity-delete-forms-1728804-47.patch | 79.31 KB | berdir |
| #41 | entity-delete-forms-1728804-41.patch | 79.31 KB | berdir |
| #37 | entity-delete-forms-1728804-37-interdiff.txt | 717 bytes | berdir |
Comments
Comment #1
berdirReviving this after a very long time.
@tstoeckler has started to work on this in #2312133: Make EntityForm::save() save the entity, I'm extracting this part from there.
Not tested yet.
Comment #2
jibranThis issue has a gender bias topic :P
Comment #4
plachComment #5
berdirDidn't update the delete forms properly.
Comment #7
berdirFixing tests. Either by updating the tests or be adding back the old message if they were better.
Comment #8
dawehnerLitteraly this code is the same: Luke: use traits here ... ... also maybe use the StringTranslationTrait?
It is not really helpful anymore to define a variable ...
Comment #10
berdirOk, made that a trait, it is a bit strange ($this->entity doesn't exist and I can't use the string translation trait there as it is coming from the form base class already, so I just have to rely on it being there...)
Also more cleanup and simplifications. Cleaned up language delete form a lot. It had a completely bogus check if the langcode exists and gets that langcode from the entity. Also the default language check should be in the access control handler, then that is checked automatically. This also allows to remove that check from LanguageListBuilder.
Comment #11
berdirAlmost all but not all delete forms write a log entry if something is deleted. Not sure about standardizing that as well, the part that is a bit annoying is that we can't easily use getDeletionMessage() for that as we are not supposed to translate the message, so we have to duplicate the string I guess, in a separate method.
Thoughts about making that the default behavior?
Another thing that is weird is the redirect/cancel url. right now the default cancel url is the entity url, which is also the redirect url. but that makes no sense ;) We really need a standard link template for the list...
Comment #13
berdirOk, this is starting to get interesting, implemented the logging part.
Comment #15
berdirFixed language access and the tests there and found another delete form to clean up.
Comment #16
jibranUsually this patch falls in @tim.plunkett's domain. :D
Code changes looks good maybe these two issues are also related here #2254935: Use a modal for content entity form delete links confirmation forms and #1842036: [META] Convert all confirm forms to use modal dialog
Comment #17
berdirCurrently working on #2401505: Add an entity collection template for lists , the last step to completely remove a bunch of custom delete forms.
@ibran: While those issues are logically related, they do not overlap in any relevant way, modal is controlled where the link is displayed, I'm only touching internals of those forms, not changing in any way how to work or look (Apart from a few minor string changes, by using the default strings in cases where I think they are actually better than what we have now).
Comment #18
jibranOk then it is RTBC for me. Pinged @tim.plunkett to eyeball the patch.
Comment #19
berdirThanks, but my plan is to get #2401505: Add an entity collection template for lists done first and then update this on top of that, to see how far we can go with this. Also want to write a change record, to announce that those classes can now be used.
Comment #20
tim.plunkettPlease add abstract protected methods to the trait for the methods you rely on, and you can @see there and remove this comment.
$this->t()
?!
Comment #21
berdirThanks.
1. Can add the method, but I can't add an abstract $this->entity (which is not actually mentioned there yet), suggestions for that?
3. see #10. That exception can not possibly ever be thrown, as we get $langcode from the already upcasted entity object.
Comment #22
tim.plunkettUse getEntity(), it's on EntityForm
Comment #23
wim leersHence postponing.
Tagging accordingly.
Comment #24
berdirThat got in, another 200 lines of code removed, including a bunch of deleted confirmation forms that no longer need a custom form class.
Completely untested. Probably full of PHP syntax errors :)
Comment #26
berdirSee.
Comment #28
berdirMissed one merge conflict.
Comment #30
andypostunrelated? it tests the page urls and message
Comment #31
berdirFixed the test fails.
@andypost: It is related, the language delete form had some custom access checks and did a redirect then. I moved that to an access check, which allows us to remove that and also the custom logic in the list builder. The difference is then that trying to delete that language (the default language), is simply a 403, like any other thing that can not be deleted.
I have some ideas how we could simplify a few lines further, like supporting collection link templates that are bundle specific, for terms and shortcuts, but this is probably going far enough already.
Comment #34
berdirComment #35
berdirComment #36
tstoecklerWow, this issue is just beautiful, I can't find any other words than that.
I read through the whole patch and only have one minor quibble, otherwise this can go RTBC IMO:
Seems this will log twice, no? I think logDeletion() should be overridden instead.
Comment #37
berdir/bow
You started it :)
Yes, fixed the language delete form, I must have missed that when I added the log message.
Comment #38
berdirDid not create a change record yet, as @alexpott mentioned recently, they are more for things that modules must change/adapt to. But it might still be useful to tell them, not sure.
Comment #39
tstoecklerHehe, that wasn't meant meant to be a hidden self-compliment. :-)
I meant specifically the splitting out of the getLogMessage(), etc. into separate methods. That makes things just way more maintainable, as is proven by the language deletion form.
This is RTBC for me, but - you are right - I did write some earlier less awesome version of this, so I guess it can't hurt for someone else to confirm.
Comment #41
berdirUps, lost the change from #31, thought I had an update to date local branch. Same interdiff as #31, just applied that again.
Comment #42
tim.plunkettVery nice.
+270, -1004 is great. Awesome work! I think this is RTBC.
Comment #43
wim leers57 files changed, 270 insertions, 1004 deletions.Magnificent. Thank you!
Comment #44
andypostfor change record
Comment #45
berdirChecked with @alexpott, he said this does not need a change record, because we are not changing anything but add something new.
Comment #46
alexpottThis will be logged twice - override the log method?
Comment #47
berdirNice catch, fixed that. Checked all other submitForm() overrides again, did not find another logger call (except NodeDeleteForm, but that does not call the parent).
Comment #48
dawehnerGreat effort!!
I'm curios whether we should just include the string translation trait and be done, see http://3v4l.org/J6YYi
Comment #49
berdirThat gave me a php strict error (which is apparently not actually visible on 3v4l.org, see http://3v4l.org/sicBo), because the part you are missing is the class c extends FormBase, which already includes that trait as well.
Comment #50
dawehnerDamn, sometimes things are not that easy in php 5.4
Comment #52
berdirReroll, trivial conflict in ShortcutDeleteForm.
Comment #53
tstoecklerComment #54
alexpottNot having 700 lines of code to maintain is huge win for reducing fragility. Also bringing all config entity deletes together might allow us to manage deleting/fixing dependent config entities. Committed 9be3165 and pushed to 8.0.x. Thanks!