You are here

public function EditForm::submitForm in Form Defaults 8

Form submission handler.

Parameters

array $form: An associative array containing the structure of the form.

\Drupal\Core\Form\FormStateInterface $form_state: The current state of the form.

Overrides FormInterface::submitForm

File

src/Form/EditForm.php, line 81

Class

EditForm

Namespace

Drupal\Formdefaults\Form

Code

public function submitForm(array &$form, FormStateInterface $form_state) {
  $form_values = $form_state
    ->getValues();
  $formid = $form_values['formid'];
  $formdef = formdefaults_getform($formid);

  // Reset fields
  if ($_POST['op'] == 'Reset Selected') {
    foreach ($form_values['fields'] as $field => $checked) {
      if ($checked) {
        unset($formdef[$field]);
      }
    }

    // Condense addon array.
    if (isset($formdef['#formdefaults_addon_fields'])) {
      $addons = (array) $formdef['#formdefaults_addon_fields'];
      $new_addons = array();
      foreach ($addons as $key => $field) {
        if (@$formdef[$key]) {
          $i = 'formdefaults_' . count($new_addons);
          if ($i != $key) {
            $formdef[$i] = $formdef[$key];
            unset($formdef[$key]);
            if ($formdef[$i . '_markup']) {
              $formdef[$i . '_markup'] = $formdef[$key . '_markup'];
              unset($formdef[$key . '_markup']);
            }
          }
          $new_addons[$i] = $field;
        }
      }
      $formdef['#formdefaults_addon_fields'] = $new_addons;
    }
  }
  if ($_POST['op'] == 'Add') {
    $i = count((array) @$formdef['#formdefaults_addon_fields']);
    $key = 'formdefaults_' . $i;
    $subkey = $key . '_markup';
    $field = array();
    $weight = $form_values['weight'];
    switch ($form_values['field_type']) {
      case "markup":
        $field = array(
          '#type' => 'markup',
          '#markup' => '',
        );
        $formdef[$key] = array(
          'type' => 'markup',
          'value' => '<p>Replace with your own markup</p>',
          'format' => 0,
          'weight' => $weight,
        );
        break;
      case "fieldset":
        $field = array(
          '#type' => 'fieldset',
          '#title' => 'Untitled',
          '#collapsible' => TRUE,
          '#collapsed' => TRUE,
          $subkey => array(
            '#type' => 'markup',
            '#value' => '',
          ),
        );
        $formdef[$key] = array(
          'type' => 'fieldset',
          'title' => 'Untitled',
          'weight' => $weight,
        );
        $formdef[$subkey] = array(
          'type' => 'markup',
          'value' => '<p>Replace with your own markup</p>',
        );
        break;
    }
    $formdef['#formdefaults_addon_fields'][$key] = $field;
  }
  $helper = new FormDefaultsHelper();
  $helper
    ->saveForm($formid, $formdef);
}