function theming_example_form_alter

Same name and namespace in other branches
  1. 4.0.x modules/theming_example/theming_example.module \theming_example_form_alter()

Implements hook_form_alter().

In Drupal 8+, all forms share the same theme hook (form). Use hook_form_alter()/hook_form_FORM_ID_alter() to mofidy the form render array.

Related topics

File

modules/theming_example/theming_example.module, line 158

Code

function theming_example_form_alter(&$form, FormStateInterface $form_state, $form_id) {
    switch ($form_id) {
        case 'theming_example_form_select':
            // Add data-strong attribute to make title strong.
            // @see theming_example_preprocess_form_element_label().
            $form['choice']['#label_attributes']['data-strong'] = 1;
            // Output choice title separately using h3 header.
            $form['title'] = [
                '#type' => 'html_tag',
                '#tag' => 'h3',
                '#value' => $form['choice']['#title'],
                '#weight' => -100,
            ];
            // Wrap choice and submit elements in inline container.
            $form['choice']['#prefix'] = '<div class="container-inline choice-wrapper">';
            $form['submit']['#suffix'] = '</div>';
            break;
        case 'theming_example_form_text':
            // Add data-strong attribute to make title strong.
            // @see theming_example_preprocess_form_element_label().
            $form['text']['#label_attributes']['data-strong'] = 1;
            break;
    }
}