diff --git a/core/lib/Drupal/Core/Config/StorableConfigBase.php b/core/lib/Drupal/Core/Config/StorableConfigBase.php index 18c0f07..a72ddf6 100644 --- a/core/lib/Drupal/Core/Config/StorableConfigBase.php +++ b/core/lib/Drupal/Core/Config/StorableConfigBase.php @@ -12,6 +12,7 @@ use Drupal\Core\TypedData\PrimitiveInterface; use Drupal\Core\TypedData\Type\FloatInterface; use Drupal\Core\TypedData\Type\IntegerInterface; +use Drupal\Core\Config\Schema\Property; /** * Provides a base class for configuration objects with storage support. @@ -167,36 +168,31 @@ protected function validateValue($key, $value) { * Exception on unsupported/undefined data type deducted. */ protected function castValue($key, $value) { - if ($value === NULL) { - $value = NULL; + $element = FALSE; + try { + $element = $this->getSchemaWrapper()->get($key); } - elseif (is_scalar($value)) { - try { - $element = $this->getSchemaWrapper()->get($key); - if ($element instanceof PrimitiveInterface) { - // Special handling for integers and floats since the configuration - // system is primarily concerned with saving values from the Form API - // we have to special case the meaning of an empty string for numeric - // types. In PHP this would be casted to a 0 but for the purposes of - // configuration we need to treat this as a NULL. - if ($value === '' && ($element instanceof IntegerInterface || $element instanceof FloatInterface)) { - $value = NULL; - } - else { - $value = $element->getCastedValue(); - } - } - else { - // Config only supports primitive data types. If the config schema - // does define a type $element will be an instance of - // \Drupal\Core\Config\Schema\Property. Convert it to string since it - // is the safest possible type. - $value = $element->getString(); - } + catch (SchemaIncompleteException $e) { + // @todo throw an exception due to an incomplete schema. + // Fix as part of https://drupal.org/node/2183983. + } + // Do not cast value of a property, as it is unknown or mixed. + if ($element && $element instanceof Property) { + return $value; + } + if ((is_scalar($value) || $value === NULL) && ($element && $element instanceof PrimitiveInterface)) { + // Special handling for integers and floats since the configuration + // system is primarily concerned with saving values from the Form API + // we have to special case the meaning of an empty string for numeric + // types. In PHP this would be casted to a 0 but for the purposes of + // configuration we need to treat this as a NULL. + $empty_value = $value === '' && ($element instanceof IntegerInterface || $element instanceof FloatInterface); + + if ($value === NULL || $empty_value) { + $value = NULL; } - catch (SchemaIncompleteException $e) { - // @todo throw an exception due to an incomplete schema. - // Fix as part of https://drupal.org/node/2183983. + else { + $value = $element->getCastedValue(); } } else { diff --git a/core/modules/config/lib/Drupal/config/Tests/ConfigSchemaTestBase.php b/core/modules/config/lib/Drupal/config/Tests/ConfigSchemaTestBase.php index 61eee7e..ce9aa63 100644 --- a/core/modules/config/lib/Drupal/config/Tests/ConfigSchemaTestBase.php +++ b/core/modules/config/lib/Drupal/config/Tests/ConfigSchemaTestBase.php @@ -92,40 +92,39 @@ public function assertConfigSchema(TypedConfigManagerInterface $typed_config, $c * Returns mixed value. */ protected function checkValue($key, $value) { + $element = FALSE; + try { + $element = $this->schema->get($key); + } + catch (SchemaIncompleteException $e) { + if (is_scalar($value) || $value === NULL) { + $this->fail("{$this->configName}:$key has no schema."); + } + } + // Do not check value of property, as it is unknown or mixed. + if ($element && $element instanceof Property) { + return $value; + } + if (is_scalar($value) || $value === NULL) { - try { - $success = FALSE; - $type = gettype($value); - $element = $this->schema->get($key); - if ($element instanceof PrimitiveInterface) { - if ($type == 'integer' && $element instanceof IntegerInterface) { - $success = TRUE; - } - if ($type == 'double' && $element instanceof FloatInterface) { - $success = TRUE; - } - if ($type == 'boolean' && $element instanceof BooleanInterface) { - $success = TRUE; - } - if ($type == 'string' && ($element instanceof StringInterface || $element instanceof Property)) { - $success = TRUE; - } + $success = FALSE; + $type = gettype($value); + if ($element instanceof PrimitiveInterface) { + $success = + ($type == 'integer' && $element instanceof IntegerInterface) || + ($type == 'double' && $element instanceof FloatInterface) || + ($type == 'boolean' && $element instanceof BooleanInterface) || + ($type == 'string' && $element instanceof StringInterface) || // Null values are allowed for all types. - if ($value === NULL) { - $success = TRUE; - } - } - else { - // @todo throw an exception due to an incomplete schema. Only possible - // once https://drupal.org/node/1910624 is complete. - } - $class = get_class($element); - if (!$success) { - $this->fail("{$this->configName}:$key has the wrong schema. Variable type is $type and schema class is $class."); - } + ($value === NULL); } - catch (SchemaIncompleteException $e) { - $this->fail("{$this->configName}:$key has no schema."); + else { + // @todo throw an exception due to an incomplete schema. Only possible + // once https://drupal.org/node/1910624 is complete. + } + $class = get_class($element); + if (!$success) { + $this->fail("{$this->configName}:$key has the wrong schema. Variable type is $type and schema class is $class."); } } else {