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.

Support from Acquia helps fund testing for Drupal Acquia logo

Comments

Status: Needs review » Needs work
Issue tags: -API addition

The last submitted patch, drupal_get_callback_form.patch, failed testing.

tim.plunkett’s picture

Status: Needs work » Needs review
Issue tags: +API addition, +Needs backport to D7

drupal_get_callback_form.patch queued for re-testing.

damiankloip’s picture

+++ b/core/includes/form.incundefined
@@ -138,6 +138,28 @@ function drupal_get_form($form_id) {
+ * @param callable $callback
+ *   A callback to be used for building the form.

Maybe 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?

tim.plunkett’s picture

Well 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)

damiankloip’s picture

Yeah, 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.

dawehner’s picture

This looks pretty good. Should we provide some test for that?

tim.plunkett’s picture

FileSize
6.4 KB

Yes, here is a test. Not bothering to upload a tests only, since it will just be "function does not exist"...

chx’s picture

I dunno, we used http://api.drupal.org/api/drupal/core%21modules%21system%21system.api.ph... before for specifying callbacks...

Status: Needs review » Needs work

The last submitted patch, form-1903176-7.patch, failed testing.

tim.plunkett’s picture

Status: Needs work » Needs review

hook_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.

tim.plunkett’s picture

FileSize
870 bytes
6.4 KB

Stupid mistake.

Status: Needs review » Needs work
Issue tags: -API addition, -Needs backport to D7

The last submitted patch, form-1903176-11.patch, failed testing.

tim.plunkett’s picture

Status: Needs work » Needs review
Issue tags: +API addition, +Needs backport to D7

#11: form-1903176-11.patch queued for re-testing.

Crell’s picture

Status: Needs review » Reviewed & tested by the community

This seems like a reasonable small cleanup. If nothing else it shortens a few code blocks elsewhere by reducing duplication.

catch’s picture

Version: 8.x-dev » 7.x-dev
Status: Reviewed & tested by the community » Patch (to be ported)
Issue tags: +Needs change record

Looks fine to me as well Committed/pushed to 8.x.

Seems reasonable to backport.

Will also need a change notice.

chx’s picture

Version: 7.x-dev » 8.x-dev
Category: task » bug
Status: Patch (to be ported) » Active
Issue tags: -Needs backport to D7, -Needs change record

Hell no. Sorry for dropping the ball but step one is rolling this back and making something better.

chx’s picture

  1. drupal_get_form($this). Make an interface. Methods:
    1. formGetId. Default to get_class($this) I think.
    2. form
    3. formValidate
    4. formSubmit
  2. Multiple forms per class? Should really be rare, but drupal_get_form(array($this, 'foo')). Methods:
    1. formFooGetId. Default to get_class($this) I think.
    2. formFoo
    3. formFooValidate
    4. formFooSubmit
Crell’s picture

Hm. 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).

dawehner’s picture

Version: 8.x-dev » 7.x-dev
Category: bug » task
Status: Active » Patch (to be ported)
Issue tags: +Needs backport to D7, +Needs change record

http://drupal.org/node/1910136 is a small change notice.

chx’s picture

Version: 7.x-dev » 8.x-dev
Category: task » bug
Status: Patch (to be ported) » Active
Issue tags: -Needs backport to D7, -Needs change record

I talked to Daniel and this was an x-post and I unpublished the change notice for now.

tim.plunkett’s picture

Assigned: tim.plunkett » Unassigned
Category: bug » task
Status: Active » Fixed

I 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.

Automatically closed -- issue fixed for 2 weeks with no activity.

Anonymous’s picture

Issue summary: View changes

Updated issue summary.