protected StorableConfigBase::castValue($key, $value)
Casts the value to correct data type using the configuration schema.
string $key: A string that maps to a key within the configuration data.
string $value: Value to associate with the key.
mixed The value cast to the type indicated in the schema.
\Drupal\Core\Config\UnsupportedDataTypeConfigException If the value is unsupported in configuration.
protected function castValue($key, $value) { $element = $this->getSchemaWrapper()->get($key); // Do not cast value if it is unknown or defined to be ignored. if ($element && ($element instanceof Undefined || $element instanceof Ignore)) { // Do validate the value (may throw UnsupportedDataTypeConfigException) // to ensure unsupported types are not supported in this case either. $this->validateValue($key, $value); return $value; } if (is_scalar($value) || $value === NULL) { if ($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; } else { $value = $element->getCastedValue(); } } } else { // Throw exception on any non-scalar or non-array value. if (!is_array($value)) { throw new UnsupportedDataTypeConfigException("Invalid data type for config element {$this->getName()}:$key"); } // Recurse into any nested keys. foreach ($value as $nested_value_key => $nested_value) { $value[$nested_value_key] = $this->castValue($key . '.' . $nested_value_key, $nested_value); } } return $value; }
© 2001–2016 by the original authors
Licensed under the GNU General Public License, version 2 and later.
Drupal is a registered trademark of Dries Buytaert.
https://api.drupal.org/api/drupal/core!lib!Drupal!Core!Config!StorableConfigBase.php/function/StorableConfigBase::castValue/8.1.x