function ComponentInFormTest::buildForm

Form constructor.

Parameters

array $form: An associative array containing the structure of the form.

\Drupal\Core\Form\FormStateInterface $form_state: The current state of the form.

Return value

array The form structure.

Overrides FormInterface::buildForm

File

core/tests/Drupal/KernelTests/Components/ComponentInFormTest.php, line 41

Class

ComponentInFormTest
Tests the correct rendering of components in form.

Namespace

Drupal\KernelTests\Components

Code

public function buildForm(array $form, FormStateInterface $form_state) : array {
  $form['normal'] = [
    '#type' => 'textfield',
    '#title' => 'Normal form element',
    '#default_value' => 'fake 1',
  ];
  // We want to test form elements inside a component, itself inside a
  // component.
  $form['banner'] = [
    '#type' => 'component',
    '#component' => 'sdc_test:my-banner',
    '#props' => [
      'ctaText' => 'Click me!',
      'ctaHref' => 'https://www.example.org',
      'ctaTarget' => '',
    ],
    'banner_body' => [
      '#type' => 'component',
      '#component' => 'sdc_theme_test:my-card',
      '#props' => [
        'header' => 'Card header',
      ],
      'card_body' => [
        'foo' => [
          '#type' => 'textfield',
          '#title' => 'Textfield in component',
          '#default_value' => 'fake 2',
        ],
        'bar' => [
          '#type' => 'select',
          '#title' => 'Select in component',
          '#options' => [
            'option_1' => 'Option 1',
            'option_2' => 'Option 2',
          ],
          '#empty_option' => 'Empty option',
          '#default_value' => 'option_1',
        ],
      ],
    ],
  ];
  $form['actions'] = [
    '#type' => 'actions',
    'submit' => [
      '#type' => 'submit',
      '#value' => 'Submit',
    ],
  ];
  return $form;
}

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