adab5bda00170141c2b7bd458cd61cf8a1522456
[yaffs-website] / web / core / modules / system / tests / modules / form_test / src / Form / FormTestClickedButtonForm.php
1 <?php
2
3 namespace Drupal\form_test\Form;
4
5 use Drupal\Core\Form\FormBase;
6 use Drupal\Core\Form\FormStateInterface;
7
8 /**
9  * Form builder to test button click detection.
10  */
11 class FormTestClickedButtonForm extends FormBase {
12
13   /**
14    * {@inheritdoc}
15    */
16   public function getFormId() {
17     return 'form_test_clicked_button';
18   }
19
20   /**
21    * {@inheritdoc}
22    */
23   public function buildForm(array $form, FormStateInterface $form_state, $first = NULL, $second = NULL, $third = NULL) {
24     // A single text field. In IE, when a form has only one non-button input field
25     // and the ENTER key is pressed while that field has focus, the form is
26     // submitted without any information identifying the button responsible for
27     // the submission. In other browsers, the form is submitted as though the
28     // first button were clicked.
29     $form['text'] = [
30       '#title' => 'Text',
31       '#type' => 'textfield',
32     ];
33
34     // Loop through each path argument, adding buttons based on the information
35     // in the argument. For example, if the path is
36     // form-test/clicked-button/s/i/rb, then 3 buttons are added: a 'submit', an
37     // 'image_button', and a 'button' with #access=FALSE. This enables form.test
38     // to test a variety of combinations.
39     $i = 0;
40     $args = [$first, $second, $third];
41     foreach ($args as $arg) {
42       $name = 'button' . ++$i;
43       // 's', 'b', or 'i' in the argument define the button type wanted.
44       if (strpos($arg, 's') !== FALSE) {
45         $type = 'submit';
46       }
47       elseif (strpos($arg, 'b') !== FALSE) {
48         $type = 'button';
49       }
50       elseif (strpos($arg, 'i') !== FALSE) {
51         $type = 'image_button';
52       }
53       else {
54         $type = NULL;
55       }
56       if (isset($type)) {
57         $form[$name] = [
58           '#type' => $type,
59           '#name' => $name,
60         ];
61         // Image buttons need a #src; the others need a #value.
62         if ($type == 'image_button') {
63           $form[$name]['#src'] = 'core/misc/druplicon.png';
64         }
65         else {
66           $form[$name]['#value'] = $name;
67         }
68         // 'r' for restricted, so we can test that button click detection code
69         // correctly takes #access security into account.
70         if (strpos($arg, 'r') !== FALSE) {
71           $form[$name]['#access'] = FALSE;
72         }
73       }
74     }
75
76     return $form;
77   }
78
79   /**
80    * {@inheritdoc}
81    */
82   public function validateForm(array &$form, FormStateInterface $form_state) {
83     if ($triggering_element = $form_state->getTriggeringElement()) {
84       drupal_set_message(t('The clicked button is %name.', ['%name' => $triggering_element['#name']]));
85     }
86     else {
87       drupal_set_message('There is no clicked button.');
88     }
89   }
90
91   /**
92    * {@inheritdoc}
93    */
94   public function submitForm(array &$form, FormStateInterface $form_state) {
95     drupal_set_message('Submit handler for form_test_clicked_button executed.');
96   }
97
98 }