FormTestObject.php 1.65 KB
Newer Older
1 2 3 4
<?php

namespace Drupal\form_test;

5
use Drupal\Core\Form\ConfigFormBase;
6
use Drupal\Core\Form\FormStateInterface;
7 8 9

/**
 * Provides a test form object.
10 11
 *
 * @internal
12
 */
13
class FormTestObject extends ConfigFormBase {
14 15

  /**
16
   * {@inheritdoc}
17
   */
18
  public function getFormId() {
19 20 21
    return 'form_test_form_test_object';
  }

22 23 24 25 26 27 28
  /**
   * {@inheritdoc}
   */
  protected function getEditableConfigNames() {
    return ['form_test.object'];
  }

29
  /**
30
   * {@inheritdoc}
31
   */
32
  public function buildForm(array $form, FormStateInterface $form_state) {
33
    $form['element'] = ['#markup' => 'The FormTestObject::buildForm() method was used for this form.'];
34

35
    $form['bananas'] = [
36
      '#type' => 'textfield',
37
      '#title' => $this->t('Bananas'),
38
    ];
39 40 41 42 43
    $form['strawberry'] = [
      '#type' => 'hidden',
      '#value' => 'red',
      '#attributes' => ['id' => 'redstrawberryhiddenfield'],
    ];
44

45
    $form['actions']['#type'] = 'actions';
46
    $form['actions']['submit'] = [
47
      '#type' => 'submit',
48
      '#value' => $this->t('Save'),
49
    ];
50 51 52

    $form['#title'] = 'Test dynamic title';

53 54 55 56
    return $form;
  }

  /**
57
   * {@inheritdoc}
58
   */
59
  public function validateForm(array &$form, FormStateInterface $form_state) {
60
    $this->messenger()->addStatus($this->t('The FormTestObject::validateForm() method was used for this form.'));
61 62 63
  }

  /**
64
   * {@inheritdoc}
65
   */
66
  public function submitForm(array &$form, FormStateInterface $form_state) {
67
    $this->messenger()->addStatus($this->t('The FormTestObject::submitForm() method was used for this form.'));
68
    $this->config('form_test.object')
69
      ->set('bananas', $form_state->getValue('bananas'))
70
      ->save();
71 72 73
  }

}