function FieldStorageAddForm::getNewFieldDefaults

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

Get default options from preconfigured options for a new field.

Parameters

string $field_name: The machine name of the field.

string $preset_key: A key in the preconfigured options array for the field.

Return value

array An array of settings with keys 'field_storage_config', 'field_config', 'entity_form_display', and 'entity_view_display'.

Throws

\Drupal\Component\Plugin\Exception\PluginNotFoundException

See also

\Drupal\Core\Field\PreconfiguredFieldUiOptionsInterface::getPreconfiguredOptions()

1 call to FieldStorageAddForm::getNewFieldDefaults()
FieldStorageAddForm::submitForm in core/modules/field_ui/src/Form/FieldStorageAddForm.php
Form submission handler.

File

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

Class

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

Namespace

Drupal\field_ui\Form

Code

protected function getNewFieldDefaults(string $field_name, string $preset_key) : array {
    $field_type_definition = $this->fieldTypePluginManager
        ->getDefinition($field_name);
    $options = $this->fieldTypePluginManager
        ->getPreconfiguredOptions($field_type_definition['id']);
    $field_options = $options[$preset_key] ?? [];
    $default_options = [];
    // 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])) {
                $default_options['field_storage_config'][$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])) {
                $default_options['field_config'][$key] = $field_options['field_config'][$key];
            }
        }
    }
    // Preconfigured options only apply to the default display modes.
    foreach ([
        'entity_form_display',
        'entity_view_display',
    ] as $key) {
        if (isset($field_options[$key])) {
            $default_options[$key] = [
                'default' => array_intersect_key($field_options[$key], [
                    'type' => '',
                    'settings' => [],
                ]),
            ];
        }
        else {
            $default_options[$key] = [
                'default' => [],
            ];
        }
    }
    return $default_options;
}

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