function FormTestClickedButtonForm::buildForm

Same name in other branches
  1. 9 core/modules/system/tests/modules/form_test/src/Form/FormTestClickedButtonForm.php \Drupal\form_test\Form\FormTestClickedButtonForm::buildForm()
  2. 8.9.x core/modules/system/tests/modules/form_test/src/Form/FormTestClickedButtonForm.php \Drupal\form_test\Form\FormTestClickedButtonForm::buildForm()
  3. 11.x core/modules/system/tests/modules/form_test/src/Form/FormTestClickedButtonForm.php \Drupal\form_test\Form\FormTestClickedButtonForm::buildForm()

Overrides FormInterface::buildForm

File

core/modules/system/tests/modules/form_test/src/Form/FormTestClickedButtonForm.php, line 25

Class

FormTestClickedButtonForm
Form builder to test button click detection.

Namespace

Drupal\form_test\Form

Code

public function buildForm(array $form, FormStateInterface $form_state, $first = NULL, $second = NULL, $third = NULL) {
    // A single text field. In IE, when a form has only one non-button input field
    // and the ENTER key is pressed while that field has focus, the form is
    // submitted without any information identifying the button responsible for
    // the submission. In other browsers, the form is submitted as though the
    // first button were clicked.
    $form['text'] = [
        '#title' => 'Text',
        '#type' => 'textfield',
    ];
    // Loop through each path argument, adding buttons based on the information
    // in the argument. For example, if the path is
    // form-test/clicked-button/s/i/rb, then 3 buttons are added: a 'submit', an
    // 'image_button', and a 'button' with #access=FALSE. This enables form.test
    // to test a variety of combinations.
    $i = 0;
    $args = [
        $first,
        $second,
        $third,
    ];
    foreach ($args as $arg) {
        $name = 'button' . ++$i;
        // 's', 'b', or 'i' in the argument define the button type wanted.
        if (!is_string($arg)) {
            $type = NULL;
        }
        elseif (str_contains($arg, 's')) {
            $type = 'submit';
        }
        elseif (str_contains($arg, 'b')) {
            $type = 'button';
        }
        elseif (str_contains($arg, 'i')) {
            $type = 'image_button';
        }
        else {
            $type = NULL;
        }
        if (isset($type)) {
            $form[$name] = [
                '#type' => $type,
                '#name' => $name,
            ];
            // Image buttons need a #src; the others need a #value.
            if ($type == 'image_button') {
                $form[$name]['#src'] = 'core/misc/druplicon.png';
            }
            else {
                $form[$name]['#value'] = $name;
            }
            // 'r' for restricted, so we can test that button click detection code
            // correctly takes #access security into account.
            if (str_contains($arg, 'r')) {
                $form[$name]['#access'] = FALSE;
            }
        }
    }
    return $form;
}

Buggy or inaccurate documentation? Please file an issue. Need support? Need help programming? Connect with the Drupal community.