function FieldStorageReuseForm::buildForm

Same name and namespace in other branches
  1. 10 core/modules/field_ui/src/Form/FieldStorageReuseForm.php \Drupal\field_ui\Form\FieldStorageReuseForm::buildForm()

Overrides FormInterface::buildForm

File

core/modules/field_ui/src/Form/FieldStorageReuseForm.php, line 85

Class

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

Namespace

Drupal\field_ui\Form

Code

public function buildForm(array $form, FormStateInterface $form_state, $entity_type_id = NULL, $bundle = NULL) {
    if (!$form_state->get('entity_type_id')) {
        $form_state->set('entity_type_id', $entity_type_id);
    }
    if (!$form_state->get('bundle')) {
        $form_state->set('bundle', $bundle);
    }
    $this->entityTypeId = $form_state->get('entity_type_id');
    $this->bundle = $form_state->get('bundle');
    $form['text'] = [
        '#plain_text' => $this->t("You can re-use a field from other sub-types of the same entity type. Re-using a field creates another usage of the same field storage."),
    ];
    $form['search'] = [
        '#type' => 'search',
        '#title' => $this->t('Filter by field or field type'),
        '#attributes' => [
            'class' => [
                'js-table-filter-text',
            ],
            'data-table' => '.js-reuse-table',
            'autocomplete' => 'off',
        ],
    ];
    $form['add'] = [
        '#type' => 'container',
        '#attributes' => [
            'class' => [
                'form--inline',
                'clearfix',
            ],
        ],
    ];
    $bundles = $this->bundleInfoService
        ->getAllBundleInfo();
    $existing_field_storage_options = $this->getExistingFieldStorageOptions();
    $rows = [];
    foreach ($existing_field_storage_options as $field) {
        $field_bundles = $field['field_storage']->getBundles();
        $summary = $this->fieldTypePluginManager
            ->getStorageSettingsSummary($field['field_storage']);
        $cardinality = $field['field_storage']->getCardinality();
        $readable_cardinality = $cardinality === -1 ? $this->t('Unlimited') : new PluralTranslatableMarkup(1, 'Single value', 'Multiple values: @cardinality', [
            '@cardinality' => $cardinality,
        ]);
        // Remove empty values.
        $list = array_filter([
            $summary,
            $readable_cardinality,
        ]);
        $settings_summary = [
            '#theme' => 'item_list',
            '#items' => $list,
            '#attributes' => [
                'class' => [
                    'field-settings-summary-cell',
                ],
            ],
        ];
        $bundle_label_arr = [];
        foreach ($field_bundles as $bundle) {
            $bundle_label_arr[] = $bundles[$this->entityTypeId][$bundle]['label'];
        }
        sort($bundle_label_arr);
        // Combine bundles to be a single string separated by a comma.
        $settings_summary['#items'][] = $this->t('Used in: @list', [
            '@list' => implode(", ", $bundle_label_arr),
        ]);
        $row = [
            '#attributes' => [
                'data-field-id' => $field["field_name"],
            ],
            'field' => [
                '#plain_text' => $field['field_name'],
                '#type' => 'item',
            ],
            'field_type' => [
                '#plain_text' => $field['field_type'],
                '#type' => 'item',
            ],
            'summary' => $settings_summary,
            'operations' => [
                '#type' => 'submit',
                '#name' => $field['field_name'],
                '#value' => $this->t('Re-use'),
                '#wrapper_attributes' => [
                    'colspan' => 5,
                ],
                '#attributes' => [
                    'class' => [
                        'button',
                        'button--small',
                        'use-ajax',
                    ],
                    'aria-label' => $this->t('Reuse @field_name', [
                        '@field_name' => $field['field_name'],
                    ]),
                    'data-dialog-type' => 'modal',
                ],
                '#submit' => [
                    'callback' => [
                        $this,
                        'reuseCallback',
                    ],
                ],
            ],
        ];
        $rows[] = $row;
    }
    // Sort rows by field name.
    ksort($rows);
    $form['add']['table'] = [
        '#type' => 'table',
        '#header' => [
            $this->t('Field'),
            $this->t('Field Type'),
            $this->t('Summary'),
            $this->t('Operations'),
        ],
        '#attributes' => [
            'class' => [
                'js-reuse-table',
            ],
        ],
    ];
    $form['add']['table'] += $rows;
    $form['#attached']['library'][] = 'field_ui/drupal.field_ui';
    return $form;
}

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