Version 1
[yaffs-website] / web / core / modules / system / tests / modules / form_test / src / FormTestObject.php
1 <?php
2
3 namespace Drupal\form_test;
4
5 use Drupal\Core\Form\ConfigFormBase;
6 use Drupal\Core\Form\FormStateInterface;
7
8 /**
9  * Provides a test form object.
10  */
11 class FormTestObject extends ConfigFormBase {
12
13   /**
14    * {@inheritdoc}
15    */
16   public function getFormId() {
17     return 'form_test_form_test_object';
18   }
19
20   /**
21    * {@inheritdoc}
22    */
23   protected function getEditableConfigNames() {
24     return ['form_test.object'];
25   }
26
27   /**
28    * {@inheritdoc}
29    */
30   public function buildForm(array $form, FormStateInterface $form_state) {
31     $form['element'] = ['#markup' => 'The FormTestObject::buildForm() method was used for this form.'];
32
33     $form['bananas'] = [
34       '#type' => 'textfield',
35       '#title' => $this->t('Bananas'),
36     ];
37     $form['strawberry'] = [
38       '#type' => 'hidden',
39       '#value' => 'red',
40       '#attributes' => ['id' => 'redstrawberryhiddenfield'],
41     ];
42
43     $form['actions']['#type'] = 'actions';
44     $form['actions']['submit'] = [
45       '#type' => 'submit',
46       '#value' => $this->t('Save'),
47     ];
48
49     $form['#title'] = 'Test dynamic title';
50
51     return $form;
52   }
53
54   /**
55    * {@inheritdoc}
56    */
57   public function validateForm(array &$form, FormStateInterface $form_state) {
58     drupal_set_message($this->t('The FormTestObject::validateForm() method was used for this form.'));
59   }
60
61   /**
62    * {@inheritdoc}
63    */
64   public function submitForm(array &$form, FormStateInterface $form_state) {
65     drupal_set_message($this->t('The FormTestObject::submitForm() method was used for this form.'));
66     $this->config('form_test.object')
67       ->set('bananas', $form_state->getValue('bananas'))
68       ->save();
69   }
70
71 }