Problem/Motivation
API page: https://api.drupal.org/api/drupal/core%21lib%21Drupal%21Core%21Form%21Fo...
The first parameter $form_arg for the FormBuilder::submitForm function is documented as:
\Drupal\Core\Form\FormInterface|string $form_arg: A form object to use to build the form, or the unique string identifying the desired form. If $form_arg is a string and a function with that name exists, it is called to build the form array.
Upon looking at the source code of the function, I've found out that the parameter cannot be a "unique string identifying the desired form". Because if it is a string, then the FormBuilder::getFormId function throws this exception:
if (!is_object($form_arg) || !($form_arg instanceof FormInterface)) {
throw new \InvalidArgumentException("The form argument $form_arg is not a valid form.");
}Proposed resolution
The documentation for the parameter should be changed to:
\Drupal\Core\Form\FormInterface|string $form_arg: The value must be one of the following:
- The name of a class that implements \Drupal\Core\Form\FormInterface.
- An instance of a class that implements \Drupal\Core\Form\FormInterface.
Remaining tasks
- Update documentation.
| Comment | File | Size | Author |
|---|---|---|---|
| #3 | correction-documentation-formbuilder-2747269-3.patch | 1.04 KB | chishah92 |
Comments
Comment #2
chishah92 commentedComment #3
chishah92 commentedUpdating the documentation in code base in FormBuilderInterface.php . I hope this might be helpful too.
Thanks
~Chirag
Comment #4
talhaparacha commentedThank you for the patch. Looks good to me but let's have others review it too.
Comment #5
ccjjmartin commentedI agree this documentation is out of date and I agree with the changes in #3. As three people agree I am marking this RTBC.
At first, I thought to myself why are we removing the documentation about the ability to use a string reference to a function name. But, if you follow the $form_arg parameter as it is passed through FormBuilder::submitForm into FormBuilder::getFormId you will notice the lines of code you mentioned in the description. Found here on GitHub for anyone jumping in after me:
Documentation lines requested to be changed:
https://github.com/drupal/drupal/blob/1842e127fece2d4ae29c97b9410bb6c062...
FormBuilder::submitForm only use of $form_arg
https://github.com/drupal/drupal/blob/1842e127fece2d4ae29c97b9410bb6c062...
FormBuilder::getFormId which tests the $form_arg parameter:
https://github.com/drupal/drupal/blob/1842e127fece2d4ae29c97b9410bb6c062...
Great find and fix.
Comment #8
catchCommitted/pushed to 8.2.x and cherry-picked to 8.1.x. Thanks!