function FieldStorageAddForm::buildForm
Same name in other branches
- 9 core/modules/field_ui/src/Form/FieldStorageAddForm.php \Drupal\field_ui\Form\FieldStorageAddForm::buildForm()
- 8.9.x core/modules/field_ui/src/Form/FieldStorageAddForm.php \Drupal\field_ui\Form\FieldStorageAddForm::buildForm()
- 10 core/modules/field_ui/src/Form/FieldStorageAddForm.php \Drupal\field_ui\Form\FieldStorageAddForm::buildForm()
Overrides FormInterface::buildForm
File
-
core/
modules/ field_ui/ src/ Form/ FieldStorageAddForm.php, line 76
Class
- FieldStorageAddForm
- Provides a form for the "field storage" add subform.
Namespace
Drupal\field_ui\FormCode
public function buildForm(array $form, FormStateInterface $form_state, $entity_type_id = NULL, $bundle = NULL, $selected_field_type = NULL, $display_as_group = 'false') {
$display_as_group = str_contains($display_as_group, 'true');
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);
}
if (!$form_state->get('field_type')) {
$form_state->set('field_type', $selected_field_type);
}
if (!$form_state->get('display_as_group')) {
$form_state->set('display_as_group', $display_as_group);
}
$this->entityTypeId = $form_state->get('entity_type_id');
$this->bundle = $form_state->get('bundle');
$unique_definitions = [];
$grouped_definitions = $this->fieldTypePluginManager
->getGroupedDefinitions($this->fieldTypePluginManager
->getEntityTypeUiDefinitions($this->entityTypeId), 'label', 'id');
if (array_key_exists($selected_field_type, $grouped_definitions)) {
$field_types = $grouped_definitions[$selected_field_type];
foreach ($field_types as $name => $field_type) {
$unique_definitions[$selected_field_type][$name] = [
'unique_identifier' => $name,
] + $field_type;
}
}
$entity_type = $this->entityTypeManager
->getDefinition($this->entityTypeId);
$route_parameters_back = [] + FieldUI::getRouteBundleParameter($entity_type, $this->bundle);
$form['actions'] = [
'#type' => 'actions',
];
$form['label'] = [
'#type' => 'textfield',
'#title' => $this->t('Label'),
'#size' => 30,
'#required' => TRUE,
'#maxlength' => 255,
'#weight' => -20,
];
$field_prefix = $this->configFactory
->get('field_ui.settings')
->get('field_prefix');
$form['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' => [
'label',
],
'exists' => [
$this,
'fieldNameExists',
],
],
'#required' => TRUE,
];
$form['field_options_wrapper'] = [
'#prefix' => '<div class="field-options-wrapper">',
'#suffix' => '</div>',
];
// Set the selected field to the form state by checking
// the checked attribute.
if (isset($selected_field_type)) {
if ($display_as_group) {
$form['field_options_wrapper']['label'] = [
'#type' => 'label',
'#title' => $this->t('Choose a field type'),
'#required' => TRUE,
];
$form['field_options_wrapper']['fields'] = [
'#type' => 'container',
'#attributes' => [
'class' => [
'group-field-options',
],
],
];
foreach ($unique_definitions[$selected_field_type] as $option_key => $option) {
$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' => Html::getClass($option['unique_identifier']),
'#weight' => $option['weight'],
'#parents' => [
'field_options_wrapper',
],
'#attributes' => [
'class' => [
'field-option-radio',
],
'data-once' => 'field-click-to-select',
'checked' => $this->getRequest()->request
->get('field_options_wrapper') !== NULL && $this->getRequest()->request
->get('field_options_wrapper') == $option_key,
],
'#wrapper_attributes' => [
'class' => [
'js-click-to-select',
'subfield-option',
],
],
'#variant' => 'field-suboption',
];
$radio_element['#return_value'] = $option['unique_identifier'];
if ((string) $option['unique_identifier'] === 'entity_reference') {
$radio_element['#title'] = 'Other';
$radio_element['#weight'] = 10;
}
$group_field_options[$option['unique_identifier']] = $radio_element;
}
uasort($group_field_options, [
SortArray::class,
'sortByWeightProperty',
]);
$form['field_options_wrapper']['fields'] += $group_field_options;
}
$form['actions']['previous'] = [
'#type' => 'link',
'#title' => $this->t('Change field type'),
'#url' => Url::fromRoute("field_ui.field_storage_config_add_{$entity_type_id}", $route_parameters_back),
'#attributes' => [
'class' => [
'button',
'use-ajax',
],
'data-dialog-type' => 'modal',
'data-dialog-options' => Json::encode([
'width' => '1100',
]),
],
];
$form['actions']['submit'] = [
'#type' => 'submit',
'#value' => $this->t('Continue'),
'#submit' => [
'::submitForm',
],
'#attributes' => [
'class' => [
'button',
'button--primary',
],
'data-dialog-type' => 'modal',
'data-dialog-options' => Json::encode([
'width' => '1100',
]),
],
];
if ($this->isAjax()) {
$form['actions']['submit']['#ajax']['callback'] = '::ajaxSubmit';
}
}
// Place the 'translatable' property as an explicit value so that contrib
// modules can form_alter() the value for newly created fields. By default,
// we create field storage as translatable, so it will be possible to enable
// translation at field level.
$form['translatable'] = [
'#type' => 'value',
'#value' => TRUE,
];
$form['#prefix'] = '<div id="field-storage-subfield">';
$form['#suffix'] = '</div>';
$form['#attached']['library'] = [
'field_ui/drupal.field_ui',
'field_ui/drupal.field_ui.manage_fields',
'core/drupal.dialog.ajax',
];
return $form;
}
Buggy or inaccurate documentation? Please file an issue. Need support? Need help programming? Connect with the Drupal community.