Early Bird Registration for DrupalCon Portland 2024 is open! Register by 23:59 PST on 31 March 2024, to get $100 off your ticket.
Problem
- Form constructor functions have no way to figure out the original $form_id name for which the form is currently built for.
Goal
- Provide the original $form_id in $form_state['build_info']['form_id'], just like 'base_form_id' is provided there.
Details
- With D7, we introduced the $form_state['build_info'] container, which holds all the build information for a form; e.g., the $args originally passed into drupal_get_form(), the base form ID in case the $form_id does not map directly to a function name and hook_forms() is invoked, etc.
- We totally forgot to record the very basic $form_id itself.
- A form constructor function is invoked directly from drupal_retrieve_form() and only gets $form and $form_state as arguments. The $form_id is not supplied anywhere.
- Only after the form has been constructed, drupal_prepare_form() adds the $form['form_id'] element to the form. Before drupal_prepare_form(), both $form and $form_state are relatively "empty".
- If a form constructor calls into other functions, then those other functions do not have a way to figure out the original $form_id, unless it was explicitly specified by the form constructor somewhere in $form or $form_state.
Related issues
Comment | File | Size | Author |
---|---|---|---|
#11 | drupal.form-state-build-info-formid.11.patch | 1.14 KB | sun |
#6 | drupal8.form-state-build-info-formid-docs.6.patch | 710 bytes | sun |
#4 | drupal.form-state-build-info-formid.4.patch | 608 bytes | sun |
drupal8.form-state-build-info-formid.0.patch | 628 bytes | sun | |
Comments
Comment #1
dwwSeems like an obvious win. Code is clean. Makes sense for this to be the very first thing stuffed into $form_state by this function (I applied the patch and looked at the full context, and there's definitely no more obvious place for this). Bot is happy. RTBC.
Thanks,
-Derek
Comment #2
sunComment #3
tim.plunkettFor those confused (like me) by sun's comment, this was committed to D8 in #1599554-36: Tutorial/guidelines for how to convert variables into configuration
Comment #4
sunIdentical patch.
Comment #5
David_Rothstein CreditAttribution: David_Rothstein commentedThis makes a lot of sense to me, but shouldn't we be documenting this new parameter?
drupal_build_form() has a massive list of $form_state parameters documented, including everything inside 'build_info', so I think we need to list this there too.
Moving back to 8.x for that first, since I think if we're going to add this to the data structure in a stable Drupal 7 release we should make sure it's correctly documented when we do so.
Comment #6
sunDocs for D8.
Comment #7
tim.plunkettLooks good to me.
Comment #8
Dries CreditAttribution: Dries commentedI believe I need to commit both #0 and #6. However, #0 no longer applies, and #6 has the documentation only. Looks like a re-roll may be in order?
Comment #9
sun#0 was committed as part of #1599554: Tutorial/guidelines for how to convert variables into configuration, so only #6 is required for D8.
Comment #10
Dries CreditAttribution: Dries commentedCommitted the documentation to 8.x. Moving back to 7.x for the documentation.
Comment #11
sun#4 was not committed to D7 yet.
Attached is the combined backport.
Comment #12
David_Rothstein CreditAttribution: David_Rothstein commentedThis looks good to me; the only new thing from the previous RTBC patch is the code comment, so moving it back to RTBC and I should be able to commit it in a few days.
Comment #13
David_Rothstein CreditAttribution: David_Rothstein commentedCommitted to 7.x - thanks! http://drupalcode.org/project/drupal.git/commit/7a3d48e