3 namespace Drupal\form_test\Form;
5 use Drupal\Core\Form\FormBase;
6 use Drupal\Core\Form\FormStateInterface;
7 use Symfony\Component\HttpFoundation\JsonResponse;
10 * Form constructor for testing #type 'machine_name' elements.
12 class FormTestMachineNameForm extends FormBase {
17 public function getFormId() {
18 return 'form_test_machine_name';
24 public function buildForm(array $form, FormStateInterface $form_state) {
25 $form['machine_name_1_label'] = [
26 '#type' => 'textfield',
27 '#title' => 'Machine name 1 label',
29 $form['machine_name_1'] = [
30 '#type' => 'machine_name',
31 '#title' => 'Machine name 1',
32 '#description' => 'A machine name.',
34 'source' => ['machine_name_1_label']
37 $form['machine_name_2_label'] = [
38 '#type' => 'textfield',
39 '#title' => 'Machine name 2 label',
41 $form['machine_name_2'] = [
42 '#type' => 'machine_name',
43 '#title' => 'Machine name 2',
44 '#description' => 'Another machine name.',
46 'source' => ['machine_name_2_label']
59 public function submitForm(array &$form, FormStateInterface $form_state) {
60 $form_state->setResponse(new JsonResponse($form_state->getValues()));