Skip to content

Commit e494858

Browse files
author
James Davidson
committed
Initial commit of multistep form for Drupal 8.
1 parent 360bc92 commit e494858

File tree

5 files changed

+280
-0
lines changed

5 files changed

+280
-0
lines changed

multistep.info.yml

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
name: Multistep
2+
description: Multistep form Drupal 8
3+
type: module
4+
core: '8.x'

multistep.module

Whitespace-only changes.

multistep.routing.yml

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
multistep.singleurl:
2+
path: '/multistep/singleurl'
3+
defaults:
4+
_title: 'Multistep Form'
5+
_controller: '\Drupal\multistep\Controller\Multistep::singleurl'
6+
requirements:
7+
_permission: 'access content'

src/Controller/Multistep.php

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
<?php
2+
3+
namespace Drupal\multistep\Controller;
4+
5+
use Drupal\Core\Controller\ControllerBase;
6+
use Drupal\Core\Form\FormBuilderInterface;
7+
use Symfony\Component\DependencyInjection\ContainerInterface;
8+
9+
/**
10+
* Create a multistep form
11+
*/
12+
class Multistep extends ControllerBase {
13+
14+
/**
15+
* The form builder.
16+
*
17+
* @var \Drupal\Core\Form\FormBuilderInterface
18+
*/
19+
protected $formBuilder;
20+
21+
/**
22+
* {@inheritdoc}
23+
*/
24+
public static function create(ContainerInterface $container) {
25+
return new static(
26+
$container->get('form_builder')
27+
);
28+
}
29+
30+
/**
31+
* Add formbuilder
32+
*/
33+
public function __construct(FormBuilderInterface $form_builder) {
34+
$this->formBuilder = $form_builder;
35+
}
36+
37+
/**
38+
* Return single URL multistep form.
39+
*/
40+
public function singleurl() {
41+
return $this->formBuilder->getForm('\Drupal\multistep\Form\Singleurl');
42+
}
43+
44+
}

src/Form/Singleurl.php

