Skip to content
Snippets Groups Projects
Verified Commit 0f1bc004 authored by Lauri Timmanee's avatar Lauri Timmanee
Browse files

Issue #3409271 by benjifisher: Clean up code in FieldStorageAddForm and related JS

parent cbbe53da
Branches
Tags 2.0.0
35 merge requests!8528Issue #3456871 by Tim Bozeman: Support NULL services,!8323Fix source code editing and in place front page site studio editing.,!6278Issue #3187770 by godotislate, smustgrave, catch, quietone: Views Rendered...,!3878Removed unused condition head title for views,!38582585169-10.1.x,!3818Issue #2140179: $entity->original gets stale between updates,!3742Issue #3328429: Create item list field formatter for displaying ordered and unordered lists,!3731Claro: role=button on status report items,!3668Resolve #3347842 "Deprecate the trusted",!3651Issue #3347736: Create new SDC component for Olivero (header-search),!3531Issue #3336994: StringFormatter always displays links to entity even if the user in context does not have access,!3502Issue #3335308: Confusing behavior with FormState::setFormState and FormState::setMethod,!3478Issue #3337882: Deleted menus are not removed from content type config,!3355Issue #3209129: Scrolling problems when adding a block via layout builder,!3226Issue #2987537: Custom menu link entity type should not declare "bundle" entity key,!3154Fixes #2987987 - CSRF token validation broken on routes with optional parameters.,!3147Issue #3328457: Replace most substr($a, $i) where $i is negative with str_ends_with(),!3146Issue #3328456: Replace substr($a, 0, $i) with str_starts_with(),!3133core/modules/system/css/components/hidden.module.css,!2964Issue #2865710 : Dependencies from only one instance of a widget are used in display modes,!2812Issue #3312049: [Followup] Fix Drupal.Commenting.FunctionComment.MissingReturnType returns for NULL,!2614Issue #2981326: Replace non-test usages of \Drupal::logger() with IoC injection,!2378Issue #2875033: Optimize joins and table selection in SQL entity query implementation,!2334Issue #3228209: Add hasRole() method to AccountInterface,!2062Issue #3246454: Add weekly granularity to views date sort,!1105Issue #3025039: New non translatable field on translatable content throws error,!1073issue #3191727: Focus states on mobile second level navigation items fixed,!10223132456: Fix issue where views instances are emptied before an ajax request is complete,!877Issue #2708101: Default value for link text is not saved,!844Resolve #3036010 "Updaters",!673Issue #3214208: FinishResponseSubscriber could create duplicate headers,!617Issue #3043725: Provide a Entity Handler for user cancelation,!579Issue #2230909: Simple decimals fail to pass validation,!560Move callback classRemove outside of the loop,!555Issue #3202493
Pipeline #65839 passed
Pipeline: drupal

