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