diff --git a/core/config/schema/core.data_types.schema.yml b/core/config/schema/core.data_types.schema.yml index 0e589e1..ef68a3e 100644 --- a/core/config/schema/core.data_types.schema.yml +++ b/core/config/schema/core.data_types.schema.yml @@ -414,14 +414,19 @@ core.date_format.*: type: string label: 'Default language' -# Schema for the String field type. +# Schema for the configuration of the String field type. + +field.string.storage_settings: + type: mapping + label: 'String settings' + mapping: + max_length: + type: integer + label: 'Maximum length' field.string.field_settings: type: sequence label: 'String settings' - sequence: - - type: string - label: 'Setting' field.string.value: type: sequence @@ -434,7 +439,127 @@ field.string.value: type: string label: 'Value' -# Schema for the configuration files of the Boolean field type. +# Schema for the configuration of the String (long) field type. + +field.string_long.storage_settings: + type: field.string.storage_settings + label: 'String (long) settings' + +field.string_long.field_settings: + type: field.string.field_settings + label: 'String (long) settings' + +field.string_long.value: + type: sequence + label: 'Default value' + sequence: + - type: mapping + label: 'Default' + mapping: + value: + type: text + label: 'Value' + +# Schema for the configuration of the URI field type. + +field.uri.storage_settings: + type: mapping + label: 'URI settings' + mapping: + max_length: + type: integer + label: 'Maximum length' + +field.uri.field_settings: + type: sequence + label: 'URI settings' + +field.uri.value: + type: sequence + label: 'Default value' + sequence: + - type: mapping + label: 'Default' + mapping: + value: + type: string + label: 'Value' + +# Schema for the configuration of the Created field type. + +field.created.storage_settings: + type: sequence + label: 'Created timestamp settings' + +field.created.field_settings: + type: sequence + label: 'Created timestamp settings' + +field.created.value: + type: sequence + label: 'Default value' + sequence: + - type: mapping + label: 'Default' + mapping: + value: + type: integer + label: 'Value' + +# Schema for the configuration of the Changed field type. + +field.changed.storage_settings: + type: sequence + label: 'Changed timestamp settings' + +field.changed.field_settings: + type: sequence + label: 'Changed timestamp settings' + +field.changed.value: + type: sequence + label: 'Default value' + sequence: + - type: mapping + label: 'Default' + mapping: + value: + type: integer + label: 'Value' + +# Schema for the configuration of the Entity reference field type. + +field.entity_reference.storage_settings: + type: mapping + label: 'Entity reference settings' + mapping: + target_type: + type: string + label: 'Type of item to reference' + +field.entity_reference.field_settings: + type: mapping + label: 'Entity reference settings' + mapping: + handler: + type: string + label: 'Reference method' + handler_settings: + type: entity_reference.[%parent.handler].handler_settings + label: 'Reference method settings' + +field.entity_reference.value: + type: sequence + label: 'Default value' + sequence: + - type: mapping + label: 'Default' + mapping: + target_id: + type: integer + label: 'Value' + +# Schema for the configuration of the Boolean field type. field.boolean.storage_settings: type: mapping @@ -449,8 +574,7 @@ field.boolean.storage_settings: field.boolean.field_settings: label: 'Boolean settings' - type: mapping - mapping: { } + type: sequence field.boolean.value: type: sequence @@ -463,7 +587,7 @@ field.boolean.value: type: integer label: 'Value' -# Schema for the configuration files of the Email field type. +# Schema for the configuration of the Email field type. field.email.storage_settings: type: sequence @@ -489,7 +613,7 @@ field.email.value: type: email label: 'Value' -# Schema for configuration files of a numeric field types. +# Schema for the configuration of the Integer field type. field.integer.storage_settings: type: sequence @@ -526,6 +650,8 @@ field.integer.value: type: integer label: 'Value' +# Schema for the configuration of the Decimal field type. + field.decimal.storage_settings: type: mapping label: 'Decimal settings' @@ -565,6 +691,8 @@ field.decimal.value: type: float label: 'Value' +# Schema for the configuration of the Float field type. + field.float.storage_settings: type: sequence label: 'Float settings' diff --git a/core/lib/Drupal/Core/Field/BaseFieldDefinition.php b/core/lib/Drupal/Core/Field/BaseFieldDefinition.php index 9b04e9c..b127a229 100644 --- a/core/lib/Drupal/Core/Field/BaseFieldDefinition.php +++ b/core/lib/Drupal/Core/Field/BaseFieldDefinition.php @@ -443,6 +443,12 @@ public function setDefaultValueCallback($callback) { * @return $this */ public function setDefaultValue($value) { + if (!is_array($value)) { + $value = array(array($this->getMainPropertyName() => $value)); + } + elseif (!is_numeric(array_keys($value)[0])) { + $value = array(0 => $value); + } $this->definition['default_value'] = $value; return $this; } diff --git a/core/modules/content_translation/src/Tests/ContentTranslationSettingsTest.php b/core/modules/content_translation/src/Tests/ContentTranslationSettingsTest.php index 98ea255..ea28195 100644 --- a/core/modules/content_translation/src/Tests/ContentTranslationSettingsTest.php +++ b/core/modules/content_translation/src/Tests/ContentTranslationSettingsTest.php @@ -8,6 +8,7 @@ namespace Drupal\content_translation\Tests; use Drupal\comment\Plugin\Field\FieldType\CommentItemInterface; +use Drupal\config\Tests\SchemaCheckTestTrait; use Drupal\Core\Field\Entity\BaseFieldOverride; use Drupal\Core\Language\Language; use Drupal\field\Entity\FieldConfig; @@ -20,6 +21,8 @@ */ class ContentTranslationSettingsTest extends WebTestBase { + use SchemaCheckTestTrait; + /** * Modules to enable. * @@ -178,6 +181,8 @@ function testSettingsUI() { $this->assertEqual($definitions['body']->isTranslatable(), $translatable, 'Field translatability correctly switched.'); $this->assertEqual($field->isTranslatable(), $definitions['body']->isTranslatable(), 'Configurable field translatability correctly switched.'); } + + $this->assertConfigSchemas(); } /** @@ -269,4 +274,22 @@ protected function entityManager() { return $this->container->get('entity.manager'); } + /** + * Asserts all active configuration matches schemas. + */ + protected function assertConfigSchemas() { + $names = $this->container->get('config.storage')->listAll(); + $factory = $this->container->get('config.factory'); + /** @var \Drupal\Core\Config\TypedConfigManagerInterface $typed_config */ + $typed_config = $this->container->get('config.typed'); + foreach ($names as $name) { + // It is not possible to provide schema due to https://www.drupal.org/node/2248709 + // @todo Refactor settings in https://www.drupal.org/node/2363155 + if ($name != 'content_translation.settings') { + $config = $factory->get($name); + $this->assertConfigSchema($typed_config, $name, $config->get()); + } + } + } + } diff --git a/core/modules/entity_reference/config/schema/entity_reference.schema.yml b/core/modules/entity_reference/config/schema/entity_reference.schema.yml index de96443..5404963 100644 --- a/core/modules/entity_reference/config/schema/entity_reference.schema.yml +++ b/core/modules/entity_reference/config/schema/entity_reference.schema.yml @@ -1,35 +1,5 @@ # Schema for the configuration files of the Entity Reference module. -field.entity_reference.storage_settings: - type: mapping - label: 'Entity reference settings' - mapping: - target_type: - type: string - label: 'Type of item to reference' - -field.entity_reference.field_settings: - type: mapping - label: 'Entity reference settings' - mapping: - handler: - type: string - label: 'Reference method' - handler_settings: - type: entity_reference.[%parent.handler].handler_settings - label: 'Reference method settings' - -field.entity_reference.value: - type: sequence - label: 'Default value' - sequence: - - type: mapping - label: 'Default' - mapping: - target_id: - type: integer - label: 'Value' - entity_reference.default.handler_settings: type: mapping label: 'View handler settings' diff --git a/core/modules/system/tests/modules/entity_test/src/Entity/EntityTest.php b/core/modules/system/tests/modules/entity_test/src/Entity/EntityTest.php index b4cb26e..ace4815 100644 --- a/core/modules/system/tests/modules/entity_test/src/Entity/EntityTest.php +++ b/core/modules/system/tests/modules/entity_test/src/Entity/EntityTest.php @@ -107,7 +107,7 @@ public static function baseFieldDefinitions(EntityTypeInterface $entity_type) { ->setSetting('handler', 'default') // Default EntityTest entities to have the root user as the owner, to // simplify testing. - ->setDefaultValue(array(0 => 1)) + ->setDefaultValue(1) ->setTranslatable(TRUE) ->setDisplayOptions('form', array( 'type' => 'entity_reference_autocomplete', diff --git a/core/tests/Drupal/Tests/Core/Entity/BaseFieldDefinitionTest.php b/core/tests/Drupal/Tests/Core/Entity/BaseFieldDefinitionTest.php index bc88b21..ae71170 100644 --- a/core/tests/Drupal/Tests/Core/Entity/BaseFieldDefinitionTest.php +++ b/core/tests/Drupal/Tests/Core/Entity/BaseFieldDefinitionTest.php @@ -164,6 +164,7 @@ public function testFieldDefaultValue() { $default_value = array( 'value' => $this->randomMachineName(), ); + $expected_default_value = array($default_value); $definition->setDefaultValue($default_value); $entity = $this->getMockBuilder('Drupal\Core\Entity\ContentEntityBase') ->disableOriginalConstructor() @@ -171,7 +172,23 @@ public function testFieldDefaultValue() { // Set the field item list class to be used to avoid requiring the typed // data manager to retrieve it. $definition->setClass('Drupal\Core\Field\FieldItemList'); - $this->assertEquals($default_value, $definition->getDefaultValue($entity)); + $this->assertEquals($expected_default_value, $definition->getDefaultValue($entity)); + + $data_definition = $this->getMockBuilder('Drupal\Core\TypedData\DataDefinition') + ->disableOriginalConstructor() + ->getMock(); + $data_definition->expects($this->any()) + ->method('getClass') + ->will($this->returnValue('Drupal\Core\Field\FieldItemBase')); + $definition->setItemDefinition($data_definition); + + // Set default value only with a literal. + $definition->setDefaultValue($default_value['value']); + $this->assertEquals($expected_default_value, $definition->getDefaultValue($entity)); + + // Set default value with an indexed array. + $definition->setDefaultValue($expected_default_value); + $this->assertEquals($expected_default_value, $definition->getDefaultValue($entity)); } /**