function AjaxAddRemoveElements::buildForm

Same name in other branches
  1. 4.0.x modules/form_api_example/src/Form/AjaxAddRemoveElements.php \Drupal\form_api_example\Form\AjaxAddRemoveElements::buildForm()

Form with 'add more' and 'remove' buttons.

Overrides FormInterface::buildForm

File

modules/form_api_example/src/Form/AjaxAddRemoveElements.php, line 26

Class

AjaxAddRemoveElements
Example ajax add remove buttons.

Namespace

Drupal\form_api_example\Form

Code

public function buildForm(array $form, FormStateInterface $form_state) {
    $form['description'] = [
        '#type' => 'item',
        '#markup' => $this->t('This example shows an add-more button and a remove button for each specific element.'),
    ];
    // Get the number of names in the form already.
    $num_lines = $form_state->get('num_lines');
    // We have to ensure that there is at least one name field.
    if ($num_lines === NULL) {
        $form_state->set('num_lines', 1);
        $num_lines = $form_state->get('num_lines');
    }
    // Get a list of fields that were removed.
    $removed_fields = $form_state->get('removed_fields');
    // If no fields have been removed yet we use an empty array.
    if ($removed_fields === NULL) {
        $form_state->set('removed_fields', []);
        $removed_fields = $form_state->get('removed_fields');
    }
    $form['#tree'] = TRUE;
    $form['names_fieldset'] = [
        '#type' => 'fieldset',
        '#title' => $this->t('People coming to picnic'),
        '#prefix' => '<div id="names-fieldset-wrapper">',
        '#suffix' => '</div>',
    ];
    for ($i = 0; $i < $num_lines; $i++) {
        // Check if field was removed.
        if (in_array($i, $removed_fields)) {
            // Skip if field was removed and move to the next field.
            continue;
        }
        
        /* Create a new fieldset for each person
         * where we can add first and last name
         */
        // Fieldset title.
        $form['names_fieldset'][$i] = [
            '#type' => 'fieldset',
            '#title' => $this->t('Person') . ' ' . ($i + 1),
        ];
        // Date.
        $form['names_fieldset'][$i]['firstname'] = [
            '#type' => 'textfield',
            '#title' => $this->t('First name'),
        ];
        // Amount.
        $form['names_fieldset'][$i]['lastname'] = [
            '#type' => 'textfield',
            '#title' => $this->t('Last name'),
        ];
        $form['names_fieldset'][$i]['actions'] = [
            '#type' => 'submit',
            '#value' => $this->t('Remove'),
            '#name' => $i,
            '#submit' => [
                '::removeCallback',
            ],
            '#ajax' => [
                'callback' => '::addmoreCallback',
                'wrapper' => 'names-fieldset-wrapper',
            ],
        ];
    }
    $form['names_fieldset']['actions'] = [
        '#type' => 'actions',
    ];
    $form['names_fieldset']['actions']['add_name'] = [
        '#type' => 'submit',
        '#value' => $this->t('Add one more'),
        '#submit' => [
            '::addOne',
        ],
        '#ajax' => [
            'callback' => '::addmoreCallback',
            'wrapper' => 'names-fieldset-wrapper',
        ],
    ];
    $form['actions']['submit'] = [
        '#type' => 'submit',
        '#value' => $this->t('Submit'),
    ];
    return $form;
}