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.
14 class FormTestMachineNameForm extends FormBase {
19 public function getFormId() {
20 return 'form_test_machine_name';
26 public function buildForm(array $form, FormStateInterface $form_state) {
27 $form['machine_name_1_label'] = [
28 '#type' => 'textfield',
29 '#title' => 'Machine name 1 label',
31 $form['machine_name_1'] = [
32 '#type' => 'machine_name',
33 '#title' => 'Machine name 1',
34 '#description' => 'A machine name.',
36 'source' => ['machine_name_1_label']
39 $form['machine_name_2_label'] = [
40 '#type' => 'textfield',
41 '#title' => 'Machine name 2 label',
43 $form['machine_name_2'] = [
44 '#type' => 'machine_name',
45 '#title' => 'Machine name 2',
46 '#description' => 'Another machine name.',
48 'source' => ['machine_name_2_label']
61 public function submitForm(array &$form, FormStateInterface $form_state) {
62 $form_state->setResponse(new JsonResponse($form_state->getValues()));