You are here

public function FormOptionsAttributesTestForm::buildForm in Form Options Attributes 2.0.x

Same name and namespace in other branches
  1. 8 tests/modules/form_options_attributes_test/src/Form/FormOptionsAttributesTestForm.php \Drupal\form_options_attributes_test\Form\FormOptionsAttributesTestForm::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

tests/modules/form_options_attributes_test/src/Form/FormOptionsAttributesTestForm.php, line 13

Class

FormOptionsAttributesTestForm

Namespace

Drupal\form_options_attributes_test\Form

Code

public function buildForm(array $form, FormStateInterface $form_state) {
  $states = [
    'AL' => $this
      ->t('Alabama'),
    'AK' => $this
      ->t('Alaska'),
    'AZ' => $this
      ->t('Arizona'),
    'AR' => $this
      ->t('Arkansas'),
    // ..
    'WI' => $this
      ->t('Wisconsin'),
    'WY' => $this
      ->t('Wyoming'),
  ];
  $states_attributes = [
    'AL' => [
      'class' => [
        'southeast',
      ],
      'data-bbq-meat' => 'pork',
    ],
    'AK' => [
      'class' => [
        'non-contiguous',
      ],
      'data-bbq-meat' => 'salmon',
    ],
    'AZ' => [
      'class' => [
        'southwest',
      ],
      'data-bbq-meat' => 'rattlesnake',
    ],
    'AR' => [
      'class' => [
        'south',
      ],
      'data-bbq-meat' => 'beef',
    ],
    // ...
    'WI' => [
      'class' => [
        'midwest',
      ],
      'data-bbq-meat' => 'cheese',
    ],
    'WY' => [
      'class' => [
        'flyover',
      ],
      'data-bbq-meat' => 'bison',
    ],
  ];
  $form['states'] = [
    '#type' => 'select',
    '#title' => $this
      ->t('States'),
    '#options' => $states,
    '#options_attributes' => $states_attributes,
    '#attributes' => [
      'class' => [
        'states-bbq-selector',
      ],
    ],
  ];
  $form['states_radio'] = [
    '#type' => 'radios',
    '#title' => $this
      ->t('States'),
    '#options' => $states,
    '#options_attributes' => $states_attributes,
    '#attributes' => [
      'class' => [
        'states-bbq-selector-radios',
      ],
    ],
  ];
  $form['states_checkboxes'] = [
    '#type' => 'checkboxes',
    '#title' => $this
      ->t('States'),
    '#options' => $states,
    '#options_attributes' => $states_attributes,
    '#attributes' => [
      'class' => [
        'states-bbq-selector-checkboxes',
      ],
    ],
  ];
  return $form;
}