Commit 7822393c authored by catch's avatar catch

Issue #2370305 by Gábor Hojtsy, yched: Refactor field type configuration...

Issue #2370305 by Gábor Hojtsy, yched: Refactor field type configuration schemas for DX, easier to find errors
parent 6714300a
......@@ -378,12 +378,16 @@ field_config_base:
type: boolean
label: 'Translatable'
default_value:
type: field.[%parent.field_type].value
type: sequence
label: 'Default values'
sequence:
- type: field.value.[%parent.%parent.field_type]
label: 'Default value'
default_value_callback:
type: string
label: 'Default value callback'
settings:
type: field.[%parent.field_type].field_settings
type: field.field_settings.[%parent.field_type]
third_party_settings:
type: sequence
label: 'Third party settings'
......@@ -417,9 +421,23 @@ core.date_format.*:
type: string
label: 'Default language'
# Generic field settings schemas.
field.storage_settings.*:
type: mapping
label: 'Settings'
field.field_settings.*:
type: mapping
label: 'Settings'
field.value.*:
type: mapping
label: 'Default value'
# Schema for the configuration of the String field type.
field.string.storage_settings:
field.storage_settings.string:
type: mapping
label: 'String settings'
mapping:
......@@ -427,45 +445,39 @@ field.string.storage_settings:
type: integer
label: 'Maximum length'
field.string.field_settings:
type: sequence
field.field_settings.string:
type: mapping
label: 'String settings'
field.string.value:
type: sequence
field.value.string:
type: mapping
label: 'Default value'
sequence:
- type: mapping
label: 'Default'
mapping:
value:
type: string
label: 'Value'
mapping:
value:
type: string
label: 'Value'
# Schema for the configuration of the String (long) field type.
field.string_long.storage_settings:
type: field.string.storage_settings
field.storage_settings.string_long:
type: field.storage_settings.string
label: 'String (long) settings'
field.string_long.field_settings:
type: field.string.field_settings
field.field_settings.string_long:
type: field.field_settings.string
label: 'String (long) settings'
field.string_long.value:
type: sequence
field.value.string_long:
type: mapping
label: 'Default value'
sequence:
- type: mapping
label: 'Default'
mapping:
value:
type: text
label: 'Value'
mapping:
value:
type: text
label: 'Value'
# Schema for the configuration of the URI field type.
field.uri.storage_settings:
field.storage_settings.uri:
type: mapping
label: 'URI settings'
mapping:
......@@ -473,66 +485,57 @@ field.uri.storage_settings:
type: integer
label: 'Maximum length'
field.uri.field_settings:
type: sequence
field.field_settings.uri:
type: mapping
label: 'URI settings'
field.uri.value:
type: sequence
field.value.uri:
type: mapping
label: 'Default value'
sequence:
- type: mapping
label: 'Default'
mapping:
value:
type: string
label: 'Value'
mapping:
value:
type: string
label: 'Value'
# Schema for the configuration of the Created field type.
field.created.storage_settings:
type: sequence
field.storage_settings.created:
type: mapping
label: 'Created timestamp settings'
field.created.field_settings:
type: sequence
field.field_settings.created:
type: mapping
label: 'Created timestamp settings'
field.created.value:
type: sequence
field.value.created:
type: mapping
label: 'Default value'
sequence:
- type: mapping
label: 'Default'
mapping:
value:
type: integer
label: 'Value'
mapping:
value:
type: integer
label: 'Value'
# Schema for the configuration of the Changed field type.
field.changed.storage_settings:
type: sequence
field.storage_settings.changed:
type: mapping
label: 'Changed timestamp settings'
field.changed.field_settings:
type: sequence
field.field_settings.changed:
type: mapping
label: 'Changed timestamp settings'
field.changed.value:
type: sequence
field.value.changed:
type: mapping
label: 'Default value'
sequence:
- type: mapping
label: 'Default'
mapping:
value:
type: integer
label: 'Value'
mapping:
value:
type: integer
label: 'Value'
# Schema for the configuration of the Entity reference field type.
field.entity_reference.storage_settings:
field.storage_settings.entity_reference:
type: mapping
label: 'Entity reference settings'
mapping:
......@@ -540,7 +543,7 @@ field.entity_reference.storage_settings:
type: string
label: 'Type of item to reference'
field.entity_reference.field_settings:
field.field_settings.entity_reference:
type: mapping
label: 'Entity reference settings'
mapping:
......@@ -551,23 +554,20 @@ field.entity_reference.field_settings:
type: entity_reference.[%parent.handler].handler_settings
label: 'Reference method settings'
field.entity_reference.value:
type: sequence
field.value.entity_reference:
type: mapping
label: 'Default value'
sequence:
- type: mapping
label: 'Default'
mapping:
target_id:
type: string
label: 'Value'
target_uuid:
type: string
label: 'Target UUID'
mapping:
target_id:
type: string
label: 'Value'
target_uuid:
type: string
label: 'Target UUID'
# Schema for the configuration of the Boolean field type.
field.boolean.storage_settings:
field.storage_settings.boolean:
type: mapping
label: 'Boolean settings'
mapping:
......@@ -578,50 +578,41 @@ field.boolean.storage_settings:
type: string
label: 'Off label'
field.boolean.field_settings:
field.field_settings.boolean:
label: 'Boolean settings'
type: sequence
type: mapping
field.boolean.value:
type: sequence
label: 'Default value'
sequence:
- type: mapping
label: 'Default'
mapping:
value:
type: integer
label: 'Value'
field.value.boolean:
type: mapping
mapping:
value:
type: integer
label: 'Value'
# Schema for the configuration of the Email field type.
field.email.storage_settings:
type: sequence
field.storage_settings.email:
type: mapping
label: 'Email settings'
sequence:
- type: string
field.email.field_settings:
type: sequence
field.field_settings.email:
type: mapping
label: 'Email settings'
sequence:
- type: string
label: 'Setting'
field.email.value:
type: sequence
field.value.email:
type: mapping
label: 'Default value'
sequence:
- type: mapping
label: 'Default'
mapping:
value:
type: email
label: 'Value'
mapping:
value:
type: email
label: 'Value'
# Schema for the configuration of the Integer field type.
field.integer.storage_settings:
field.storage_settings.integer:
type: mapping
label: 'Integer settings'
mapping:
......@@ -632,7 +623,7 @@ field.integer.storage_settings:
type: string
label: 'Database storage size'
field.integer.field_settings:
field.field_settings.integer:
type: mapping
label: 'Integer'
mapping:
......@@ -649,20 +640,17 @@ field.integer.field_settings:
type: string
label: 'Suffix'
field.integer.value:
type: sequence
field.value.integer:
type: mapping
label: 'Default value'
sequence:
- type: mapping
label: 'Default value'
mapping:
value:
type: integer
label: 'Value'
mapping:
value:
type: integer
label: 'Value'
# Schema for the configuration of the Decimal field type.
field.decimal.storage_settings:
field.storage_settings.decimal:
type: mapping
label: 'Decimal settings'
mapping:
......@@ -673,9 +661,9 @@ field.decimal.storage_settings:
type: integer
label: 'Scale'
field.decimal.field_settings:
field.field_settings.decimal:
type: mapping
label: 'Decimal'
label: 'Decimal settings'
mapping:
min:
type: float
......@@ -690,29 +678,23 @@ field.decimal.field_settings:
type: string
label: 'Suffix'
field.decimal.value:
type: sequence
field.value.decimal:
type: mapping
label: 'Default value'
sequence:
- type: mapping
label: 'Default value'
mapping:
value:
type: float
label: 'Value'
mapping:
value:
type: float
label: 'Value'
# Schema for the configuration of the Float field type.
field.float.storage_settings:
type: sequence
field.storage_settings.float:
type: mapping
label: 'Float settings'
sequence:
- type: string
label: 'setting'
field.float.field_settings:
field.field_settings.float:
type: mapping
label: 'Float'
label: 'Float settings'
mapping:
min:
type: float
......@@ -727,16 +709,13 @@ field.float.field_settings:
type: string
label: 'Suffix'
field.float.value:
type: sequence
field.value.float:
type: mapping
label: 'Default value'
sequence:
- type: mapping
label: 'Default value'
mapping:
value:
type: float
label: 'Value'
mapping:
value:
type: float
label: 'Value'
# Text with a text format.
text_format:
......
......@@ -154,8 +154,12 @@ protected function getBaseFieldDefinition() {
* BaseFieldOverride::allowBundleRename() has not been called.
*/
public function preSave(EntityStorageInterface $storage) {
// Set the default instance settings.
$this->settings += \Drupal::service('plugin.manager.field.field_type')->getDefaultFieldSettings($this->getType());
// Filter out unknown settings and make sure all settings are present, so
// that a complete field definition is passed to the various hooks and
// written to config.
$field_type_manager = \Drupal::service('plugin.manager.field.field_type');
$default_settings = $field_type_manager->getDefaultFieldSettings($this->getType());
$this->settings = array_intersect_key($this->settings, $default_settings) + $default_settings;
// Call the parent's presave method to perform validate and calculate
// dependencies.
......
......@@ -52,40 +52,38 @@ comment.type.*:
type: text
label: 'Description'
field.comment.storage_settings:
type: sequence
label: 'Settings'
sequence:
- type: string
label: 'Setting'
field.storage_settings.comment:
type: mapping
label: 'Comment settings'
mapping:
comment_type:
label: 'Comment type'
type: string
field.comment.value:
type: sequence
label: 'Settings'
sequence:
- type: mapping
label: 'Settings'
mapping:
status:
type: integer
label: 'Status'
cid:
type: integer
label: 'Status'
last_comment_timestamp:
type: integer
label: 'Last comment timestamp'
last_comment_name:
type: integer
label: 'Name'
last_comment_uid:
type: integer
label: 'UID'
comment_count:
type: integer
label: 'Count'
field.value.comment:
type: mapping
label: 'Default value'
mapping:
status:
type: integer
label: 'Comment status'
cid:
type: integer
label: 'Last comment ID'
last_comment_timestamp:
type: integer
label: 'Last comment timestamp'
last_comment_name:
type: integer
label: 'Last comment name'
last_comment_uid:
type: integer
label: 'Last comment user ID'
comment_count:
type: integer
label: 'Number of comments'
field.comment.field_settings:
field.field_settings.comment:
type: mapping
label: 'Comment settings'
mapping:
......
# Schema for the configuration files of the Datetime module.
field.datetime.storage_settings:
field.storage_settings.datetime:
type: mapping
label: 'Datetime settings'
mapping:
......@@ -8,23 +8,20 @@ field.datetime.storage_settings:
type: string
label: 'Date type'
field.datetime.field_settings:
type: sequence
label: 'Settings'
sequence:
- type: string
field.field_settings.datetime:
type: mapping
label: 'Datetime settings'
field.datetime.value:
type: sequence
field.value.datetime:
type: mapping
label: 'Default value'
sequence:
- type: sequence
label: 'Default value'
sequence:
- type: string
label: 'Type'
- type: string
label: 'Value'
mapping:
default_date_type:
type: string
label: 'Default date type'
default_date:
type: string
label: 'Default date value'
field.formatter.settings.datetime_default:
type: mapping
......
......@@ -25,7 +25,7 @@ field.storage.*.*:
type: string
label: 'Type'
settings:
type: field.[%parent.type].storage_settings
type: field.storage_settings.[%parent.type]
module:
type: string
label: 'Module'
......
......@@ -138,9 +138,13 @@ public function preSave(EntityStorageInterface $storage) {
$storage_definition = $this->getFieldStorageDefinition();
// Filter out unknown settings and make sure all settings are present, so
// that a complete field definition is passed to the various hooks and
// written to config.
$default_settings = $field_type_manager->getDefaultFieldSettings($storage_definition->type);
$this->settings = array_intersect_key($this->settings, $default_settings) + $default_settings;
if ($this->isNew()) {
// Set the default field settings.
$this->settings += $field_type_manager->getDefaultFieldSettings($storage_definition->type);
// Notify the entity storage.
$entity_manager->getStorage($this->entity_type)->onFieldDefinitionCreate($this);
}
......@@ -155,8 +159,6 @@ public function preSave(EntityStorageInterface $storage) {
if ($storage_definition->uuid() != $this->original->getFieldStorageDefinition()->uuid()) {
throw new FieldException("Cannot change an existing field's storage.");
}
// Set the default field settings.
$this->settings += $field_type_manager->getDefaultFieldSettings($storage_definition->type);
// Notify the entity storage.
$entity_manager->getStorage($this->entity_type)->onFieldDefinitionUpdate($this, $this->original);
}
......
......@@ -253,6 +253,13 @@ public function preSave(EntityStorageInterface $storage) {
// Clear the derived data about the field.
unset($this->schema);
// Filter out unknown settings and make sure all settings are present, so
// that a complete field definition is passed to the various hooks and
// written to config.
$field_type_manager = \Drupal::service('plugin.manager.field.field_type');
$default_settings = $field_type_manager->getDefaultStorageSettings($this->type);
$this->settings = array_intersect_key($this->settings, $default_settings) + $default_settings;
if ($this->isNew()) {
$this->preSaveNew($storage);
}
......@@ -303,10 +310,6 @@ protected function preSaveNew(EntityStorageInterface $storage) {
}
$this->module = $field_type['provider'];
// Make sure all settings are present, so that a complete field
// definition is passed to the various hooks and written to config.
$this->settings += $field_type_manager->getDefaultStorageSettings($this->type);
// Notify the entity manager.
$entity_manager->onFieldStorageDefinitionCreate($this);
}
......@@ -333,7 +336,6 @@ public function calculateDependencies() {
protected function preSaveUpdated(EntityStorageInterface $storage) {
$module_handler = \Drupal::moduleHandler();
$entity_manager = \Drupal::entityManager();
$field_type_manager = \Drupal::service('plugin.manager.field.field_type');
// Some updates are always disallowed.
if ($this->type != $this->original->type) {
......@@ -343,10 +345,6 @@ protected function preSaveUpdated(EntityStorageInterface $storage) {
throw new FieldException("Cannot change the entity type for an existing field storage.");
}
// Make sure all settings are present, so that a complete field
// definition is passed to the various hooks and written to config.
$this->settings += $field_type_manager->getDefaultStorageSettings($this->type);
// See if any module forbids the update by throwing an exception. This
// invokes hook_field_storage_config_update_forbid().
$module_handler->invokeAll('field_storage_config_update_forbid', array($this, $this->original));
......
......@@ -22,7 +22,7 @@ file.settings:
type: path
label: 'Directory'
field.file.storage_settings:
field.storage_settings.file:
type: base_entity_reference_field_settings
label: 'File settings'
mapping:
......@@ -36,12 +36,9 @@ field.file.storage_settings:
type: string
label: 'Upload destination'
field.file.value:
type: sequence
field.value.file:
type: mapping
label: 'Default value'
sequence:
- type: string
label: 'Value'
base_file_field_field_settings:
type: mapping
......@@ -59,7 +56,7 @@ base_file_field_field_settings:
type: string
label: 'Maximum upload size'
field.file.field_settings:
field.field_settings.file:
type: base_file_field_field_settings
label: 'File settings'
mapping:
......
......@@ -83,15 +83,15 @@ image.settings:
type: boolean
label: 'Suppress the itok query string for image derivatives'
field.image.storage_settings:
type: field.file.storage_settings
field.storage_settings.image:
type: field.storage_settings.file
label: 'Image settings'
mapping:
default_image:
type: field_default_image
label: 'Default value'
field.image.field_settings:
field.field_settings.image:
type: base_file_field_field_settings
label: 'Image settings'
mapping:
......@@ -117,28 +117,9 @@ field.image.field_settings:
type: field_default_image
label: 'Default value'
field.image.value:
type: sequence
field.value.image:
type: field_default_image
label: 'Default value'
sequence:
- type: mapping
label: 'Default image'
mapping:
fid:
type: integer
label: 'File ID'
alt:
type: string
label: 'Alternative text'
title:
type: string
label: 'Title text'
width:
type: integer
label: 'Width'
height:
type: integer
label: 'Height'
field.formatter.settings.image:
type: mapping
......
......@@ -44,8 +44,6 @@ public function transform($value, MigrateExecutable $migrate_executable, Row $ro
* A valid array of settings.
*/
public function getSettings($field_type, $global_settings, $widget_settings) {
$image_label = isset($widget_settings['alt']) ? $widget_settings['alt'] : '';
$title_label = isset($widget_settings['title']) ? $widget_settings['title'] : '';
$max_length = isset($global_settings['max_length']) ? $global_settings['max_length'] : '';
$max_length = empty($max_length) ? 255 : $max_length;