diff --git a/core/modules/comment/comment.module b/core/modules/comment/comment.module index eb81824..d9f9674 100644 --- a/core/modules/comment/comment.module +++ b/core/modules/comment/comment.module @@ -320,7 +320,7 @@ function comment_form_field_ui_field_storage_add_form_alter(&$form, FormStateInt } if (!_comment_entity_uses_integer_id($form_state->get('entity_type_id'))) { // You cannot use comment fields on entity types with non-integer IDs. - unset($form['add']['new_field_type']['#options']['comment']); + unset($form['add']['new_storage_type']['#options']['comment']); } } diff --git a/core/modules/comment/src/Tests/CommentNonNodeTest.php b/core/modules/comment/src/Tests/CommentNonNodeTest.php index b2f22e0..9189d42 100644 --- a/core/modules/comment/src/Tests/CommentNonNodeTest.php +++ b/core/modules/comment/src/Tests/CommentNonNodeTest.php @@ -411,9 +411,9 @@ public function testsNonIntegerIdEntities() { // Visit the Field UI field add page. $this->drupalGet('entity_test_string_id/structure/entity_test/fields/add-field'); // Ensure field isn't shown for string IDs. - $this->assertNoOption('edit-new-field-type', 'comment'); + $this->assertNoOption('edit-new-storage-type', 'comment'); // Ensure a core field type shown. - $this->assertOption('edit-new-field-type', 'boolean'); + $this->assertOption('edit-new-storage-type', 'boolean'); // Create a bundle for entity_test_no_id. entity_test_create_bundle('entity_test', 'Entity Test', 'entity_test_no_id'); @@ -423,9 +423,9 @@ public function testsNonIntegerIdEntities() { // Visit the Field UI field add page. $this->drupalGet('entity_test_no_id/structure/entity_test/fields/add-field'); // Ensure field isn't shown for empty IDs. - $this->assertNoOption('edit-new-field-type', 'comment'); + $this->assertNoOption('edit-new-storage-type', 'comment'); // Ensure a core field type shown. - $this->assertOption('edit-new-field-type', 'boolean'); + $this->assertOption('edit-new-storage-type', 'boolean'); } } diff --git a/core/modules/entity_reference/src/Tests/EntityReferenceAdminTest.php b/core/modules/entity_reference/src/Tests/EntityReferenceAdminTest.php index f09eb90..a13a75b 100644 --- a/core/modules/entity_reference/src/Tests/EntityReferenceAdminTest.php +++ b/core/modules/entity_reference/src/Tests/EntityReferenceAdminTest.php @@ -61,7 +61,7 @@ public function testFieldAdminHandler() { $this->drupalPostForm($bundle_path . '/fields/add-field', array( 'label' => 'Test label', 'field_name' => 'test', - 'new_field_type' => 'entity_reference', + 'new_storage_type' => 'entity_reference', ), t('Save and continue')); // Node should be selected by default. diff --git a/core/modules/field_ui/field_ui.js b/core/modules/field_ui/field_ui.js index 0839833..021dfb5 100644 --- a/core/modules/field_ui/field_ui.js +++ b/core/modules/field_ui/field_ui.js @@ -9,16 +9,20 @@ Drupal.behaviors.fieldUIFieldStorageAddForm = { attach: function (context) { - var $newFieldType = $(context).find('select[name="new_field_type"]').once('new-field-type'); - var $existingStorageName = $(context).find('select[name="existing_storage_name"]').once('existing-storage-name'); - - if ($newFieldType.length) { - // Add the 'form-required' css class here. We can not use the Form API + var $form = $(context).find('#field-ui-field-storage-add-form').once('field_ui_add'); + if ($form.length) { + // Add a few '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. - $(context).find('.form-item-label label').addClass('form-required'); - $(context).find('.form-item-field-name label').addClass('form-required'); + $(context).find( + '.form-item-label label,' + + '.form-item-field-name label,' + + '.form-item-existing-storage-label label') + .addClass('form-required'); + + var $newFieldType = $(context).find('select[name="new_storage_type"]'); + var $existingStorageName = $(context).find('select[name="existing_storage_name"]'); // When the user selects a new field type, clear the "existing field" // selection. @@ -28,12 +32,6 @@ $existingStorageName.val('').change(); } }); - } - - if ($existingStorageName.length) { - // Add the 'form-required' css class to the label of the form element. - // The actual validation will happen server-side. - $(context).find('.form-item-existing-storage-label label').addClass('form-required'); // When the user selects an existing storage name, clear the "new field // type" selection and populate the 'existing_storage_label' element. diff --git a/core/modules/field_ui/src/Form/FieldStorageAddForm.php b/core/modules/field_ui/src/Form/FieldStorageAddForm.php index cc5c7e7..a9fb7f6 100644 --- a/core/modules/field_ui/src/Form/FieldStorageAddForm.php +++ b/core/modules/field_ui/src/Form/FieldStorageAddForm.php @@ -144,7 +144,7 @@ public function buildForm(array $form, FormStateInterface $form_state, $entity_t '#attributes' => array('class' => array('field-type-wrapper', 'clearfix')), ); - $form['add']['new_field_type'] = array( + $form['add']['new_storage_type'] = array( '#type' => 'select', '#title' => $this->t('Add a new field'), '#options' => $field_type_options, @@ -175,22 +175,22 @@ public function buildForm(array $form, FormStateInterface $form_state, $entity_t } // Field label and field_name. - $form['add_storage_wrapper'] = array( + $form['new_storage_wrapper'] = array( '#type' => 'container', '#states' => array( '!visible' => array( - ':input[name="new_field_type"]' => array('value' => ''), + ':input[name="new_storage_type"]' => array('value' => ''), ), ), ); - $form['add_storage_wrapper']['label'] = array( + $form['new_storage_wrapper']['label'] = array( '#type' => 'textfield', '#title' => $this->t('Label'), '#size' => 15, ); $field_prefix = $this->config('field_ui.settings')->get('field_prefix'); - $form['add_storage_wrapper']['field_name'] = array( + $form['new_storage_wrapper']['field_name'] = array( '#type' => 'machine_name', // This field should stay LTR even for RTL languages. '#field_prefix' => '' . $field_prefix, @@ -201,7 +201,7 @@ public function buildForm(array $form, FormStateInterface $form_state, $entity_t // setting. Maximum length is 32. '#maxlength' => FieldStorageConfig::NAME_MAX_LENGTH - strlen($field_prefix), '#machine_name' => array( - 'source' => array('add_storage_wrapper', 'label'), + 'source' => array('new_storage_wrapper', 'label'), 'exists' => array($this, 'fieldNameExists'), ), '#required' => FALSE, @@ -247,13 +247,13 @@ public function buildForm(array $form, FormStateInterface $form_state, $entity_t */ public function validateForm(array &$form, FormStateInterface $form_state) { // Missing field type. - if (!$form_state->getValue('new_field_type') && !$form_state->getValue('existing_storage_name')) { - $form_state->setErrorByName('new_field_type', $this->t('You need to select a field type or an existing field.')); + if (!$form_state->getValue('new_storage_type') && !$form_state->getValue('existing_storage_name')) { + $form_state->setErrorByName('new_storage_type', $this->t('You need to select a field type or an existing field.')); } // Both field type and existing field option selected. This is prevented in // the UI with JavaScript but we also need a proper server-side validation. - elseif ($form_state->getValue('new_field_type') && $form_state->getValue('existing_storage_name')) { - $form_state->setErrorByName('new_field_type', $this->t('Adding a new field and re-using an existing field at the same time is not allowed.')); + elseif ($form_state->getValue('new_storage_type') && $form_state->getValue('existing_storage_name')) { + $form_state->setErrorByName('new_storage_type', $this->t('Adding a new field and re-using an existing field at the same time is not allowed.')); return; } @@ -273,7 +273,7 @@ public function validateForm(array &$form, FormStateInterface $form_state) { */ protected function validateAddNew(array $form, FormStateInterface $form_state) { // Validate if any information was provided in the 'add new field' case. - if ($form_state->getValue('new_field_type')) { + if ($form_state->getValue('new_storage_type')) { // Missing label. if (!$form_state->getValue('label')) { $form_state->setErrorByName('label', $this->t('Add new field: you need to provide a label.')); @@ -289,7 +289,7 @@ protected function validateAddNew(array $form, FormStateInterface $form_state) { // Add the field prefix. $field_name = $this->configFactory->get('field_ui.settings')->get('field_prefix') . $field_name; - $form_state->setValueForElement($form['add_storage_wrapper']['field_name'], $field_name); + $form_state->setValueForElement($form['new_storage_wrapper']['field_name'], $field_name); } } } @@ -322,13 +322,13 @@ public function submitForm(array &$form, FormStateInterface $form_state) { $destinations = array(); // Create new field. - if ($values['new_field_type']) { + if ($values['new_storage_type']) { // Create the field storage and field. try { $this->entityManager->getStorage('field_storage_config')->create(array( 'field_name' => $values['field_name'], 'entity_type' => $this->entityTypeId, - 'type' => $values['new_field_type'], + 'type' => $values['new_storage_type'], 'translatable' => $values['translatable'], ))->save(); diff --git a/core/modules/field_ui/src/Tests/FieldUiTestTrait.php b/core/modules/field_ui/src/Tests/FieldUiTestTrait.php index ccb8751..8b14eda 100644 --- a/core/modules/field_ui/src/Tests/FieldUiTestTrait.php +++ b/core/modules/field_ui/src/Tests/FieldUiTestTrait.php @@ -34,7 +34,7 @@ public function fieldUIAddNewField($bundle_path, $field_name, $label = NULL, $field_type = 'test_field', array $storage_edit = array(), array $field_edit = array()) { $label = $label ?: $this->randomString(); $initial_edit = array( - 'new_field_type' => $field_type, + 'new_storage_type' => $field_type, 'label' => $label, 'field_name' => $field_name, ); diff --git a/core/modules/field_ui/src/Tests/ManageFieldsTest.php b/core/modules/field_ui/src/Tests/ManageFieldsTest.php index 12142d0..335b63b 100644 --- a/core/modules/field_ui/src/Tests/ManageFieldsTest.php +++ b/core/modules/field_ui/src/Tests/ManageFieldsTest.php @@ -427,7 +427,7 @@ function testDisallowedFieldNames() { $label = 'Disallowed field'; $edit = array( 'label' => $label, - 'new_field_type' => 'test_field', + 'new_storage_type' => 'test_field', ); // Try with an entity key. @@ -486,8 +486,8 @@ function testLockedField() { function testHiddenFields() { // Check that the field type is not available in the 'add new field' row. $this->drupalGet('admin/structure/types/manage/' . $this->type . '/fields/add-field'); - $this->assertFalse($this->xpath('//select[@id="edit-new-field-type"]//option[@value="hidden_test_field"]'), "The 'add new field' select respects field types 'no_ui' property."); - $this->assertTrue($this->xpath('//select[@id="edit-new-field-type"]//option[@value="shape"]'), "The 'add new field' select shows a valid option."); + $this->assertFalse($this->xpath('//select[@id="edit-new-storage-type"]//option[@value="hidden_test_field"]'), "The 'add new field' select respects field types 'no_ui' property."); + $this->assertTrue($this->xpath('//select[@id="edit-new-storage-type"]//option[@value="shape"]'), "The 'add new field' select shows a valid option."); // Create a field storage and a field programmatically. $field_name = 'hidden_test_field'; @@ -523,10 +523,10 @@ function testHiddenFields() { $field_types = \Drupal::service('plugin.manager.field.field_type')->getDefinitions(); foreach ($field_types as $field_type => $definition) { if (empty($definition['no_ui'])) { - $this->assertTrue($this->xpath('//select[@id="edit-new-field-type"]//option[@value=:field_type]', array(':field_type' => $field_type)), String::format('Configurable field type @field_type is available.', array('@field_type' => $field_type))); + $this->assertTrue($this->xpath('//select[@id="edit-new-storage-type"]//option[@value=:field_type]', array(':field_type' => $field_type)), String::format('Configurable field type @field_type is available.', array('@field_type' => $field_type))); } else { - $this->assertFalse($this->xpath('//select[@id="edit-new-field-type"]//option[@value=:field_type]', array(':field_type' => $field_type)), String::format('Non-configurable field type @field_type is not available.', array('@field_type' => $field_type))); + $this->assertFalse($this->xpath('//select[@id="edit-new-storage-type"]//option[@value=:field_type]', array(':field_type' => $field_type)), String::format('Non-configurable field type @field_type is not available.', array('@field_type' => $field_type))); } } } @@ -553,7 +553,7 @@ function testDuplicateFieldName() { $edit = array( 'field_name' => 'tags', 'label' => $this->randomMachineName(), - 'new_field_type' => 'taxonomy_term_reference', + 'new_storage_type' => 'taxonomy_term_reference', ); $url = 'admin/structure/types/manage/' . $this->type . '/fields/add-field'; $this->drupalPostForm($url, $edit, t('Save and continue'));