I'm creating a 3-step (3-page) form in Drupal 8. One property I use for all three pages is $continue - which triggers an exit process (redirect) if an error condition is encountered, or allows the process to continue through all three pages. Also, there is a node created during submitForm() in step 1, and the node id is needed in steps 2 and 3.

I'm struggling with exactly how to pass data of that nature through the multiple steps. Initially, the form is built, and properties can be initialized in the constructor. But after the user submits the form, that's an entirely new request, right? So doesn't the form class have to be instantiated again before the submitForm() method is invoked? If so, it doesn't seem like properties could persist from buildForm() to submitForm() and back to the buildForm() for step 2?

In Drupal 7, I had to save all values I wanted to persist into $form_state. Do I still need to do the same in Drupal 8?

1 Answer 1


Yes, do the same in D8, use $form_state. There is one caveat though. The form object and form state doesn't persist for the original form build. This was removed in the early stages of Drupal 8, because the form cache table was growing too fast. Since then forms are only cached after the first rebuild.

Normally this isn't a problem. If you store for example the form step in $form_state and it is initialized again with 1, but you have a form action triggered incrementing it to 2, your form then can use the cached $form_state to move back and forth.

This doesn't work however for data no longer available in later rebuilds. In this case use a hidden form value which will be cached together with the rendered form. Add the correct cache metadata for this value so that the rendered form can be invalidated when the value is changed.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

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