1

I have a form that allows users to upload a CSV file. Upon submit, I would like to show a confirmation form ("Are you sure you want to import this file?").

However, the Drupal 8 examples for ConfirmFormBase seem to only focus on building the confirmation form without actually showing how one gets to that form from another form.

Can someone provide an example of a regular form that then works in conjunction with a confirmation form?

2

As you've already discovered, ConfigFormBase just provides some buttons, handling for messages, and submit/cancel logic. On its own, it's not designed to be used as the sort of multistep form you're looking for.

Getting that functionality is pretty easy though; you could use the logic in Drupal\config\Form\ConfigSingleImportForm as a rough example. Basically you just handle the multistep part of it yourself. For example:

protected $step = 1;

public function buildForm(array $form, FormStateInterface $form_state) {
  // When this is the confirmation step fall through to the confirmation form.
  if ($this->step === 2) {
    return parent::buildForm($form, $form_state);
  }

  // Build and return your upload form as normal, but don't call the parent method.
}

public function validateForm(array &$form, FormStateInterface $form_state) {
  // The confirmation step needs no additional validation.
  if ($this->step === 2) {
    return;
  }

  // Perform any additional validation 
}

public function submitForm(array &$form, FormStateInterface $form_state) {
  if ($this->step === 1) {
    $form_state->setRebuild();
    $this->step = 2;
    return;
  }

  // Extract rows from CSV and set batch to import here
}
  • I tried implementing this, but I ran into problem in the submitForm function in step 2, $form_state did not have the values saved anymore from the step 1 form. I fixed this by adding this line in submitForm after line $this->step = 2;: $this->data = $form_state->getValues(); That way, in step 2 you can use $this->data to reference all the values from step 1. – Risse Jan 19 at 8:47

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.