7
7
/**
8
8
* Implements the state demo form controller.
9
9
*
10
- * This example demonstrates using the #state property to bind the visibility of
11
- * a form element to the value of another element in the form. In the example,
12
- * when the user checks the "Need Special Accommodation" checkbox, additional
13
- * form elements are made visible.
10
+ * This example demonstrates using the #states property to bind the visibility
11
+ * of a form element to the value of another element in the form. In the
12
+ * example, when the user checks the "Need Special Accommodation" checkbox,
13
+ * additional form elements are made visible.
14
14
*
15
15
* The submit handler for this form is implemented by the
16
16
* \Drupal\form_api_example\Form\DemoBase class.
17
17
*
18
18
* @see \Drupal\Core\Form\FormBase
19
19
* @see \Drupal\form_api_example\Form\DemoBase
20
+ * @see drupal_process_states()
20
21
*/
21
22
class StateDemo extends DemoBase {
22
23
@@ -43,9 +44,22 @@ public function buildForm(array $form, FormStateInterface $form_state) {
43
44
'#attributes ' => [
44
45
'class ' => 'accommodation ' ,
45
46
],
47
+ // #states is an associative array. Each key is the name of a state to
48
+ // apply to the element, such as 'visible'. Each value is another array,
49
+ // making a list of conditions that denote when the state should be
50
+ // applied. Every condition is a key/value pair, whose key is a jQuery
51
+ // selector that denotes another element on the page, and whose value is
52
+ // an array of conditions, which must bet met on in order for the state to
53
+ // be applied.
54
+ //
55
+ // For additional documentation on the #states property including a list
56
+ // of valid states and conditions see drupal_process_states().
46
57
'#states ' => [
58
+ // The state being affected is "invisible".
47
59
'invisible ' => [
48
- 'input[name="needs_accommodation"] ' => ['checked ' => FALSE ],
60
+ // Drupal will only apply this state when the element that satisfies
61
+ // the selector input[name="needs_accommodation"] is un-checked.
62
+ ':input[name="needs_accommodation"] ' => ['checked ' => FALSE ],
49
63
],
50
64
],
51
65
];
0 commit comments