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.
CommentFileSizeAuthor
#17 2747273-17.patch1.1 KBquietone
#4 2747273-4.patch880 bytesmayurjadhav

Comments

talhaparacha created an issue. See original summary.

talhaparacha’s picture

cilefen’s picture

Status: Needs work » Active
mayurjadhav’s picture

Status: Active » Needs review
StatusFileSize
new880 bytes

Try this one, may be this will work.

talhaparacha’s picture

Status: Needs review » Active

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

Version: 8.2.x-dev » 8.3.x-dev

Drupal 8.2.0-beta1 was released on August 3, 2016, which means new developments and disruptive changes should now be targeted against the 8.3.x-dev branch. For more information see the Drupal 8 minor version schedule and the Allowed changes during the Drupal 8 release cycle.

Version: 8.3.x-dev » 8.4.x-dev

Drupal 8.3.0-alpha1 will be released the week of January 30, 2017, which means new developments and disruptive changes should now be targeted against the 8.4.x-dev branch. For more information see the Drupal 8 minor version schedule and the Allowed changes during the Drupal 8 release cycle.

Version: 8.4.x-dev » 8.5.x-dev

Drupal 8.4.0-alpha1 will be released the week of July 31, 2017, which means new developments and disruptive changes should now be targeted against the 8.5.x-dev branch. For more information see the Drupal 8 minor version schedule and the Allowed changes during the Drupal 8 release cycle.

Version: 8.5.x-dev » 8.6.x-dev

Drupal 8.5.0-alpha1 will be released the week of January 17, 2018, which means new developments and disruptive changes should now be targeted against the 8.6.x-dev branch. For more information see the Drupal 8 minor version schedule and the Allowed changes during the Drupal 8 release cycle.

Version: 8.6.x-dev » 8.7.x-dev

Drupal 8.6.0-alpha1 will be released the week of July 16, 2018, which means new developments and disruptive changes should now be targeted against the 8.7.x-dev branch. For more information see the Drupal 8 minor version schedule and the Allowed changes during the Drupal 8 release cycle.

Version: 8.7.x-dev » 8.8.x-dev

Drupal 8.7.0-alpha1 will be released the week of March 11, 2019, which means new developments and disruptive changes should now be targeted against the 8.8.x-dev branch. For more information see the Drupal 8 minor version schedule and the Allowed changes during the Drupal 8 release cycle.

Version: 8.8.x-dev » 8.9.x-dev

Drupal 8.8.0-alpha1 will be released the week of October 14th, 2019, which means new developments and disruptive changes should now be targeted against the 8.9.x-dev branch. (Any changes to 8.9.x will also be committed to 9.0.x in preparation for Drupal 9’s release, but some changes like significant feature additions will be deferred to 9.1.x.). For more information see the Drupal 8 and 9 minor version schedule and the Allowed changes during the Drupal 8 and 9 release cycles.

Version: 8.9.x-dev » 9.1.x-dev

Drupal 8.9.0-beta1 was released on March 20, 2020. 8.9.x is the final, long-term support (LTS) minor release of Drupal 8, which means new developments and disruptive changes should now be targeted against the 9.1.x-dev branch. For more information see the Drupal 8 and 9 minor version schedule and the Allowed changes during the Drupal 8 and 9 release cycles.

Version: 9.1.x-dev » 9.2.x-dev

Drupal 9.1.0-alpha1 will be released the week of October 19, 2020, which means new developments and disruptive changes should now be targeted for the 9.2.x-dev branch. For more information see the Drupal 9 minor version schedule and the Allowed changes during the Drupal 9 release cycle.

Version: 9.2.x-dev » 9.3.x-dev

Drupal 9.2.0-alpha1 will be released the week of May 3, 2021, which means new developments and disruptive changes should now be targeted for the 9.3.x-dev branch. For more information see the Drupal core minor version schedule and the Allowed changes during the Drupal core release cycle.

Version: 9.3.x-dev » 9.4.x-dev

Drupal 9.3.0-rc1 was released on November 26, 2021, which means new developments and disruptive changes should now be targeted for the 9.4.x-dev branch. For more information see the Drupal core minor version schedule and the Allowed changes during the Drupal core release cycle.

quietone’s picture

StatusFileSize
new1.1 KB

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

quietone’s picture

Status: Active » Needs review
Issue tags: +Bug Smash Initiative

Meant to change this to NR and tag

larowlan’s picture

Issue tags: -Bug Smash Initiative
larowlan’s picture

Status: Needs review » Reviewed & tested by the community
Issue tags: +Bug Smash Initiative

Cross post

This looks good to me.

quietone’s picture

Version: 9.4.x-dev » 10.0.x-dev
quietone’s picture

Just to be sure, started tests on 10.0.x

edit: fix typo.

alexpott’s picture

Version: 10.0.x-dev » 9.4.x-dev
Status: Reviewed & tested by the community » Fixed

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

  • alexpott committed d06b0a8 on 10.0.x
    Issue #2747273 by quietone, mayurjadhav, talhaparacha: Example given on...

  • alexpott committed 635d9e0 on 9.5.x
    Issue #2747273 by quietone, mayurjadhav, talhaparacha: Example given on...

  • alexpott committed 40c2597 on 9.4.x
    Issue #2747273 by quietone, mayurjadhav, talhaparacha: Example given on...

Status: Fixed » Closed (fixed)

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