function FieldStorageAddForm::addFieldOptionsForGroup

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

Adds field types for the selected group to the form.

Parameters

array $form: An associative array containing the structure of the form.

\Drupal\Core\Form\FormStateInterface $form_state: The current state of the form.

1 call to FieldStorageAddForm::addFieldOptionsForGroup()
FieldStorageAddForm::buildForm in core/modules/field_ui/src/Form/FieldStorageAddForm.php
Form constructor.

File

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

Class

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

Namespace

Drupal\field_ui\Form

Code

protected function addFieldOptionsForGroup(array &$form, FormStateInterface $form_state) : void {
    // Field label and field_name.
    $form['new_storage_wrapper'] = [
        '#type' => 'container',
        '#attributes' => [
            'class' => [
                'field-ui-new-storage-wrapper',
            ],
        ],
    ];
    $form['new_storage_wrapper']['label'] = [
        '#type' => 'textfield',
        '#title' => $this->t('Label'),
        '#size' => 30,
    ];
    $field_prefix = $this->config('field_ui.settings')
        ->get('field_prefix');
    $form['new_storage_wrapper']['field_name'] = [
        '#type' => 'machine_name',
        '#field_prefix' => $field_prefix,
        '#size' => 15,
        '#description' => $this->t('A unique machine-readable name containing letters, numbers, and underscores.'),
        // Calculate characters depending on the length of the field prefix
        // setting. Maximum length is 32.
'#maxlength' => FieldStorageConfig::NAME_MAX_LENGTH - strlen($field_prefix),
        '#machine_name' => [
            'source' => [
                'new_storage_wrapper',
                'label',
            ],
            'exists' => [
                $this,
                'fieldNameExists',
            ],
        ],
        '#required' => FALSE,
    ];
    $form['actions']['submit']['#validate'][] = '::validateFieldType';
    $form['actions']['back'] = [
        '#type' => 'submit',
        '#value' => $this->t('Back'),
        '#submit' => [
            '::startOver',
        ],
    ];
    $field_type_options = $form_state->get('field_type_options');
    $new_storage_type = $form_state->getValue('new_storage_type');
    $form['new_storage_type'] = [
        '#type' => 'value',
        '#value' => $new_storage_type,
    ];
    if (!isset($new_storage_type) || !$field_type_options[$new_storage_type]['display_as_group']) {
        return;
    }
    // Create a wrapper for all the field options to be provided.
    $form['group_field_options_wrapper'] = [
        '#prefix' => '<div id="group-field-options-wrapper" class="group-field-options-wrapper">',
        '#suffix' => '</div>',
    ];
    $form['group_field_options_wrapper']['label'] = [
        '#type' => 'label',
        '#title' => $this->t('Choose an option below'),
        '#required' => TRUE,
    ];
    $form['group_field_options_wrapper']['fields'] = [
        '#type' => 'container',
        '#attributes' => [
            'class' => [
                'group-field-options',
            ],
        ],
    ];
    $unique_definitions = $form_state->get('unique_definitions')[$new_storage_type] ?? [];
    $group_field_options = [];
    foreach ($unique_definitions as $option) {
        $identifier = $option['unique_identifier'];
        // If the field type plugin's annotation defines description as an
        // array, render it as an item_list.
        $description = !is_array($option['description']) ? $option['description'] : [
            '#theme' => 'item_list',
            '#items' => $option['description'],
        ];
        $radio_element = [
            '#type' => 'radio',
            '#theme_wrappers' => [
                'form_element__new_storage_type',
            ],
            '#title' => $option['label'],
            '#description' => $description,
            '#id' => $identifier,
            '#weight' => $option['weight'],
            '#parents' => [
                'group_field_options_wrapper',
            ],
            '#attributes' => [
                'class' => [
                    'field-option-radio',
                ],
                'data-once' => 'field-click-to-select',
            ],
            '#wrapper_attributes' => [
                'class' => [
                    'js-click-to-select',
                    'subfield-option',
                ],
            ],
            '#variant' => 'field-suboption',
            '#return_value' => $identifier,
        ];
        if ($identifier === 'entity_reference') {
            $radio_element['#title'] = 'Other';
            $radio_element['#weight'] = 10;
        }
        $group_field_options[$identifier] = $radio_element;
    }
    uasort($group_field_options, [
        SortArray::class,
        'sortByWeightProperty',
    ]);
    $form['group_field_options_wrapper']['fields'] += $group_field_options;
}

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