Problem/Motivation
Entity forms where classes before the other forms were. This has been gradually merged together again, but entity forms are in some cases still special. One example is $form_state['controller'], which is now the same object as $form_state['build_info']['callback_object'].
It still hints at entity forms being controllers, but they were renamed to just entity forms and controllers re renamed in general to handlers: #1976158: Rename entity storage/list/form/render "controllers" to handlers.
The reason this object is important is that it's the API for entity form alter hooks to get the entity that is being edited: $form_state['controller']->getEntity().
Proposed resolution
We have the FormState object now, so it would be nice to have a single method for this object, something like:
FormStateInterface::getFormObject()? or just getObject() ?
Then you would do: $form_state->getFormObject()->getEntity()*
* Note that getEntity() would be specific to entity forms, and type hinting would not work for it, but it's better than what is available now.
Remaining tasks
Decide on method name, implement it.
User interface changes
API changes
$form_state['controller'] will go away, $form_state['build_info']['callback_object'] will probably still work. We could even consider to keep support for $form_state['controller'] if not having an API change is more important than getting rid of "entity controller". But I think not :)
Comment | File | Size | Author |
---|---|---|---|
#17 | form-state-controller-2313931-17-interdiff.txt | 763 bytes | Berdir |
#17 | form-state-controller-2313931-17.patch | 25.6 KB | Berdir |
#17 | form-state-controller-2313931-17.patch | 25.6 KB | Berdir |
#14 | form-state-controller-2313931-14-interdiff.txt | 10.07 KB | Berdir |
#14 | form-state-controller-2313931-14.patch | 25.6 KB | Berdir |
Comments
Comment #1
BerdirComment #2
tim.plunkettBoth $form_state['controller'] and $form_state['build_info']['callback_object'] are new to D8, so I don't think we have to worry about BC there.
I vote for getFormObject().
Comment #3
xjmgetFormObject()
sounds like what we should name the getter on FormState that gets $form when we convert it to an object in D9. ;) So I think that would confuse.Comment #4
tim.plunkettComment #5
BerdirI don't think we should worry about 9.x here, that is very very far away and there are only few reasons to use it, so if that would really happen (and I'm not sure $form/render arrays will become render objects in 9.x) we can just rename this.
I'm +1 on getFormObject() too.
Comment #6
BerdirHere is a first patch that removes $form_state['controller']. This will likely fail without #2309323: Allow #submit and #validate to be specified as methods of the form object, as that removes the need for the hacky re-assignment of $form_state['controller'] that I removed here.
Comment #8
xjmI still think getFormObject() sounds like it would get a... form... object. The bit about D9 was a joke; I just think that the name doesn't explain what it does. It's more like a getObjectThisFormIsEditing() but that's not exactly a good name either so I guess disregard my opinion if everyone else thinks that's a good name. :)
Comment #9
BerdirAh, maybe there is a small misunderstanding here :) This is not "getObjectThisFormIsEditing()", it returns the object that is defining the form. getFormObject()->getEntity() would be getObjectThisFormIsEditing(). (Except that this concept doesn't exist outside of entity forms, as a single form can be about no or many objects).
We have no other name for whatever implements FormInterface. The class docblock for FormInterface is just "Provides an interface for a Form.". and getForm() would be more misleading than getFormObject() IMHO.
We already use getFormObject() in some cases, see FormController::getFormObject() as an example. So I think this is as consistent with HEAD as it can get.
Comment #10
BerdirRe-roll.
Comment #12
BerdirComment #13
tim.plunkett?!
This seems like a useless function.
Comment #14
BerdirYep, removed that function, only problem is that we lose the right type hint for getEntity()/getFormLangcode(), which is the only thing it is used for.
Comment #15
tim.plunkettJust wanted to say once again: Yay!
Thanks for addressing my feedback.
Comment #17
BerdirMixed up $form_state/$form_object.
Comment #19
tim.plunkettComment #20
webchickNice clean-up.
Committed and pushed to 8.x. Thanks!