Multistep form using webform and submit data in a custom table
make a custom table using an install file

Code With Travel
2 min readJan 22, 2020

--

Step 1:

Download the Webform module.

Enable Webform Module.

Step 2:

Go to structure/webform and add a webform

Step 3:

Add 2 wizard page named contact details and personal Info. After that added element as per the below image.

Now make one custom module with name loremipsum and go to loremipsum.install file. We will use hook_schema() to make a custom table for a module and save webform data into it.

Put the following code in loremipsum.install file.

<?phpfunction loremipsum_schema() {
$schema['loremipsum'] = array(
'fields' => array(
'id'=>array(
'type'=>'serial',
'not null' => TRUE,
),
'name'=>array(
'type' => 'varchar',
'length' => 40,
'not null' => TRUE,
),
'mobilenumber'=>array(
'type' => 'varchar',
'length' => 40,
'not null' => TRUE,
),
'email'=>array(
'type' => 'varchar',
'length' => 40,
'not null' => TRUE,
),
'age'=>array(
'type' => 'varchar',
'length' => 25,
'not null' => TRUE,
),
'gender'=>array(
'type' => 'varchar',
'length' => 40,
'not null' => TRUE,
),
'website'=>array(
'type' => 'varchar',
'length' => 25,
'not null' => TRUE,
),
),
'primary key' => array('id'),
);
return $schema;}

After this in loremipsum.module file we will add one hook_form_alter and also define the submit handler.

For this put the following code into loremipsum.module file.

function loremipsum_form_alter(&$form, $form_state, $form_id){
if($form['#form_id'] == "webform_submission_multistep_webfrom_demo_add_form"){
$form['actions']['submit']['#submit'][] = 'webform_submission_multistep_webfrom_demo_add_form';
}
}
function webform_submission_multistep_webfrom_demo_add_form(&$form, $form_state) {
$field=$form_state->getValues();
$name=$field['name'];
$number=$field['phone'];
$email=$field['email'];
$age=$field['age'];
$gender=$field['gender'];
$field = array(
'name' => $name,
'mobilenumber' => $number,
'email' => $email,
'age' => $age,
'gender' => $gender,
'website' => ''
);
$query = \Drupal::database();
$query ->insert('loremipsum')
->fields($field)
->execute();
drupal_set_message("succesfully saved");
}

For code and module go to https://github.com/stemword/drupal8-module-boilerplate.

--

--

No responses yet