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.
Follow-up to #2652814: Allow the use of callbacks for element value callbacks and batch API redirect callbacks
Problem/Motivation
The form API is still stuck with using global functions as callbacks:
$function = $form['whatever callback'];
$result = $function($form, $form_state);
This inhibits the usage of proper callbacks and makes mixing OOP with D7 a complete mess.
Proposed resolution
Change the way these callbacks are invoked.
Remaining tasks
- In drupal_retrieve_form change
$form_state['build_info']['base_form_id'] = $callback;
to$form_state['build_info']['base_form_id'] = isset($form_definition['base_form_id']) ? $form_definition['base_form_id'] : $callback;
- In drupal_retrieve_form change
function_exists($form_state['wrapper_callback'])
tois_callable($form_state['wrapper_callback'])
- In drupal_get_form $form_id param doxygen add a blurb about hook_forms can be used to define forms in classes
- In hook_forms mention that callback can be any callable so forms can be defined in classes.
- In hook_forms doxygen add base_form_id as an option when callable is not a function name.
User interface changes
None
API changes
Several functions can now be any callable and hook_forms gains the optional base_form_id.
Data model changes
None
Comment | File | Size | Author |
---|---|---|---|
#4 | 2652814--manual-testing--pass-do-not-commit.patch | 2.49 KB | Fabianx |
#4 | 2652814--manual-testing--fail.patch | 933 bytes | Fabianx |
#3 | allow_the_use_of-2760609-2.patch | 5.08 KB | Fabianx |
Comments
Comment #3
Fabianx CreditAttribution: Fabianx as a volunteer and at Tag1 Consulting commentedSome credit to chx for the idea and help on the implementation.
Unfortunately hook_forms() is 100% untested in Drupal 7.
Comment #4
Fabianx CreditAttribution: Fabianx as a volunteer and at Tag1 Consulting commentedHere is a fail and pass patch with manually changing hook_forms() in trigger, which is not for commit.
Comment #5
Fabianx CreditAttribution: Fabianx as a volunteer and at Tag1 Consulting commentedExamples show that this needs works and tests. Moving to 7.60.
Comment #8
Fabianx CreditAttribution: Fabianx as a volunteer and at Tag1 Consulting commentedComment #9
Fabianx CreditAttribution: Fabianx as a volunteer and at Tag1 Consulting commentedI was again confused as it would not pass on PHP 5.3 in the first place ...
Comment #10
stefan.r CreditAttribution: stefan.r commented#2652814: Allow the use of callbacks for element value callbacks and batch API redirect callbacks and this issue can share a CR
ID
wizard-like
Comment #12
stefan.r CreditAttribution: stefan.r commentedCommitted and pushed to 7.x, thanks!
Comment #13
David_Rothstein CreditAttribution: David_Rothstein as a volunteer commentedThis got committed without any issue credits (I think the mysterious drupal.org bug struck again) so I'm going to roll this back and then recommit so people get credit in the commit message.
Comment #15
David_Rothstein CreditAttribution: David_Rothstein as a volunteer commentedWe are down to only two of these is_callable() patches in Drupal 7.50 (this one and #2747679: Ajax form callbacks can only be global functions).... need to think about the best way to do a change notice for that.
This one is actually different in that because the existing code already did a call_user_func_array(), you can actually do this on any PHP version (not just 5.4 and higher) and it should work.
Comment #16
David_Rothstein CreditAttribution: David_Rothstein as a volunteer commentedI took a stab at a change record here, for this issue and the other one combined: https://www.drupal.org/node/2761169
Comment #18
Fabianx CreditAttribution: Fabianx as a volunteer and at Tag1 Consulting commented