summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorAlex Pott2013-06-17 21:14:16 (GMT)
committerAlex Pott2013-06-17 21:14:16 (GMT)
commit097303e4bf3b1741f6aae689785f9655f0f023f8 (patch)
tree21e1942237c942dd0ebf7ac56548d5df7e44b998
parent9236b3f7eec1d1f523e2836b0c4499c5ed830a2d (diff)
Issue #1393094 by swentel, solotandem: Make the field prefix in Field ui configurable.
-rw-r--r--core/modules/field_ui/config/field_ui.settings.yml1
-rw-r--r--core/modules/field_ui/config/schema/field_ui.schema.yml9
-rw-r--r--core/modules/field_ui/field_ui.install9
-rw-r--r--core/modules/field_ui/lib/Drupal/field_ui/FieldOverview.php15
-rw-r--r--core/modules/field_ui/lib/Drupal/field_ui/Tests/ManageFieldsTest.php30
5 files changed, 59 insertions, 5 deletions
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 0000000..046f9a4
--- /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 0000000..fe7e95c
--- /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 8ce3e2c..c434bb3 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 cc5ab8b..ec602ff 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\field_ui\OverviewBase;
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 @@ class FieldOverview extends OverviewBase {
$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 @@ class FieldOverview extends OverviewBase {
'#title' => t('New field name'),
'#title_display' => 'invisible',
// This field should stay LTR even for RTL languages.
- '#field_prefix' => '<span dir="ltr">field_',
+ '#field_prefix' => '<span dir="ltr">' . $field_prefix,
'#field_suffix' => '</span>&lrm;',
'#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' => '<div class="add-new-placeholder">&nbsp;</div>',
'#machine_name' => array(
'source' => array('fields', $name, 'label'),
@@ -502,8 +507,8 @@ class FieldOverview extends OverviewBase {
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 c0f5471..dd362cd 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
@@ -245,6 +245,36 @@ class ManageFieldsTest extends FieldUiTestBase {
}
/**
+ * 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.
*/
function testDefaultValue() {