summaryrefslogtreecommitdiffstats
path: root/core/modules/search/tests/modules/search_embedded_form/src/Form/SearchEmbeddedForm.php
blob: 5f13ee93940c0cfdc0b79a8527882b89f737eb3e (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
<?php

namespace Drupal\search_embedded_form\Form;

use Drupal\Core\Form\FormBase;
use Drupal\Core\Form\FormStateInterface;

/**
 * Form controller for search_embedded_form form.
 *
 * @internal
 */
class SearchEmbeddedForm extends FormBase {

  /**
   * {@inheritdoc}
   */
  public function getFormId() {
    return 'search_embedded_form';
  }

  /**
   * {@inheritdoc}
   */
  public function buildForm(array $form, FormStateInterface $form_state) {
    $count = \Drupal::state()->get('search_embedded_form.submit_count');

    $form['name'] = [
      '#type' => 'textfield',
      '#title' => $this->t('Your name'),
      '#maxlength' => 255,
      '#default_value' => '',
      '#required' => TRUE,
      '#description' => $this->t('Times form has been submitted: %count', ['%count' => $count]),
    ];

    $form['actions'] = ['#type' => 'actions'];
    $form['actions']['submit'] = [
      '#type' => 'submit',
      '#value' => $this->t('Send away'),
    ];

    return $form;
  }

  /**
   * {@inheritdoc}
   */
  public function submitForm(array &$form, FormStateInterface $form_state) {
    $state = \Drupal::state();
    $submit_count = (int) $state->get('search_embedded_form.submit_count');
    $state->set('search_embedded_form.submit_count', $submit_count + 1);
    $this->messenger()->addStatus($this->t('Test form was submitted'));
  }

}