You are here

public function AutosaveFormBuilder::rebuildForm in Autosave Form 8

Constructs a new $form from the information in $form_state.

This is the key function for making multi-step forms advance from step to step. It is called by self::processForm() when all user input processing, including calling validation and submission handlers, for the request is finished. If a validate or submit handler set $form_state->isRebuilding() to TRUE, and if other conditions don't preempt a rebuild from happening, then this function is called to generate a new $form, the next step in the form workflow, to be returned for rendering.

Ajax form submissions are almost always multi-step workflows, so that is one common use-case during which form rebuilding occurs.

Parameters

string $form_id: The unique string identifying the desired form. If a function with that name exists, it is called to build the form array.

\Drupal\Core\Form\FormStateInterface $form_state: The current state of the form.

array|null $old_form: (optional) A previously built $form. Used to retain the #build_id and #action properties in Ajax callbacks and similar partial form rebuilds. The only properties copied from $old_form are the ones which both exist in $old_form and for which $form_state->getRebuildInfo()['copy'][PROPERTY] is TRUE. If $old_form is not passed, the entire $form is rebuilt freshly. 'rebuild_info' needs to be a separate top-level property next to 'build_info', since the contained data must not be cached.

Return value

array The newly built form.

Overrides FormBuilder::rebuildForm

See also

self::processForm()

File

src/Form/AutosaveFormBuilder.php, line 160

Class

AutosaveFormBuilder
Provides form building and processing with AutosaveForm enabled.

Namespace

Drupal\autosave_form\Form

Code

public function rebuildForm($form_id, FormStateInterface &$form_state, $old_form = NULL) {
  $this
    ->restoreAutosavedState($form_id, $form_state);
  return parent::rebuildForm($form_id, $form_state, $old_form);
}