class SelectForm

A simple form that displays a select box and submit button.

This form will be be themed by the 'theming_example_select_form' theme handler.

Hierarchy

  • class \Drupal\theming_example\Form\SelectForm extends \Drupal\Core\Form\FormBase

Expanded class hierarchy of SelectForm

1 string reference to 'SelectForm'
theming_example.routing.yml in modules/theming_example/theming_example.routing.yml
modules/theming_example/theming_example.routing.yml

File

modules/theming_example/src/Form/SelectForm.php, line 14

Namespace

Drupal\theming_example\Form
View source
class SelectForm extends FormBase {
  
  /**
   * {@inheritdoc}
   */
  public function getFormId() {
    return 'theming_example_form_select';
  }
  
  /**
   * {@inheritdoc}
   */
  public function buildForm(array $form, FormStateInterface $form_state) {
    $options = [
      'newest_first' => $this->t('Newest first'),
      'newest_last' => $this->t('Newest last'),
      'edited_first' => $this->t('Edited first'),
      'edited_last' => $this->t('Edited last'),
      'by_name' => $this->t('By name'),
    ];
    $form['choice'] = [
      '#type' => 'select',
      '#options' => $options,
      '#title' => $this->t('Choose which ordering you want'),
    ];
    $form['submit'] = [
      '#type' => 'submit',
      '#value' => $this->t('Go'),
    ];
    return $form;
  }
  
  /**
   * {@inheritdoc}
   */
  public function submitForm(array &$form, FormStateInterface $form_state) {
    $this->messenger()
      ->addMessage($this->t('You chose %input', [
      '%input' => $form_state->getValue('choice'),
    ]));
  }

}

Members

Title Sort descending Modifiers Object type Summary
SelectForm::buildForm public function
SelectForm::getFormId public function
SelectForm::submitForm public function