How to pass values from one form to another in Drupal 9

When we are developing multiple forms which are related , there will be a need for passing values between forms. We can achieve this with different ways. Here we are discussing below types of methods where we can pass values between form.

  • Passing values using form action
  • Passing values using session
  • Passing values using configuration

Passing values using form action

Here we are passing value from first form by specifying action url of the first form as url of second form. So values will be posted to second form with out doing anything in first form submit handler.

So here we have a custom module dn_multipleform.

SO we are going to create tow forms in below path.



So below route we have added in routing.yml


So in first form we have fields first name and last name.

We are providing action target as second page url as below.

$form[‘#action’] = Url::fromUri(‘internal:/’ . ‘actionsecondform’)->toString();

See complete code of ActionFirstForm.php

So in second form, we are going to capture the values as same like $_POST[’fname’].

In Drupal you can retrieve those value as below.

Here in second form we are just retrieving the value and displaying on the second form. Complete code of second form provided below.


Passing values using session

Here we are storing values of first form in session variable and retrieving same value in second form.

We have created below forms for the same purpose.



Added below routes in routing.yml

So here in first form submit handler we are saving fname value in session as below.

Then redirect to the second form.

Here firstname is the session variable name and dn_multipleform is the name of our module.

So complete code of first form will be as below.

So in second form retrieve session variable as below.

So fname will be displayed in second form.

See the complete code of second form.


So after submitting page /sessionfirstform you can see first from value is appearing in second form.

Passing values using configuration

Here we are saving value in configuration and retrieving value in second form from configuration. This is the most secured way of passing values between forms.

We are creating below forms for this purposes.



Below routing added for the same

In submit handler of first form , save fname value to configuration as below.

Here dn_multipleform is the name of our custom form.

And then redirect to second form as below.

So complete source code of ConfigFirstForm is provided below.

So in second form we will be retrieving same value as below.

$fname = \Drupal::config(‘dn_multipleform.settings’)->get(‘fname’);

See below complete code of second form ConfigSecondfForm.

All these examples are provided in below sample module.

Download  sample module.

Get Free E-book
Get a free Ebook on Drupal 8 -theme tutorial
I agree to have my personal information transfered to MailChimp ( more information )
if you like this article Buy me a Coffee this will be an ispiration for me to write articles like this.

You may also like...