Commit 097303e4 authored by alexpott's avatar alexpott

Issue #1393094 by swentel, solotandem: Make the field prefix in Field ui configurable.

parent 9236b3f7
# 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'
......@@ -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');
}
......@@ -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' => '<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 @@ 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);
}
......
......@@ -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.
*/
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment