Currently hook_node_prepare()
is a one-off buried into NodeFormController::prepareEntity()
. Let's generalize this approach to make it available to any entity type.
Related issues:
- #1810394: Site configuration with domain based language negotiation results in redirecting authenticated users to a different domain when accessing a content entity route for translation language different from the interface language
- #2018707: Do we still need to support hook_field_prepare_translation() ?
Comment | File | Size | Author |
---|---|---|---|
#39 | entity-prepare-2025991-39.interdiff.do_not_test.patch | 6.92 KB | plach |
#39 | entity-prepare-2025991-39.patch | 38.36 KB | plach |
#34 | entity-prepare-2025991-34.interdiff.do_not_test.patch | 1.49 KB | plach |
#34 | entity-prepare-2025991-34.patch | 40.83 KB | plach |
#32 | entity-prepare-2025991-32.interdiff.do_not_test.patch | 1.59 KB | plach |
Comments
Comment #1
plachHere is a patch.
Comment #2
plachmore tags
Comment #3
plachaw
Comment #4
fagoGenerally, +1 on doing so. Here some thoughts about it:
Comment #5
plachI completely agree, I picked the current name to avoid API changes as this allows us to use the existing
hook_node_preprare()
implementations. It was a conservative choice, but I guess we can still do this before code freeze.We ARE doing it here as well, otherwise tests wouldn't be green ;)
Good point
Do you mean explicitly? Because it should be available in the form state (see the example in entity.api.php)
Comment #6
plachThis should address #5.
Comment #7
Gábor HojtsyLooks generally good except a missing healthy does of dependency injection.
Acts
I think we are moving away from default in favor of "edit", "delete", etc, no?
"in default values"
Well, so @YesCT would say if you call \Drupal from a method, you do it wrong, the modulehandler should be injected on controller creation. She has references to show in case needed :)
You don't have this comment elsewhere either, I don't think we need / should have this here.
Comment #8
plachDefinitely a good dose of DI ;)
Comment #9
plachComment #11
fagoI see we can get the controller via $form_state, but yeah - I think the operation is something most implementations should or will have to check here. I'd pass $operation and $langcode as separate parameters to highlight this will run per operation and language, for anything special people still can go for $form_state['controller'].
Why is that removed - shouldn't it be updated instead?
Comment #12
plach[double post]
Comment #13
plachThis should fix some failures.
I just realized that ET will need to change the translation object all other hook implementations will work on, and this can be done only by making the controller return the one matching the form langauge after it has been updated by ET. Hence I needed to change the signature to pass along the controller isntaead of the entity. Spoke with Gabor and he agrees on this approach.
We don't need language since it will be available on the translation object, does it feel enough or you still think we should pass the operation explicitly?
My bad, I thought we documented just the generic one.
Comment #14
fagoThe patch seems to still pass $entity not $controller as first argument?
Howsoever, I think this should work analogously to hook_entity_prepare_view().
So how about going with
hook_entity_prepare_form($entity, $form_display, $operation, $form_state)
?The passed entity can be just $controller->getEntity() - so that should not make a difference compared to passing $controller. However, imho it's better DX to have all the used parameters separately compared to having to get everything out of the controlller?
Regarding language I agree - it's probably the way it will work in d8 everywhere that you just get a language-aware $entity.
Comment #16
plachImplemented #14 :)
Comment #17
plachI'll try to take this home myself.
Comment #19
plachRerolled.
Comment #21
plachThis should be green.
Comment #22
plachFixed a couple of things in comments and API docs.
Comment #24
plachThis time for real :)
Comment #25
Gábor HojtsyI found nothing to complain about, not even code style and @plach walked me through the recent changes too.
Comment #26
alexpottComment #27
David Hernández CreditAttribution: David Hernández commentedRe-roll
Comment #28
David Hernández CreditAttribution: David Hernández commentedChanged status
Comment #30
plachThe reroll was missing some pieces, this one should be complete. Let's wait for the bot.
Comment #32
plachWe have one brand new form controller after #111715: Convert node/content types into configuration :)
Comment #34
plachActually two form controllers, and a random failure :(
Comment #35
Gábor HojtsyLooks good, reviewed multiple times above.
Comment #36
tim.plunkettWhat does this do that a form alter cannot? Seems strangely overkill
This just worsens the DX for a simple entity form.
I went out of my way to *not* pass $form_state to prepareEntity
Why is $operation added here?
Comment #37
alexpottBased on #36 I think we need more discussion here...
Comment #38
plachFor instance it changes the entity values before field widgets are generated. Changing the default values keys afterwards would be much harder. The use case I had in mind was being able to switch the form language before the form is built.
Why? If it has no additional constructor parameter it can skip it altogether and things will just work. See
NodeTypeFormController
for instance.Why? It's a form hook after all.
Just a reroll issue I think.
Comment #39
plachSpoke with Tim in IRC. This one should be better.
Comment #40
tim.plunkettThis is much more reasonable, IMO.
Back to RTBC if green.
Btw, I like this change.
Comment #41
alexpottCommitted f74d298 and pushed to 8.x. Thanks!
Let's ensure the change notices are up-to-date...
https://drupal.org/list-changes/drupal?keywords_description=EntityFormCo...
Comment #42
plachAdded a paragraph about
hook_entity_prepare_form()
in the API changes section of https://drupal.org/node/1734556.Comment #43
plachAlso renaming for easier tracking.
Comment #44
catchComment #45
penyaskitoI read the change notice and looks clear to me. The only thing that confused me is the example of hook_menu, because I expected to find a Routing example instead.
Comment #46
plachWell, that was already there :)
Comment #47
Gábor HojtsyThat menu example is adding a local tab. Those are still in flux, so I would not bother with keeping that updated to interim APIs. It was already there before. Thanks for the update!
Comment #48
plach