diff --git a/core/modules/field_ui/config/field_ui.settings.yml b/core/modules/field_ui/config/field_ui.settings.yml new file mode 100644 index 0000000000000000000000000000000000000000..046f9a494de3f5786f47f81ed2e2d65a98510913 --- /dev/null +++ b/core/modules/field_ui/config/field_ui.settings.yml @@ -0,0 +1 @@ +field_prefix: field_ diff --git a/core/modules/field_ui/config/schema/field_ui.schema.yml b/core/modules/field_ui/config/schema/field_ui.schema.yml new file mode 100644 index 0000000000000000000000000000000000000000..fe7e95c2e052e6cee0fedf2dfdbfe6685d4b2718 --- /dev/null +++ b/core/modules/field_ui/config/schema/field_ui.schema.yml @@ -0,0 +1,9 @@ +# Schema for configuration files of the Field UI module. + +field_ui.settings: + type: mapping + label: 'Field UI settings' + mapping: + field_prefix: + type: string + label: 'The prefix for new fields created via Field UI' diff --git a/core/modules/field_ui/field_ui.install b/core/modules/field_ui/field_ui.install index 8ce3e2c1b30e6f2ddad5f5bd878ab35a6645fa9b..c434bb3b63c0ac6ef4ed061dc2beac63b23870b6 100644 --- a/core/modules/field_ui/field_ui.install +++ b/core/modules/field_ui/field_ui.install @@ -43,3 +43,12 @@ function field_ui_update_8001() { } } } + +/** + * Installs default config for Field UI. + * + * @ingroup config_upgrade + */ +function field_ui_update_8002() { + update_7_to_8_install_default_config('module', 'field_ui.settings'); +} diff --git a/core/modules/field_ui/lib/Drupal/field_ui/FieldOverview.php b/core/modules/field_ui/lib/Drupal/field_ui/FieldOverview.php index cc5ab8bf1d24392b3b4bf28811f665fee5be3953..ec602ffe4e83372b0d8aeb2a1aff5030447a92f4 100644 --- a/core/modules/field_ui/lib/Drupal/field_ui/FieldOverview.php +++ b/core/modules/field_ui/lib/Drupal/field_ui/FieldOverview.php @@ -11,6 +11,7 @@ use Drupal\Core\Entity\EntityManager; use Drupal\field\Plugin\Type\Widget\WidgetPluginManager; use Symfony\Component\DependencyInjection\ContainerInterface; +use Drupal\field\Plugin\Core\Entity\Field; /** * Field UI field overview form. @@ -92,6 +93,9 @@ public function buildForm(array $form, array &$form_state, $entity_type = NULL, $extra_fields = field_info_extra_fields($this->entity_type, $this->bundle, 'form'); $entity_form_display = entity_get_form_display($this->entity_type, $this->bundle, $this->mode); + // Field prefix. + $field_prefix = config('field_ui.settings')->get('field_prefix'); + $form += array( '#entity_type' => $this->entity_type, '#bundle' => $this->bundle, @@ -308,12 +312,13 @@ public function buildForm(array $form, array &$form_state, $entity_type = NULL, '#title' => t('New field name'), '#title_display' => 'invisible', // This field should stay LTR even for RTL languages. - '#field_prefix' => 'field_', + '#field_prefix' => '' . $field_prefix, '#field_suffix' => '‎', '#size' => 15, '#description' => t('A unique machine-readable name containing letters, numbers, and underscores.'), - // 32 characters minus the 'field_' prefix. - '#maxlength' => 26, + // Calculate characters depending on the length of the field prefix + // setting. Maximum length is 32. + '#maxlength' => Field::ID_MAX_LENGTH - strlen($field_prefix), '#prefix' => '
 
', '#machine_name' => array( 'source' => array('fields', $name, 'label'), @@ -502,8 +507,8 @@ protected function validateAddNew(array $form, array &$form_state) { else { $field_name = $field['field_name']; - // Add the 'field_' prefix. - $field_name = 'field_' . $field_name; + // Add the field prefix. + $field_name = config('field_ui.settings')->get('field_prefix') . $field_name; form_set_value($form['fields']['_add_new_field']['field_name'], $field_name, $form_state); } diff --git a/core/modules/field_ui/lib/Drupal/field_ui/Tests/ManageFieldsTest.php b/core/modules/field_ui/lib/Drupal/field_ui/Tests/ManageFieldsTest.php index c0f5471ce460f75171c392a08619ce8c8b20c669..dd362cdbe18335f1892d1eb233f6644766f436ed 100644 --- a/core/modules/field_ui/lib/Drupal/field_ui/Tests/ManageFieldsTest.php +++ b/core/modules/field_ui/lib/Drupal/field_ui/Tests/ManageFieldsTest.php @@ -244,6 +244,36 @@ function assertFieldSettings($bundle, $field_name, $string = 'dummy test string' $this->assertTrue($widget_configuration['settings']['test_widget_setting'] == $string, 'Field widget settings were found.'); } + /** + * Tests that the 'field_prefix' setting works on Field UI. + */ + function testFieldPrefix() { + // Change default field prefix. + $field_prefix = strtolower($this->randomName(10)); + \Drupal::config('field_ui.settings')->set('field_prefix', $field_prefix)->save(); + + // Create a field input and label exceeding the new maxlength, which is 22. + $field_exceed_max_length_label = $this->randomString(23); + $field_exceed_max_length_input = $this->randomName(23); + + // Try to create the field. + $edit = array( + 'fields[_add_new_field][label]' => $field_exceed_max_length_label, + 'fields[_add_new_field][field_name]' => $field_exceed_max_length_input, + ); + $this->drupalPost('admin/structure/types/manage/' . $this->type . '/fields', $edit, t('Save')); + $this->assertText('New field name cannot be longer than 22 characters but is currently 23 characters long.'); + + // Create a valid field. + $edit = array( + 'fields[_add_new_field][label]' => $this->field_label, + 'fields[_add_new_field][field_name]' => $this->field_name_input, + ); + $this->fieldUIAddNewField('admin/structure/types/manage/' . $this->type, $edit); + $this->drupalGet('admin/structure/types/manage/' . $this->type . '/fields/node.' . $this->type . '.' . $field_prefix . $this->field_name_input); + $this->assertText(format_string('@label settings for @type', array('@label' => $this->field_label, '@type' => $this->type))); + } + /** * Tests that default value is correctly validated and saved. */