function FieldStorageAddForm::submitForm

Same name and namespace in other branches
  1. 9 core/modules/field_ui/src/Form/FieldStorageAddForm.php \Drupal\field_ui\Form\FieldStorageAddForm::submitForm()
  2. 10 core/modules/field_ui/src/Form/FieldStorageAddForm.php \Drupal\field_ui\Form\FieldStorageAddForm::submitForm()
  3. 11.x core/modules/field_ui/src/Form/FieldStorageAddForm.php \Drupal\field_ui\Form\FieldStorageAddForm::submitForm()

Overrides FormInterface::submitForm

File

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

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) {
    $error = FALSE;
    $values = $form_state->getValues();
    $destinations = [];
    $entity_type = $this->entityTypeManager
        ->getDefinition($this->entityTypeId);
    // Create new field.
    if ($values['new_storage_type']) {
        $field_storage_values = [
            'field_name' => $values['field_name'],
            'entity_type' => $this->entityTypeId,
            'type' => $values['new_storage_type'],
            'translatable' => $values['translatable'],
        ];
        $field_values = [
            'field_name' => $values['field_name'],
            'entity_type' => $this->entityTypeId,
            'bundle' => $this->bundle,
            'label' => $values['label'],
            // Field translatability should be explicitly enabled by the users.
'translatable' => FALSE,
        ];
        $widget_id = $formatter_id = NULL;
        $widget_settings = $formatter_settings = [];
        // Check if we're dealing with a preconfigured field.
        if (strpos($field_storage_values['type'], 'field_ui:') !== FALSE) {
            list(, $field_type, $option_key) = explode(':', $field_storage_values['type'], 3);
            $field_storage_values['type'] = $field_type;
            $field_definition = $this->fieldTypePluginManager
                ->getDefinition($field_type);
            $options = $this->fieldTypePluginManager
                ->getPreconfiguredOptions($field_definition['id']);
            $field_options = $options[$option_key];
            // Merge in preconfigured field storage options.
            if (isset($field_options['field_storage_config'])) {
                foreach ([
                    'cardinality',
                    'settings',
                ] as $key) {
                    if (isset($field_options['field_storage_config'][$key])) {
                        $field_storage_values[$key] = $field_options['field_storage_config'][$key];
                    }
                }
            }
            // Merge in preconfigured field options.
            if (isset($field_options['field_config'])) {
                foreach ([
                    'required',
                    'settings',
                ] as $key) {
                    if (isset($field_options['field_config'][$key])) {
                        $field_values[$key] = $field_options['field_config'][$key];
                    }
                }
            }
            $widget_id = isset($field_options['entity_form_display']['type']) ? $field_options['entity_form_display']['type'] : NULL;
            $widget_settings = isset($field_options['entity_form_display']['settings']) ? $field_options['entity_form_display']['settings'] : [];
            $formatter_id = isset($field_options['entity_view_display']['type']) ? $field_options['entity_view_display']['type'] : NULL;
            $formatter_settings = isset($field_options['entity_view_display']['settings']) ? $field_options['entity_view_display']['settings'] : [];
        }
        // Create the field storage and field.
        try {
            $this->entityTypeManager
                ->getStorage('field_storage_config')
                ->create($field_storage_values)
                ->save();
            $field = $this->entityTypeManager
                ->getStorage('field_config')
                ->create($field_values);
            $field->save();
            $this->configureEntityFormDisplay($values['field_name'], $widget_id, $widget_settings);
            $this->configureEntityViewDisplay($values['field_name'], $formatter_id, $formatter_settings);
            // Always show the field settings step, as the cardinality needs to be
            // configured for new fields.
            $route_parameters = [
                'field_config' => $field->id(),
            ] + FieldUI::getRouteBundleParameter($entity_type, $this->bundle);
            $destinations[] = [
                'route_name' => "entity.field_config.{$this->entityTypeId}_storage_edit_form",
                'route_parameters' => $route_parameters,
            ];
            $destinations[] = [
                'route_name' => "entity.field_config.{$this->entityTypeId}_field_edit_form",
                'route_parameters' => $route_parameters,
            ];
            $destinations[] = [
                'route_name' => "entity.{$this->entityTypeId}.field_ui_fields",
                'route_parameters' => $route_parameters,
            ];
            // Store new field information for any additional submit handlers.
            $form_state->set([
                'fields_added',
                '_add_new_field',
            ], $values['field_name']);
        } catch (\Exception $e) {
            $error = TRUE;
            $this->messenger()
                ->addError($this->t('There was a problem creating field %label: @message', [
                '%label' => $values['label'],
                '@message' => $e->getMessage(),
            ]));
        }
    }
    // Re-use existing field.
    if ($values['existing_storage_name']) {
        $field_name = $values['existing_storage_name'];
        try {
            $field = $this->entityTypeManager
                ->getStorage('field_config')
                ->create([
                'field_name' => $field_name,
                'entity_type' => $this->entityTypeId,
                'bundle' => $this->bundle,
                'label' => $values['existing_storage_label'],
            ]);
            $field->save();
            $this->configureEntityFormDisplay($field_name);
            $this->configureEntityViewDisplay($field_name);
            $route_parameters = [
                'field_config' => $field->id(),
            ] + FieldUI::getRouteBundleParameter($entity_type, $this->bundle);
            $destinations[] = [
                'route_name' => "entity.field_config.{$this->entityTypeId}_field_edit_form",
                'route_parameters' => $route_parameters,
            ];
            $destinations[] = [
                'route_name' => "entity.{$this->entityTypeId}.field_ui_fields",
                'route_parameters' => $route_parameters,
            ];
            // Store new field information for any additional submit handlers.
            $form_state->set([
                'fields_added',
                '_add_existing_field',
            ], $field_name);
        } catch (\Exception $e) {
            $error = TRUE;
            $this->messenger()
                ->addError($this->t('There was a problem creating field %label: @message', [
                '%label' => $values['label'],
                '@message' => $e->getMessage(),
            ]));
        }
    }
    if ($destinations) {
        $destination = $this->getDestinationArray();
        $destinations[] = $destination['destination'];
        $form_state->setRedirectUrl(FieldUI::getNextDestination($destinations, $form_state));
    }
    elseif (!$error) {
        $this->messenger()
            ->addStatus($this->t('Your settings have been saved.'));
    }
}

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