You are here

public function ConditionalFieldEditForm::submitForm in Conditional Fields 8

Same name and namespace in other branches
  1. 4.x src/Form/ConditionalFieldEditForm.php \Drupal\conditional_fields\Form\ConditionalFieldEditForm::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

1 call to ConditionalFieldEditForm::submitForm()
ConditionalFieldEditFormTab::submitForm in src/Form/ConditionalFieldEditFormTab.php
Form submission handler.
1 method overrides ConditionalFieldEditForm::submitForm()
ConditionalFieldEditFormTab::submitForm in src/Form/ConditionalFieldEditFormTab.php
Form submission handler.

File

src/Form/ConditionalFieldEditForm.php, line 314

Class

ConditionalFieldEditForm
Class ConditionalFieldEditForm.

Namespace

Drupal\conditional_fields\Form

Code

public function submitForm(array &$form, FormStateInterface $form_state) {
  $values = $form_state
    ->cleanValues()
    ->getValues();
  $uuid = $values['uuid'];
  $entity_type = $values['entity_type'];
  $bundle = $values['bundle'];

  /** @var EntityFormDisplay $entity */
  $entity = $this->entityTypeManager
    ->getStorage('entity_form_display')
    ->load("{$entity_type}.{$bundle}.default");
  if (!$entity) {
    return;
  }
  $field_names = explode('-', $values['field_name']);
  foreach ($field_names as $field_name) {
    $field = $entity
      ->getComponent($field_name);
    $settings =& $field['third_party_settings']['conditional_fields'][$uuid]['settings'];
    $dependee = $field['third_party_settings']['conditional_fields'][$uuid]['dependee'];
    $exclude_fields = [
      'entity_type',
      'bundle',
      'field_name',
      'uuid',
    ];
    foreach ($values as $key => $value) {
      if (in_array($key, $exclude_fields) || empty($value)) {
        continue;
      }
      else {
        $settings[$key] = $value;
      }
    }
    if ($settings['effect'] == 'show') {
      $settings['effect_options'] = [];
    }

    // Set field value.
    if (isset($settings[$dependee]) && !empty($settings[$dependee])) {

      // Get and save value as string with timezone.
      $value =& $settings[$dependee];
      if (!empty($value[0]['value']) && is_object($value[0]['value']) && $value[0]['value'] instanceof DrupalDateTime) {
        foreach ($value as $delta => $date) {
          if (!empty($date['value'])) {

            // Need to find a solution to handle both datetime and date types.
            $value[$delta]['value'] = $date['value']
              ->format(DateTimeItemInterface::DATETIME_STORAGE_FORMAT);
          }
        }
      }
      $settings['value_form'] = $settings[$dependee];
      unset($settings[$dependee]);
    }
    $entity
      ->setComponent($field_name, $field);
  }
  $entity
    ->save();
  $parameters = [
    'entity_type' => $values['entity_type'],
    'bundle' => $values['bundle'],
  ];
  $form_state
    ->setRedirect($this->redirectPath, $parameters);
}