function FieldStorageAddForm::addGroupFieldOptions

Adds ungrouped field types and field type groups to the form.

When a group is selected, the related fields are shown when the form is rebuilt.

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::addGroupFieldOptions()
FieldStorageAddForm::buildForm in core/modules/field_ui/src/Form/FieldStorageAddForm.php
Form constructor.

File

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

Class

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

Namespace

Drupal\field_ui\Form

Code

protected function addGroupFieldOptions(array &$form, FormStateInterface $form_state) : void {
  $field_type_options_radios = [];
  foreach ($form_state->get('field_type_options') as $id => $field_type) {
    /** @var  \Drupal\Core\Field\FieldTypeCategoryInterface $category_info */
    $category_info = $this->fieldTypeCategoryManager
      ->createInstance($field_type['category'], $field_type);
    $display_as_group = $field_type['display_as_group'];
    $cleaned_class_name = Html::getClass($field_type['unique_identifier']);
    $field_type_options_radios[$id] = [
      '#type' => 'container',
      '#attributes' => [
        'class' => [
          'field-option',
          'js-click-to-select',
        ],
      ],
      '#weight' => $category_info->getWeight(),
      'thumb' => [
        '#type' => 'container',
        '#attributes' => [
          'class' => [
            'field-option__thumb',
          ],
        ],
        'icon' => [
          '#type' => 'container',
          '#attributes' => [
            'class' => [
              'field-option__icon',
              $display_as_group ? "field-icon-{$field_type['category']}" : "field-icon-{$cleaned_class_name}",
            ],
          ],
        ],
      ],
      'radio' => [
        '#type' => 'radio',
        '#title' => $category_info->getLabel(),
        '#parents' => [
          'new_storage_type',
        ],
        '#title_display' => 'before',
        '#description_display' => 'before',
        '#theme_wrappers' => [
          'form_element__new_storage_type',
        ],
        // If it is a category, set return value as the category label.
        // Otherwise, set it as the field type id.
'#return_value' => $display_as_group ? $field_type['category'] : $field_type['unique_identifier'],
        '#attributes' => [
          'class' => [
            'field-option-radio',
          ],
        ],
        '#description' => [
          '#type' => 'container',
          '#attributes' => [
            'class' => [
              'field-option__description',
            ],
          ],
          '#markup' => $category_info->getDescription(),
        ],
        '#variant' => 'field-option',
      ],
    ];
    if ($libraries = $category_info->getLibraries()) {
      $field_type_options_radios[$id]['#attached']['library'] = $libraries;
    }
  }
  uasort($field_type_options_radios, [
    SortArray::class,
    'sortByWeightProperty',
  ]);
  $form['add-label'] = [
    '#type' => 'label',
    '#title' => $this->t('Choose a type of field'),
    '#required' => TRUE,
  ];
  $form['add'] = [
    '#type' => 'container',
    '#attributes' => [
      'class' => 'add-field-container',
    ],
  ];
  $form['add']['new_storage_type'] = $field_type_options_radios;
  $form['actions']['submit']['#validate'][] = '::validateGroupOrField';
  $form['actions']['submit']['#submit'][] = '::rebuildWithOptions';
}

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