diff -u b/core/modules/field_ui/src/Form/FieldStorageAddForm.php b/core/modules/field_ui/src/Form/FieldStorageAddForm.php --- b/core/modules/field_ui/src/Form/FieldStorageAddForm.php +++ b/core/modules/field_ui/src/Form/FieldStorageAddForm.php @@ -114,6 +114,7 @@ $form['add'] = [ '#type' => 'container', '#attributes' => ['class' => ['form--inline', 'clearfix']], + '#weight' => -10, ]; $form['add']['new_storage_type'] = [ @@ -140,7 +141,8 @@ '#empty_option' => $this->t('- Select an existing field -'), ]; - $form['#attached']['drupalSettings']['existingFieldLabels'] = $this->getExistingFieldLabels(array_keys($existing_field_storage_options)); + $existing_field_labels = $this->getExistingFieldLabels(array_keys($existing_field_storage_options)); + $form['#attached']['drupalSettings']['existingFieldLabels'] = $existing_field_labels; } else { // Provide a placeholder form element to simplify the validation code. @@ -150,22 +152,41 @@ ]; } - // New field storage subform wrapper. + $form['add']['rebuild'] = [ + '#type' => 'submit', + '#value' => $this->t('Update'), + '#attributes' => ['class' => ['js-hide'], 'style' => ['margin-top: 2.2em']], + '#limit_validation_errors' => [], + '#submit' => ['::newStorageTypeSubmit'], + ]; + + $user_input = $form_state->getUserInput(); + + $existing_storage_name = isset($user_input['existing_storage_name']) ? $user_input['existing_storage_name'] : ''; + if ($existing_storage_name) { + // Field label and field_name. + $form['label'] = [ + '#type' => 'value', + '#value' => $existing_field_labels[$existing_storage_name], + ]; + } + $form['new_storage_wrapper'] = [ '#type' => 'container', '#id' => 'new-storage-wrapper', ]; - - if ($form_state->isRebuilding() && ($new_storage_type = $form_state->getValue('new_storage_type'))) { + $new_storage_type = isset($user_input['new_storage_type']) ? $user_input['new_storage_type'] : ''; + if ($new_storage_type) { + // New field storage subform wrapper. + $field_prefix = $this->config('field_ui.settings')->get('field_prefix'); // Field label and field_name. $form['new_storage_wrapper']['label'] = [ '#type' => 'textfield', '#title' => $this->t('Label'), '#size' => 15, + '#required' => TRUE, '#weight' => -15, ]; - - $field_prefix = $this->config('field_ui.settings')->get('field_prefix'); $form['new_storage_wrapper']['field_name'] = [ '#type' => 'machine_name', // This field should stay LTR even for RTL languages. @@ -180,7 +201,7 @@ 'source' => ['new_storage_wrapper', 'label'], 'exists' => [$this, 'fieldNameExists'], ], - '#required' => FALSE, + '#required' => TRUE, '#weight' => -14, ]; @@ -227,18 +248,2 @@ - // Provide a separate label element for the "Re-use existing field" case - // and place it outside the $form['add'] wrapper because those elements - // are displayed inline. - if ($existing_field_storage_options) { - $form['existing_storage_label'] = [ - '#type' => 'textfield', - '#title' => $this->t('Label'), - '#size' => 15, - '#states' => [ - '!visible' => [ - ':input[name="existing_storage_name"]' => ['value' => ''], - ], - ], - ]; - } - // Place the 'translatable' property as an explicit value so that contrib @@ -294,7 +299,6 @@ } $this->validateAddNew($form, $form_state); - $this->validateAddExisting($form, $form_state); } /** @@ -338,19 +341,0 @@ - /** - * Validates the 're-use existing field' case. - * - * @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. - * - * @see \Drupal\field_ui\Form\FieldStorageAddForm::validateForm() - */ - protected function validateAddExisting(array $form, FormStateInterface $form_state) { - if ($form_state->getValue('existing_storage_name')) { - // Missing label. - if (!$form_state->getValue('existing_storage_label')) { - $form_state->setErrorByName('existing_storage_label', $this->t('Re-use existing field: you need to provide a label.')); - } - } - } - @@ -414,12 +399,15 @@ $field_name = $values['existing_storage_name']; try { - $field = $this->entityManager->getStorage('field_config')->create([ + $definition = [ 'field_name' => $field_name, 'entity_type' => $this->entityTypeId, 'bundle' => $this->bundle, - 'label' => $values['existing_storage_label'], - ]); + ]; + if ($values['label'] != 'undefined') { + $definition['label'] = $values['label']; + } + $field = $this->entityManager->getStorage('field_config')->create($definition); $field->save(); $this->configureEntityFormDisplay($field_name); only in patch2: unchanged: --- a/core/modules/field_ui/field_ui.es6.js +++ b/core/modules/field_ui/field_ui.es6.js @@ -16,26 +16,11 @@ .find('[data-drupal-selector="field-ui-field-storage-add-form"]') .once('field_ui_add'); if ($form.length) { - // Add a few 'js-form-required' and 'form-required' css classes here. - // We can not use the Form API '#required' property because both label - // elements for "add new" and "re-use existing" can never be filled and - // submitted at the same time. The actual validation will happen - // server-side. - $form - .find( - '.js-form-item-label label,' + - '.js-form-item-field-name label,' + - '.js-form-item-existing-storage-label label', - ) - .addClass('js-form-required form-required'); - const $newFieldType = $form.find('select[name="new_storage_type"]'); + const $newFieldName = $form.find('input[name="field_name"]'); 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. @@ -47,21 +32,13 @@ }); // When the user selects an existing storage name, clear the "new field - // type" selection and populate the 'existing_storage_label' element. + // type" selection. $existingStorageName.on('change', function() { const value = $(this).val(); if (value !== '') { // Reset the "new field type" selection. $newFieldType.val('').trigger('change'); - - // Pre-populate the "existing storage label" element. - if ( - typeof drupalSettings.existingFieldLabels[value] !== 'undefined' - ) { - $existingStorageLabel.val( - drupalSettings.existingFieldLabels[value], - ); - } + $newFieldName.val('').trigger('change'); } }); } only in patch2: unchanged: --- a/core/modules/field_ui/field_ui.js +++ b/core/modules/field_ui/field_ui.js @@ -10,11 +10,9 @@ attach: function attach(context) { var $form = $(context).find('[data-drupal-selector="field-ui-field-storage-add-form"]').once('field_ui_add'); if ($form.length) { - $form.find('.js-form-item-label label,' + '.js-form-item-field-name label,' + '.js-form-item-existing-storage-label label').addClass('js-form-required form-required'); - var $newFieldType = $form.find('select[name="new_storage_type"]'); + var $newFieldName = $form.find('input[name="field_name"]'); var $existingStorageName = $form.find('select[name="existing_storage_name"]'); - var $existingStorageLabel = $form.find('input[name="existing_storage_label"]'); $newFieldType.on('change', function () { if ($(this).val() !== '') { @@ -26,10 +24,7 @@ var value = $(this).val(); if (value !== '') { $newFieldType.val('').trigger('change'); - - if (typeof drupalSettings.existingFieldLabels[value] !== 'undefined') { - $existingStorageLabel.val(drupalSettings.existingFieldLabels[value]); - } + $newFieldName.val('').trigger('change'); } }); }