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