Problem/Motivation
API page: https://api.drupal.org/api/drupal/core%21lib%21Drupal%21Core%21Form%21Fo...
An example for programmatically registering a new user via the user registration form is given on the FormBuilder::submitForm API page:
// register a new user
$form_state = new FormState();
$values['name'] = 'robo-user';
$values['mail'] = 'robouser@example.com';
$values['pass']['pass1'] = 'password';
$values['pass']['pass2'] = 'password';
$values['op'] = t('Create new account');
$form_state->setValues($values);
\Drupal::formBuilder()->submitForm('user_register_form', $form_state);I've found the example to be not working.
This is because a form ID is being sent to submitForm function instead of a form object. A related issue has been attached which captures this need for a valid parameter required by FormBuilder::submitForm function.
Remaining tasks
- Remove/correct the example from the API documentation page.
| Comment | File | Size | Author |
|---|---|---|---|
| #17 | 2747273-17.patch | 1.1 KB | quietone |
| #4 | 2747273-4.patch | 880 bytes | mayurjadhav |
Comments
Comment #2
talhaparacha commentedComment #3
cilefen commentedComment #4
mayurjadhav commentedTry this one, may be this will work.
Comment #5
talhaparacha commentedChecked the patch @ 4 but it's still not working. Actually, to me it seems that the example will not work as long as we keep passing a form ID (i.e. user_register_form) to the FormBuilder::submitForm function. Please see the related issue for more info on this specific parameter requirement.
Comment #17
quietone commentedI wasn't able to get this code working. There is an existing usage in core in \Drupal\user\Form\UserMultipleCancelConfirm::submitForm which I used as a guide but could not successfully register a new user.
I asked in #bugsmash where larowlan suggested using a settings form instead of an entity form. I did that and it worked so I made a patch with that example.
Comment #18
quietone commentedMeant to change this to NR and tag
Comment #19
larowlanComment #20
larowlanCross post
This looks good to me.
Comment #21
quietone commentedComment #22
quietone commentedJust to be sure, started tests on 10.0.x
edit: fix typo.
Comment #23
alexpottCommitted and pushed d06b0a8f29 to 10.0.x and 635d9e02b4 to 9.5.x and 40c2597c83 to 9.4.x. Thanks!
Backported to 9.4.x as this is a documentation fix.