summaryrefslogtreecommitdiff
path: root/modules/field_ui
diff options
context:
space:
mode:
authorwebchick <webchick@24967.no-reply.drupal.org>2012-03-24 00:18:48 -0600
committerwebchick <webchick@24967.no-reply.drupal.org>2012-03-24 00:18:48 -0600
commitf26da2d1ef3a0fe93c793a9a982c52325c798ba6 (patch)
tree5f8e6516451a9c4260a548e6da1be2e48fb1066f /modules/field_ui
parente6c2e730a6ccd79134c2bf0ec3cb67f9107c40e0 (diff)
downloadbrdo-f26da2d1ef3a0fe93c793a9a982c52325c798ba6.tar.gz
brdo-f26da2d1ef3a0fe93c793a9a982c52325c798ba6.tar.bz2
Issue #1164812 by Niklas Fiekas, tim.plunkett, xjm: Improve UX for machine names for fields.
Diffstat (limited to 'modules/field_ui')
-rw-r--r--modules/field_ui/field_ui.admin.inc57
-rw-r--r--modules/field_ui/field_ui.css8
-rw-r--r--modules/field_ui/field_ui.test27
3 files changed, 64 insertions, 28 deletions
diff --git a/modules/field_ui/field_ui.admin.inc b/modules/field_ui/field_ui.admin.inc
index f19429df5..33ce2f05c 100644
--- a/modules/field_ui/field_ui.admin.inc
+++ b/modules/field_ui/field_ui.admin.inc
@@ -316,7 +316,7 @@ function field_ui_field_overview_form($form, &$form_state, $entity_type, $bundle
t('Label'),
t('Weight'),
t('Parent'),
- t('Name'),
+ t('Machine name'),
t('Field'),
t('Widget'),
array('data' => t('Operations'), 'colspan' => 2),
@@ -498,16 +498,24 @@ function field_ui_field_overview_form($form, &$form_state, $entity_type, $bundle
),
),
'field_name' => array(
- '#type' => 'textfield',
+ '#type' => 'machine_name',
'#title' => t('New field name'),
'#title_display' => 'invisible',
// This field should stay LTR even for RTL languages.
'#field_prefix' => '<span dir="ltr">field_',
'#field_suffix' => '</span>&lrm;',
- '#attributes' => array('dir'=>'ltr'),
- '#size' => 10,
- '#description' => t('Field name (a-z, 0-9, _)'),
+ '#size' => 15,
+ '#description' => t('A unique machine-readable name containing letters, numbers, and underscores.'),
+ // 32 characters minus the 'field_' prefix.
+ '#maxlength' => 26,
'#prefix' => '<div class="add-new-placeholder">&nbsp;</div>',
+ '#machine_name' => array(
+ 'source' => array('fields', $name, 'label'),
+ 'exists' => '_field_ui_field_name_exists',
+ 'standalone' => TRUE,
+ 'label' => '',
+ ),
+ '#required' => FALSE,
),
'type' => array(
'#type' => 'select',
@@ -678,25 +686,8 @@ function _field_ui_field_overview_form_validate_add_new($form, &$form_state) {
$field_name = $field['field_name'];
// Add the 'field_' prefix.
- if (substr($field_name, 0, 6) != 'field_') {
- $field_name = 'field_' . $field_name;
- form_set_value($form['fields']['_add_new_field']['field_name'], $field_name, $form_state);
- }
-
- // Invalid field name.
- if (!preg_match('!^field_[a-z0-9_]+$!', $field_name)) {
- form_set_error('fields][_add_new_field][field_name', t('Add new field: the field name %field_name is invalid. The name must include only lowercase unaccentuated letters, numbers, and underscores.', array('%field_name' => $field_name)));
- }
- if (strlen($field_name) > 32) {
- form_set_error('fields][_add_new_field][field_name', t("Add new field: the field name %field_name is too long. The name is limited to 32 characters, including the 'field_' prefix.", array('%field_name' => $field_name)));
- }
-
- // Field name already exists. We need to check inactive fields as well, so
- // we can't use field_info_fields().
- $fields = field_read_fields(array('field_name' => $field_name), array('include_inactive' => TRUE));
- if ($fields) {
- form_set_error('fields][_add_new_field][field_name', t('Add new field: the field name %field_name already exists.', array('%field_name' => $field_name)));
- }
+ $field_name = 'field_' . $field_name;
+ form_set_value($form['fields']['_add_new_field']['field_name'], $field_name, $form_state);
}
// Missing field type.
@@ -719,6 +710,24 @@ function _field_ui_field_overview_form_validate_add_new($form, &$form_state) {
}
/**
+ * Render API callback: Checks if a field machine name is taken.
+ *
+ * @param $value
+ * The machine name, not prefixed with 'field_'.
+ *
+ * @return
+ * Whether or not the field machine name is taken.
+ */
+function _field_ui_field_name_exists($value) {
+ // Prefix with 'field_'.
+ $field_name = 'field_' . $value;
+
+ // We need to check inactive fields as well, so we can't use
+ // field_info_fields().
+ return (bool) field_read_fields(array('field_name' => $field_name), array('include_inactive' => TRUE));
+}
+
+/**
* Validates the 'add existing field' row of field_ui_field_overview_form().
*
* @see field_ui_field_overview_form_validate()
diff --git a/modules/field_ui/field_ui.css b/modules/field_ui/field_ui.css
index ae469e207..2184023b7 100644
--- a/modules/field_ui/field_ui.css
+++ b/modules/field_ui/field_ui.css
@@ -12,6 +12,10 @@ table.field-ui-overview tr.add-new .tabledrag-changed {
}
table.field-ui-overview tr.add-new .description {
margin-bottom: 0;
+ max-width: 250px;
+}
+table.field-ui-overview tr.add-new .form-type-machine-name .description {
+ white-space: normal;
}
table.field-ui-overview tr.add-new .add-new-placeholder {
font-weight: bold;
@@ -29,6 +33,10 @@ table.field-ui-overview tr.region-populated {
table.field-ui-overview tr.region-add-new-title {
display: none;
}
+table.field-ui-overview tr.add-new td {
+ vertical-align: top;
+ white-space: nowrap;
+}
/* 'Manage display' overview */
#field-display-overview .field-formatter-summary-cell {
diff --git a/modules/field_ui/field_ui.test b/modules/field_ui/field_ui.test
index f1e770baa..75a70846f 100644
--- a/modules/field_ui/field_ui.test
+++ b/modules/field_ui/field_ui.test
@@ -172,7 +172,7 @@ class FieldUIManageFieldsTestCase extends FieldUITestCase {
// Check all table columns.
$table_headers = array(
t('Label'),
- t('Name'),
+ t('Machine name'),
t('Field'),
t('Widget'),
t('Operations'),
@@ -338,7 +338,7 @@ class FieldUIManageFieldsTestCase extends FieldUITestCase {
$bundle_path1 = 'admin/structure/types/manage/' . $this->hyphen_type;
$edit1 = array(
'fields[_add_new_field][label]' => $this->field_label,
- 'fields[_add_new_field][field_name]' => $this->field_name,
+ 'fields[_add_new_field][field_name]' => $this->field_name_input,
);
$this->fieldUIAddNewField($bundle_path1, $edit1);
@@ -426,6 +426,25 @@ class FieldUIManageFieldsTestCase extends FieldUITestCase {
$this->drupalGet('admin/structure/types/manage/' . $hyphen_type2 . '/fields');
}
+
+ /**
+ * Tests that a duplicate field name is caught by validation.
+ */
+ function testDuplicateFieldName() {
+ // field_tags already exists, so we're expecting an error when trying to
+ // create a new field with the same name.
+ $edit = array(
+ 'fields[_add_new_field][field_name]' => 'tags',
+ 'fields[_add_new_field][label]' => $this->randomName(),
+ 'fields[_add_new_field][type]' => 'taxonomy_term_reference',
+ 'fields[_add_new_field][widget_type]' => 'options_select',
+ );
+ $url = 'admin/structure/types/manage/' . $this->hyphen_type . '/fields';
+ $this->drupalPost($url, $edit, t('Save'));
+
+ $this->assertText(t('The machine-readable name is already in use. It must be unique.'));
+ $this->assertUrl($url, array(), 'Stayed on the same page.');
+ }
}
/**
@@ -454,7 +473,7 @@ class FieldUIManageDisplayTestCase extends FieldUITestCase {
// Create a field, and a node with some data for the field.
$edit = array(
'fields[_add_new_field][label]' => 'Test field',
- 'fields[_add_new_field][field_name]' => 'field_test',
+ 'fields[_add_new_field][field_name]' => 'test',
);
$this->fieldUIAddNewField($manage_fields, $edit);
@@ -499,7 +518,7 @@ class FieldUIManageDisplayTestCase extends FieldUITestCase {
// Create a field, and a node with some data for the field.
$edit = array(
'fields[_add_new_field][label]' => 'Test field',
- 'fields[_add_new_field][field_name]' => 'field_test',
+ 'fields[_add_new_field][field_name]' => 'test',
);
$this->fieldUIAddNewField('admin/structure/types/manage/' . $this->hyphen_type, $edit);
// For this test, use a formatter setting value that is an integer unlikely