0

I have a FormBase plugin for an admin configuration page. My desired behavior is to select a series of checkboxes and when you submit the form based on those values a new set of fields will show. However I'm not sure how to build the form after submitting the form with new values.

Here's my current buildForm function

    $form['pages'] = [
        '#type' => 'checkboxes',
        '#title' => 'Pages to clone:',
        '#options' => [
            'about' => 'About',
            'home' => 'Home',
            'blog' => 'Blog',
        ],
    ];

    $form['actions'] = [
        '#type' => 'actions',
    ];
    $form['actions']['submit'] = [
        '#type' => 'submit',
        '#value' => $this->t('Submit'),
    ];

(Sidenote: Not sure if or why the last bit with the 'actions' is necessary) The form is build fine and in my submitForm function I have

public function submitForm(array &$form, FormStateInterface $form_state) {
    $form_state->set('pages', $form_state->getValue());
    $form_state->set('test_parameter', 'test_value');
}

However when I add a var_dump($form_state) to the buildForm function and submit the form like so

public function buildForm(array $form, FormStateInterface $form_state) { var_dump($form_state); $form_state->set('pages', $form_state->getValue()); $form_state->set('test_parameter', 'test_value'); }

I get some information about the form_state but nothing about the parameters I set.

object(Drupal\Core\Form\FormState)#599 (33) {
["complete_form":protected]=> NULL ["build_info":protected]=>
array(4) { ["args"]=> array(0) { } ["files"]=> array(0) { } ["callback_object"]=> object(Drupal\my_plugin\Form\PageCloner)#600 (10) { ["number":protected]=> int(1) ["requestStack":protected]=> NULL ["configFactory":protected]=> NULL ["routeMatch":protected]=> NULL ["_serviceIds":protected]=> array(0) { } ["linkGenerator":protected]=> NULL ["loggerFactory":protected]=> NULL ["redirectDestination":protected]=> NULL ["stringTranslation":protected]=> NULL ["urlGenerator":protected]=> NULL } ["form_id"]=> string(9) "page_pack" } ["rebuild_info":protected]=> array(0) { } ["rebuild":protected]=> bool(false)
["invalidToken":protected]=> bool(false) ["response":protected]=> NULL ["redirect":protected]=> NULL ["no_redirect":protected]=>
NULL ["method":protected]=> string(4) "POST"
["requestMethod":protected]=> string(3) "GET"
["cache":protected]=> bool(false) ["no_cache":protected]=> NULL ["values":protected]=> array(0) { }
["cleanValueKeys":protected]=> array(4) { [0]=> string(7) "form_id" [1]=> string(10) "form_token" [2]=> string(13) "form_build_id" [3]=> string(2) "op" } ["input":protected]=> array(0) { } ["always_process":protected]=> NULL ["must_validate":protected]=> NULL ["programmed":protected]=> bool(false)
["programmed_bypass_access_check":protected]=> bool(true)
["process_input":protected]=> NULL ["submitted":protected]=>
bool(false) ["executed":protected]=> bool(false)
["triggering_element":protected]=> NULL
["has_file_element":protected]=> NULL ["groups":protected]=>
array(0) { } ["storage":protected]=> array(0) { }
["buttons":protected]=> array(0) { } ["temporary":protected]=>
array(0) { } ["validation_complete":protected]=> bool(false)
["errors":protected]=> array(0) { }
["limit_validation_errors":protected]=> NULL
["validate_handlers":protected]=> array(0) { }
["submit_handlers":protected]=> array(0) { } }

What is the best way to get information retrieved in the formSubmit function back to when the buildForm function is fired again after submit? Or is there a better way I'm not considering to create a new form based on these values?

UPDATE: 4k4's solution worked but I ran into another issue where the form state wasn't properly saving. I resolved this by adding

parent::submitForm($form, $form_state);

to the submitForm function before $form_state->setRebuild(); this led to an error saying

Cannot call abstract method Drupal\Core\Form\FormInterface::submitForm()

to resolve that I changed the form from extending FormBase to extending ConfigFormBase instead like so

class MyPlugin extends ConfigFormBase {
1

If you want to rebuild the form with the values of $form_state you have to set the form to be rebuilt after processing:

public function submitForm(array &$form, FormStateInterface $form_state) {
  $form_state->set('test_parameter', 'test_value');
  $form_state->setRebuild();
}

Otherwise the form redirects to a new form build.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.