Support for Drupal 7 is ending on 5 January 2025—it’s time to migrate to Drupal 10! Learn about the many benefits of Drupal 10 and find migration tools in our resource center.
Thanks to call_user_func_array(), forms can be built using any callable, not just functions.
However, the steps to do this are rather confusing, and it's hard to find examples of how to do it.
I think another drupal_*_form() function is a small price to pay for the utility of this, especially in the D8 world of increasing OO.
Since it is a straight API addition, it should even be backportable.
Comment | File | Size | Author |
---|---|---|---|
#11 | form-1903176-11.patch | 6.4 KB | tim.plunkett |
#11 | interdiff.txt | 870 bytes | tim.plunkett |
#7 | form-1903176-7.patch | 6.4 KB | tim.plunkett |
drupal_get_callback_form.patch | 3.28 KB | tim.plunkett | |
Comments
Comment #2
tim.plunkettdrupal_get_callback_form.patch queued for re-testing.
Comment #3
damiankloip CreditAttribution: damiankloip commentedMaybe this param should be $callable instead? Just for clarity? or maybe not, I'm not sure on that :) Atleast maybe this could reference that the parameter is the same as that of call_user_func_array?
Comment #4
tim.plunkettWell the 'callable' type hint is a PHP thing (see http://php.net/manual/en/language.types.callable.php or http://php.net/manual/en/function.is-callable.php) and http://php.net/call_user_func_array just happens to accept those.
I named it $callback because that's what the $form_state key is. Maybe that should be changed to 'callable', but I think that consistency is more important here.
(We don't often see
@param string $string
)Comment #5
damiankloip CreditAttribution: damiankloip commentedYeah, I guess people can look up callables if they need to.
Consistency is good. I think the patch is just fine how it is now.
Comment #6
dawehnerThis looks pretty good. Should we provide some test for that?
Comment #7
tim.plunkettYes, here is a test. Not bothering to upload a tests only, since it will just be "function does not exist"...
Comment #8
chx CreditAttribution: chx commentedI dunno, we used http://api.drupal.org/api/drupal/core%21modules%21system%21system.api.ph... before for specifying callbacks...
Comment #10
tim.plunketthook_forms() does not help with non-static methods because you do not have the object on hand.
Consider field_ui_field_overview(), which has to create a new object for whatever entity type it is passed, and then specify that object and its method as the callback.
Comment #11
tim.plunkettStupid mistake.
Comment #14
tim.plunkett#11: form-1903176-11.patch queued for re-testing.
Comment #16
Crell CreditAttribution: Crell commentedThis seems like a reasonable small cleanup. If nothing else it shortens a few code blocks elsewhere by reducing duplication.
Comment #17
catchLooks fine to me as well Committed/pushed to 8.x.
Seems reasonable to backport.
Will also need a change notice.
Comment #18
chx CreditAttribution: chx commentedHell no. Sorry for dropping the ball but step one is rolling this back and making something better.
Comment #19
chx CreditAttribution: chx commenteddrupal_get_form($this)
. Make an interface. Methods:formGetId
. Default toget_class($this)
I think.form
formValidate
formSubmit
drupal_get_form(array($this, 'foo'))
. Methods:formFooGetId
. Default toget_class($this)
I think.formFoo
formFooValidate
formFooSubmit
Comment #20
Crell CreditAttribution: Crell commentedHm. If we were to go with chx's proposal, I'd suggest restricting it to one form per object. Multiple forms just gets messy.
Note that allowing an object there would mean some forms could not be used as declared callbacks (from menu items/routes, etc.) as those need to be static strings. That may be OK, but does create two classes of form (no pun intended).
Comment #21
dawehnerhttp://drupal.org/node/1910136 is a small change notice.
Comment #22
chx CreditAttribution: chx commentedI talked to Daniel and this was an x-post and I unpublished the change notice for now.
Comment #23
tim.plunkettI do not think that this should be backported after all.
To follow-up with chx's request in #19, I've opened #1913084: Introduce a Form interface for building, validating, and submitting forms.
I'm republishing the change notice.
Comment #24.0
(not verified) CreditAttribution: commentedUpdated issue summary.