#65840

    ......@@ -31,47 +31,6 @@
    '.js-form-item-existing-storage-label label',
    )
    .addClass('js-form-required form-required');
    const $newFieldType = $form.find('select[name="new_storage_type"]');
    const $existingStorageName = $form.find(
    'select[name="existing_storage_name"]',
    );
    const $existingStorageLabel = $form.find(
    'input[name="existing_storage_label"]',
    );
    // When the user selects a new field type, clear the "existing field"
    // selection.
    $newFieldType.on('change', function () {
    if (this.value !== '') {
    // Reset the "existing storage name" selection.
    if ($existingStorageName.length) {
    $existingStorageName[0].value = '';
    $existingStorageName.trigger('change');
    }
    }
    });
    // When the user selects an existing storage name, clear the "new field
    // type" selection and populate the 'existing_storage_label' element.
    $existingStorageName.on('change', function () {
    const { value } = this;
    if (value !== '') {
    if ($newFieldType.length) {
    // Reset the "new field type" selection.
    $newFieldType[0].value = '';
    $newFieldType.trigger('change');
    }
    // Pre-populate the "existing storage label" element.
    if (
    typeof drupalSettings.existingFieldLabels[value] !== 'undefined'
    ) {
    $existingStorageLabel[0].value =
    drupalSettings.existingFieldLabels[value];
    }
    }
    });
    }
    },
    };
    ......
    ......@@ -97,13 +97,6 @@ public function __construct(EntityTypeManagerInterface $entity_type_manager, Fie
    }
    }
    /**
    * {@inheritdoc}
    */
    public function getFormId() {
    return 'field_ui_field_storage_add_form';
    }
    /**
    * {@inheritdoc}
    */
    ......@@ -118,6 +111,13 @@ public static function create(ContainerInterface $container) {
    );
    }
    /**
    * {@inheritdoc}
    */
    public function getFormId() {
    return 'field_ui_field_storage_add_form';
    }
    /**
    * {@inheritdoc}
    */
    ......@@ -156,8 +156,8 @@ public function buildForm(array $form, FormStateInterface $form_state, $entity_t
    'field_ui/drupal.field_ui.manage_fields',
    'core/drupal.ajax',
    ];
    // The group info is stored in new_storage_type.
    if ($form_state->getValue('new_storage_type')) {
    if ($form_state->hasValue('new_storage_type')) {
    // A group is already selected. Show field types for that group.
    $this->addFieldOptionsForGroup($form, $form_state);
    }
    ......@@ -170,108 +170,36 @@ public function buildForm(array $form, FormStateInterface $form_state, $entity_t
    }
    /**
    * Adds field types for the selected group to the form.
    * Save field type definitions and categories in the form state.
    *
    * Get all field type definitions and store each one twice:
    * - field_type_options: each field type is indexed by its category plugin ID
    * or its label.
    * - unique_definitions: each field type is indexed by its category and name.
    *
    * @param array $form
    * An associative array containing the structure of the form.
    * @param \Drupal\Core\Form\FormStateInterface $form_state
    * The current state of the form.
    */
    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'][] = '::validateAddNew';
    $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');
    $group_field_options = [];
    foreach ($unique_definitions[$new_storage_type] as $option_key => $option) {
    $radio_element = [
    '#type' => 'radio',
    '#theme_wrappers' => ['form_element__new_storage_type'],
    '#title' => $option['label'],
    '#description' => [
    '#theme' => 'item_list',
    '#items' => $unique_definitions[$new_storage_type][$option_key]['description'],
    ],
    '#id' => $option['unique_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',
    ];
    $radio_element['#return_value'] = $option['unique_identifier'];
    if ((string) $option['unique_identifier'] === 'entity_reference') {
    $radio_element['#title'] = 'Other';
    $radio_element['#weight'] = 10;
    protected function processFieldDefinitions(FormStateInterface $form_state): void {
    $field_type_options = $unique_definitions = [];
    $grouped_definitions = $this->fieldTypePluginManager
    ->getGroupedDefinitions($this->fieldTypePluginManager->getEntityTypeUiDefinitions($this->entityTypeId), 'label', 'id');
    foreach ($grouped_definitions as $category => $field_types) {
    foreach ($field_types as $name => $field_type) {
    $definition = ['unique_identifier' => $name] + $field_type;
    $category_info = $this->fieldTypeCategoryManager
    ->createInstance($field_type['category'], $definition);
    $definition['display_as_group'] = !($category_info instanceof FallbackFieldTypeCategory);
    $id = $this->fieldTypeCategoryManager->hasDefinition($category)
    ? $category_info->getPluginId()
    : (string) $field_type['label'];
    $field_type_options[$id] = $definition;
    $unique_definitions[$category][$name] = $definition;
    }
    $group_field_options[$option['unique_identifier']] = $radio_element;
    }
    uasort($group_field_options, [SortArray::class, 'sortByWeightProperty']);
    $form['group_field_options_wrapper']['fields'] += $group_field_options;
    $form_state->set('field_type_options', $field_type_options);
    $form_state->set('unique_definitions', $unique_definitions);
    }
    /**
    ......@@ -286,18 +214,17 @@ protected function addFieldOptionsForGroup(array &$form, FormStateInterface $for
    * The current state of the form.
    */
    protected function addGroupFieldOptions(array &$form, FormStateInterface $form_state): void {
    $field_type_options = $form_state->get('field_type_options');
    $field_type_options_radios = [];
    foreach ($field_type_options as $id => $field_type) {
    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);
    $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'],
    'checked' => $this->getRequest()->request->get('new_storage_type') !== NULL && $this->getRequest()->request->get('new_storage_type') == ($display_as_group ? $field_type['category'] : $field_type['unique_identifier']),
    ],
    '#weight' => $category_info->getWeight(),
    'thumb' => [
    ......@@ -308,8 +235,9 @@ protected function addGroupFieldOptions(array &$form, FormStateInterface $form_s
    'icon' => [
    '#type' => 'container',
    '#attributes' => [
    'class' => ['field-option__icon', $display_as_group ?
    "field-icon-$field_type[category]" : "field-icon-$cleaned_class_name",
    'class' => [
    'field-option__icon',
    $display_as_group ? "field-icon-$field_type[category]" : "field-icon-$cleaned_class_name",
    ],
    ],
    ],
    ......@@ -321,8 +249,8 @@ protected function addGroupFieldOptions(array &$form, FormStateInterface $form_s
    '#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.
    // 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'],
    ......@@ -358,54 +286,116 @@ protected function addGroupFieldOptions(array &$form, FormStateInterface $form_s
    ];
    $form['add']['new_storage_type'] = $field_type_options_radios;
    $form['group_submit'] = [
    '#type' => 'submit',
    '#value' => $this->t('Change field group'),
    '#limit_validation_errors' => [],
    '#attributes' => [
    'class' => ['js-hide'],
    ],
    '#submit' => [[static::class, 'rebuildWithOptions']],
    ];
    $form['actions']['submit']['#validate'][] = '::validateGroupOrField';
    $form['actions']['submit']['#submit'][] = '::rebuildWithOptions';
    }
    /**
    * Save field type definitions and categories in the form state.
    *
    * Get all field type definitions and store each one twice:
    * - field_type_options: each field type is indexed by its category plugin ID
    * or its label.
    * - unique_definitions: each field type is indexed by its category and name.
    * Adds field types for the selected group to the form.
    *
    * @param array $form
    * An associative array containing the structure of the form.
    * @param \Drupal\Core\Form\FormStateInterface $form_state
    * The current state of the form.
    */
    protected function processFieldDefinitions(FormStateInterface $form_state): void {
    $field_type_options = $unique_definitions = [];
    $grouped_definitions = $this->fieldTypePluginManager->getGroupedDefinitions($this->fieldTypePluginManager->getEntityTypeUiDefinitions($this->entityTypeId), 'label', 'id');
    // Invoke a hook to get category properties.
    foreach ($grouped_definitions as $category => $field_types) {
    foreach ($field_types as $name => $field_type) {
    $definition = ['unique_identifier' => $name] + $field_type;
    $category_info = $this->fieldTypeCategoryManager
    ->createInstance($field_type['category'], $definition);
    $definition['display_as_group'] = !($category_info instanceof FallbackFieldTypeCategory);
    if ($this->fieldTypeCategoryManager->hasDefinition($category)) {
    $id = $category_info->getPluginId();
    }
    else {
    $id = (string) $field_type['label'];
    }
    $field_type_options[$id] = $definition;
    $unique_definitions[$category][$name] = $definition;
    }
    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;
    }
    $form_state->set('field_type_options', $field_type_options);
    $form_state->set('unique_definitions', $unique_definitions);
    // 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'];
    $radio_element = [
    '#type' => 'radio',
    '#theme_wrappers' => ['form_element__new_storage_type'],
    '#title' => $option['label'],
    '#description' => [
    '#theme' => 'item_list',
    '#items' => $option['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;
    }
    /**
    ......@@ -430,7 +420,7 @@ public function validateGroupOrField(array &$form, FormStateInterface $form_stat
    * @param \Drupal\Core\Form\FormStateInterface $form_state
    * The current state of the form.
    */
    public function validateAddNew(array $form, FormStateInterface $form_state) {
    public function validateFieldType(array $form, FormStateInterface $form_state) {
    // Missing label.
    if (!$form_state->getValue('label')) {
    $form_state->setErrorByName('label', $this->t('Add new field: you need to provide a label.'));
    ......@@ -466,15 +456,15 @@ public function submitForm(array &$form, FormStateInterface $form_state) {
    'entity_type' => $this->entityTypeId,
    'bundle' => $this->bundle,
    ];
    $default_options = [];
    // Check if we're dealing with a preconfigured field.
    if (strpos($field_storage_type, 'field_ui:') === 0) {
    if (str_starts_with($field_storage_type, 'field_ui:')) {
    [, $field_type, $preset_key] = explode(':', $field_storage_type, 3);
    $default_options = $this->getNewFieldDefaults($field_type, $preset_key);
    }
    else {
    $field_type = $field_storage_type;
    $default_options = [];
    }
    $field_values += [
    ...$default_options['field_config'] ?? [],
    ......@@ -493,7 +483,9 @@ public function submitForm(array &$form, FormStateInterface $form_state) {
    ];
    try {
    $field_storage_entity = $this->entityTypeManager->getStorage('field_storage_config')->create($field_storage_values);
    $field_storage_entity = $this->entityTypeManager
    ->getStorage('field_storage_config')
    ->create($field_storage_values);
    }
    catch (\Exception $e) {
    $this->messenger()->addError($this->t('There was a problem creating field %label: @message', ['%label' => $values['label'], '@message' => $e->getMessage()]));
    ......
    0% Loading or .
    You are about to add 0 people to the discussion. Proceed with caution.
    Please register or to comment