Lines changed: 225 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,225 @@
1+
<?php
2+
3+
namespace Drupal\multistep\Form;
4+
5+
use Drupal\Core\Form\FormBase;
6+
use Drupal\Core\Form\FormStateInterface;
7+
8+
/**
9+
* Provides multistep single url form.
10+
*/
11+
class Singleurl extends FormBase {
12+
13+
/**
14+
* Current step
15+
* @var int
16+
*/
17+
protected $step = '';
18+
19+
/**
20+
* Steps for the form
21+
* @var array
22+
*/
23+
protected $steps = array();
24+
25+
/**
26+
* Store values between steps
27+
* @var array
28+
*/
29+
protected $multistep_values = array();
30+
31+
/**
32+
* Is the form finished?
33+
* @var bool
34+
*/
35+
protected $complete = FALSE;
36+
37+
/**
38+
* Singleurl constructor.
39+
*/
40+
public function __construct() {
41+
$this->steps = array(
42+
1 => 'room',
43+
2 => 'service',
44+
3 => 'review',
45+
);
46+
47+
if (!$this->step) {
48+
$this->step = $this->steps[1];
49+
}
50+
}
51+
52+
/**
53+
* {@inheritdoc}
54+
*/
55+
public function getFormId() {
56+
return 'multistep_singleurl';
57+
}
58+
59+
/**
60+
* {@inheritdoc}
61+
*/
62+
public function buildForm(array $form, FormStateInterface $form_state) {
63+
64+
// Set values from all step submissions.
65+
$form_state->setValues($this->multistep_values);
66+
67+
// Get the step method
68+
$method = 'step' . ucwords($this->step);
69+
70+
if (method_exists($this, $method)) {
71+
$this->{$method}($form, $form_state);
72+
}
73+
else {
74+
exit('You have not created a method ' . $method);
75+
}
76+
77+
return $form;
78+
}
79+
80+
/**
81+
* First step of form.
82+
*
83+
* @param array $form
84+
* @param \Drupal\Core\Form\FormStateInterface $form_state
85+
*/
86+
private function stepRoom(array &$form, FormStateInterface $form_state) {
87+
$rating = $form_state->getValue('room_rating');
88+
89+
$form['rate_the_room']['room_rating'] = array(
90+
'#type' => 'radios',
91+
'#title' => 'How would you rate the room you stayed in?',
92+
'#required' => TRUE,
93+
'#options' => array(1 => 1, 2 => 2, 3 => 3, 4 => 4, 5 => 5),
94+
'#default_value' => $rating ? $rating : NULL,
95+
);
96+
97+
$form['next'] = array(
98+
'#type' => 'submit',
99+
'#value' => $this->t('Next'),
100+
);
101+
}
102+
103+
/**
104+
* Second step of form
105+
*
106+
* @param array $form
107+
* @param \Drupal\Core\Form\FormStateInterface $form_state
108+
*/
109+
private function stepService(array &$form, FormStateInterface $form_state) {
110+
$rating = $form_state->getValue('service_rating');
111+
112+
$form['rate_the_service']['service_rating'] = array(
113+
'#type' => 'radios',
114+
'#title' => 'How would you rate our service?',
115+
'#required' => TRUE,
116+
'#options' => array(1 => 1, 2 => 2, 3 => 3, 4 => 4, 5 => 5),
117+
'#default_value' => $rating ? $rating : NULL,
118+
);
119+
120+
$form['actions']['back'] = array(
121+
'#type' => 'submit',
122+
'#value' => $this->t('Back'),
123+
'#id' => 'back',
124+
'#validate' => array(),
125+
'#limit_validation_errors' => array(),
126+
'#submit' => array(),
127+
);
128+
129+
$form['actions']['next'] = array(
130+
'#type' => 'submit',
131+
'#value' => $this->t('Next'),
132+
'#id' => 'next',
133+
);
134+
135+
$form['#submit'] = array(
136+
137+
);
138+
}
139+
140+
/**
141+
* Review step of form
142+
*
143+
* @param array $form
144+
* @param \Drupal\Core\Form\FormStateInterface $form_state
145+
*/
146+
private function stepReview(array &$form, FormStateInterface $form_state) {
147+
148+
$room = $form_state->getValue('room_rating');
149+
$service = $form_state->getValue('service_rating');
150+
$email = $form_state->getValue('email');
151+
152+
$form['review']['review_details'] = array(
153+
'#type' => 'table',
154+
'#rows' => array(
155+
array('Room Rating', $room),
156+
array('Service Rating', $service),
157+
),
158+
);
159+
160+
$form['review']['email'] = array(
161+
'#type' => 'email',
162+
'#title' => $this->t('Email Address'),
163+
'#description' => $this->t('Enter your email address and you\'ll be entered into a prize draw'),
164+
'#default_value' => $email ? $email : NULL,
165+
);
166+
167+
$form['actions']['back'] = array(
168+
'#type' => 'submit',
169+
'#value' => $this->t('Back'),
170+
'#id' => 'back',
171+
'#validate' => array(),
172+
'#limit_validation_errors' => array(),
173+
'#submit' => array(),
174+
);
175+
176+
$form['actions']['next'] = array(
177+
'#type' => 'submit',
178+
'#value' => $this->t('Next'),
179+
'#id' => 'next',
180+
);
181+
}
182+
183+
/**
184+
* @param array $form
185+
* @param \Drupal\Core\Form\FormStateInterface $form_state
186+
*/
187+
public function submitForm(array &$form, FormStateInterface $form_state) {
188+
189+
$this->multistep_values = $form_state->getValues() + $this->multistep_values;
190+
$step_key = array_search($this->step, $this->steps);
191+
192+
// Get the step method
193+
$method = 'step' . ucwords($this->step) . 'Submit';
194+
195+
if (method_exists($this, $method)) {
196+
$this->{$method}($form, $form_state);
197+
}
198+
199+
if ($this->complete) {
200+
return;
201+
}
202+
203+
if ($form_state->getTriggeringElement()['#id'] == 'back') {
204+
// Move to previous step
205+
$this->step = $this->steps[$step_key - 1];
206+
}
207+
else {
208+
// Move to next step.
209+
$this->step = $this->steps[$step_key + 1];
210+
}
211+
212+
$form_state->setRebuild();
213+
}
214+
215+
/**
216+
* @param array $form
217+
* @param \Drupal\Core\Form\FormStateInterface $form_state
218+
*/
219+
public function stepReviewSubmit(array &$form, FormStateInterface &$form_state) {
220+
drupal_set_message($this->t('Thank you for your submission'));
221+
$form_state->setRedirect('<front>');
222+
$this->complete = TRUE;
223+
}
224+
225+
}

0 commit comments

Comments
 (0)