You are here

public function ConditionalFieldForm::submitForm in Conditional Fields 8

Same name and namespace in other branches
  1. 4.x src/Form/ConditionalFieldForm.php \Drupal\conditional_fields\Form\ConditionalFieldForm::submitForm()

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/ConditionalFieldForm.php, line 203

Class

ConditionalFieldForm
Class ConditionalFieldForm.

Namespace

Drupal\conditional_fields\Form

Code

public function submitForm(array &$form, FormStateInterface $form_state) {
  $table = $form_state
    ->getValue('table');
  if (empty($table['add_new_dependency']) || !is_array($table['add_new_dependency'])) {
    parent::submitForm($form, $form_state);
  }
  $field_names = [];
  $form_state
    ->set('plugin_settings_edit', NULL);
  $conditional_values = $table['add_new_dependency'];

  // Copy values from table for submit.
  $component_value = [];
  $settings = $this->list
    ->conditionalFieldsDependencyDefaultSettings();
  foreach ($conditional_values as $key => $value) {
    if ($key == 'dependent') {
      $field_names = $value;
      continue;
    }
    if (in_array($key, [
      'entity_type',
      'bundle',
      'dependee',
    ])) {
      $component_value[$key] = $value;
      continue;
    }

    // @TODO: it seems reasonable
    // to only set values allowed by field schema,
    // @see conditional_fields.schema.yml
    $settings[$key] = $value;
  }
  unset($settings['actions']);
  $component_value['settings'] = $settings;
  $component_value['entity_type'] = $form_state
    ->getValue('entity_type');
  $component_value['bundle'] = $form_state
    ->getValue('bundle');

  /** @var \Drupal\Core\Entity\Display\EntityFormDisplayInterface $entity */
  $entity = $this->entityTypeManager
    ->getStorage('entity_form_display')
    ->load($component_value['entity_type'] . '.' . $component_value['bundle'] . '.' . 'default');
  if (!$entity) {
    return;
  }

  // Handle one to one condition creating with redirect to edit form.
  if (count($field_names) == 1) {
    $field_name = reset($field_names);
    $uuid = $form_state
      ->getValue('uuid', $this->uuidGenerator
      ->generate());
    $field = $entity
      ->getComponent($field_name);
    $field['third_party_settings']['conditional_fields'][$uuid] = $component_value;
    $entity
      ->setComponent($field_name, $field);
    $entity
      ->save();
    $parameters = [
      'entity_type' => $component_value['entity_type'],
      'bundle' => $component_value['bundle'],
      'field_name' => $field_name,
      'uuid' => $uuid,
    ];
    $form_state
      ->setRedirect($this->editPath, $parameters);
    return;
  }

  // Handle many to one, in that case we always need new uuid.
  foreach ($field_names as $field_name) {
    $uuid = $this->uuidGenerator
      ->generate();
    $field = $entity
      ->getComponent($field_name);
    $field['third_party_settings']['conditional_fields'][$uuid] = $component_value;
    $entity
      ->setComponent($field_name, $field);
  }
  $entity
    ->save();
}