3 namespace Drupal\form_test\Form;
5 use Drupal\Core\Form\FormBase;
6 use Drupal\Core\Form\FormStateInterface;
9 * Form builder to test button click detection.
11 class FormTestClickedButtonForm extends FormBase {
16 public function getFormId() {
17 return 'form_test_clicked_button';
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.
31 '#type' => 'textfield',
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.
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) {
47 elseif (strpos($arg, 'b') !== FALSE) {
50 elseif (strpos($arg, 'i') !== FALSE) {
51 $type = 'image_button';
61 // Image buttons need a #src; the others need a #value.
62 if ($type == 'image_button') {
63 $form[$name]['#src'] = 'core/misc/druplicon.png';
66 $form[$name]['#value'] = $name;
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;
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']]));
87 drupal_set_message('There is no clicked button.');
94 public function submitForm(array &$form, FormStateInterface $form_state) {
95 drupal_set_message('Submit handler for form_test_clicked_button executed.');