function FieldStorageAddForm::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

core/modules/field_ui/src/Form/FieldStorageAddForm.php, line 452

Class

FieldStorageAddForm
Provides a form for the "field storage" add page.

Namespace

Drupal\field_ui\Form

Code

public function submitForm(array &$form, FormStateInterface $form_state) {
  $values = $form_state->getValues();
  $entity_type = $this->entityTypeManager
    ->getDefinition($this->entityTypeId);
  $field_storage_type = $values['group_field_options_wrapper'] ?? $values['new_storage_type'];
  $field_name = $values['field_name'];
  $field_values = [
    'entity_type' => $this->entityTypeId,
    'bundle' => $this->bundle,
  ];
  // Check if we're dealing with a preconfigured field.
  if (str_starts_with($field_storage_type, 'field_ui:')) {
    [, $field_type, $preset_key] = explode(':', $field_storage_type, 3);
    $default_options = $this->getNewFieldDefaults($field_type, $preset_key);
  }
  else {
    $field_type = $field_storage_type;
    $default_options = [];
  }
  $field_values += [
    $default_options['field_config'] ?? [],
    'field_name' => $field_name,
    'label' => $values['label'],
    // Field translatability should be explicitly enabled by the users.
'translatable' => FALSE,
  ];
  $field_storage_values = [
    $default_options['field_storage_config'] ?? [],
    'field_name' => $field_name,
    'type' => $field_type,
    'entity_type' => $this->entityTypeId,
    'translatable' => $values['translatable'],
  ];
  try {
    $field_storage_entity = $this->entityTypeManager
      ->getStorage('field_storage_config')
      ->create($field_storage_values);
  } catch (\Exception $e) {
    $this->messenger()
      ->addError($this->t('There was a problem creating field %label: @message', [
      '%label' => $values['label'],
      '@message' => $e->getMessage(),
    ]));
    return;
  }
  // Save field and field storage values in tempstore.
  $this->tempStore
    ->set($this->entityTypeId . ':' . $field_name, [
    'field_storage' => $field_storage_entity,
    'field_config_values' => $field_values,
    'default_options' => $default_options,
  ]);
  // Configure next steps in the multi-part form.
  $destinations = [];
  $route_parameters = [
    'entity_type' => $this->entityTypeId,
    'field_name' => $field_name,
  ] + FieldUI::getRouteBundleParameter($entity_type, $this->bundle);
  $destinations[] = [
    'route_name' => "field_ui.field_add_{$this->entityTypeId}",
    'route_parameters' => $route_parameters,
  ];
  $destinations[] = [
    'route_name' => "entity.{$this->entityTypeId}.field_ui_fields",
    'route_parameters' => $route_parameters,
  ];
  $destination = $this->getDestinationArray();
  $destinations[] = $destination['destination'];
  $form_state->setRedirectUrl(FieldUI::getNextDestination($destinations));
  // Store new field information for any additional submit handlers.
  $form_state->set([
    'fields_added',
    '_add_new_field',
  ], $field_name);
}

Buggy or inaccurate documentation? Please file an issue. Need support? Need help programming? Connect with the Drupal community.