Newer
Older
Shreya Shetty
committed
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
<?php
namespace Drupal\fapi_example\Form;
use Drupal\Core\Form\FormStateInterface;
/**
* Implements the ajax demo form controller.
*
* This example demonstrates using ajax callbacks to populate the options of a
* color select element dynamically based on the value selected in another
* select element in the form.
*
* @see \Drupal\Core\Form\FormBase
* @see \Drupal\Core\Form\ConfigFormBase
*/
class AjaxAddMore extends DemoBase {
/**
* Form with 'add more' and 'remove' buttons.
*
* This example shows a button to "add more" - add another textfield, and
* the corresponding "remove" button.
*/
public function buildForm(array $form, FormStateInterface $form_state) {
$form['description'] = array(
'#markup' => '<div>' . t('This example shows an add-more and a remove-last button.') . '</div>',
);
$i = 0;
$name_field = $form_state->get('num_names');
$form['#tree'] = TRUE;
$form['names_fieldset'] = [
'#type' => 'fieldset',
'#title' => $this->t('People coming to picnic'),
'#prefix' => '<div id="names-fieldset-wrapper">',
'#suffix' => '</div>',
];
if (empty($name_field)) {
$name_field = $form_state->set('num_names', 1);
}
for ($i = 0; $i < $name_field; $i++) {
$form['names_fieldset']['name'][$i] = [
'#type' => 'textfield',
'#title' => t('Name'),
];
}
$form['actions'] = [
'#type' => 'actions',
];
$form['names_fieldset']['actions']['add_name'] = [
'#type' => 'submit',
'#value' => t('Add one more'),
'#submit' => array('::addOne'),
'#ajax' => [
'callback' => '::addmoreCallback',
'wrapper' => 'names-fieldset-wrapper',
],
];
if ($name_field > 1) {
$form['names_fieldset']['actions']['remove_name'] = [
'#type' => 'submit',
'#value' => t('Remove one'),
'#submit' => array('::removeCallback'),
'#ajax' => [
'callback' => '::addmoreCallback',
'wrapper' => 'names-fieldset-wrapper',
],
];
}
$form_state->setCached(FALSE);
$form['actions']['submit'] = [
'#type' => 'submit',
'#value' => $this->t('Submit'),
];
return $form;
}
/**
* {@inheritdoc}
*/
public function getFormId() {
return 'fapi_example_ajax_addmore';
}
/**
* Callback for both ajax-enabled buttons.
*
* Selects and returns the fieldset with the names in it.
*/
public function addmoreCallback(array &$form, FormStateInterface $form_state) {
$name_field = $form_state->get('num_names');
return $form['names_fieldset'];
}
/**
* Submit handler for the "add-one-more" button.
*
* Increments the max counter and causes a rebuild.
*/
public function addOne(array &$form, FormStateInterface $form_state) {
$name_field = $form_state->get('num_names');
$add_button = $name_field + 1;
$form_state->set('num_names', $add_button);
$form_state->setRebuild();
}
/**
* Submit handler for the "remove one" button.
*
* Decrements the max counter and causes a form rebuild.
*/
public function removeCallback(array &$form, FormStateInterface $form_state) {
$name_field = $form_state->get('num_names');
if ($name_field > 1) {
$remove_button = $name_field - 1;
$form_state->set('num_names', $remove_button);
}
$form_state->setRebuild();
}
/**
* Final submit handler.
*
* Reports what values were finally set.
*/
public function submitForm(array &$form, FormStateInterface $form_state) {
$values = $form_state->getValue(array('names_fieldset', 'name'));
$output = t('These people are coming to the picnic: @names', array(
'@names' => implode(', ', $values),
)
);
drupal_set_message($output);
}
}