Commit 4b78e91b authored by catch's avatar catch

Issue #2562155 by stefan.r, joelpittet, alexpott: Cast TranslationWrapper...

Issue #2562155 by stefan.r, joelpittet, alexpott: Cast TranslationWrapper objects in config to string during Config::set()/setData()
parent 1bd7a291
......@@ -108,8 +108,8 @@ public function get($key = '') {
/**
* {@inheritdoc}
*/
public function setData(array $data, $validate_keys = TRUE) {
parent::setData($data, $validate_keys);
public function setData(array $data) {
parent::setData($data);
$this->resetOverriddenData();
return $this;
}
......
......@@ -8,6 +8,7 @@
namespace Drupal\Core\Config;
use Drupal\Component\Utility\NestedArray;
use Drupal\Component\Utility\SafeStringInterface;
use Drupal\Core\Cache\Cache;
use Drupal\Core\Cache\RefinableCacheableDependencyInterface;
use Drupal\Core\Cache\RefinableCacheableDependencyTrait;
......@@ -153,10 +154,6 @@ public function get($key = '') {
*
* @param array $data
* The new configuration data.
* @param bool $validate_keys
* (optional) Whether the data should be verified for valid keys. Set to
* FALSE if the $data is known to be valid already (for example, being
* loaded from the config storage).
*
* @return $this
* The configuration object.
......@@ -164,10 +161,9 @@ public function get($key = '') {
* @throws \Drupal\Core\Config\ConfigValueException
* If any key in $data in any depth contains a dot.
*/
public function setData(array $data, $validate_keys = TRUE) {
if ($validate_keys) {
$this->validateKeys($data);
}
public function setData(array $data) {
$data = $this->castSafeStrings($data);
$this->validateKeys($data);
$this->data = $data;
return $this;
}
......@@ -187,6 +183,7 @@ public function setData(array $data, $validate_keys = TRUE) {
* If $value is an array and any of its keys in any depth contains a dot.
*/
public function set($key, $value) {
$value = $this->castSafeStrings($value);
// The dot/period is a reserved character; it may appear between keys, but
// not within keys.
if (is_array($value)) {
......@@ -280,4 +277,27 @@ public function getCacheMaxAge() {
return $this->cacheMaxAge;
}
/**
* Casts any objects that implement SafeStringInterface to string.
*
* @param mixed $data
* The configuration data.
*
* @return mixed
* The data with any safe strings cast to string.
*/
protected function castSafeStrings($data) {
if ($data instanceof SafeStringInterface) {
$data = (string) $data;
}
else if (is_array($data)) {
array_walk_recursive($data, function (&$value) {
if ($value instanceof SafeStringInterface) {
$value = (string) $value;
}
});
}
return $data;
}
}
......@@ -98,7 +98,7 @@ abstract class StorableConfigBase extends ConfigBase {
*/
public function initWithData(array $data) {
$this->isNew = FALSE;
$this->setData($data, FALSE);
$this->data = $data;
$this->originalData = $this->data;
return $this;
}
......
......@@ -12,6 +12,7 @@
use Drupal\Core\Field\FieldStorageDefinitionInterface;
use Drupal\Core\Form\FormStateInterface;
use Drupal\Core\Session\AccountInterface;
use Drupal\Core\StringTranslation\TranslationWrapper;
use Drupal\Core\TypedData\OptionsProviderInterface;
use Drupal\Core\TypedData\DataDefinition;
......@@ -33,8 +34,8 @@ class BooleanItem extends FieldItemBase implements OptionsProviderInterface {
*/
public static function defaultFieldSettings() {
return array(
'on_label' => t('On'),
'off_label' => t('Off'),
'on_label' => new TranslationWrapper('On'),
'off_label' => new TranslationWrapper('Off'),
) + parent::defaultFieldSettings();
}
......
......@@ -8,6 +8,7 @@
namespace Drupal\Tests\Core\Config;
use Drupal\Core\DependencyInjection\ContainerBuilder;
use Drupal\Core\Render\SafeString;
use Drupal\Tests\UnitTestCase;
use Drupal\Core\Config\Config;
use Drupal\Component\Utility\SafeMarkup;
......@@ -528,4 +529,26 @@ public function assertOriginalConfigDataEquals($data, $apply_overrides) {
}
}
/**
* @covers ::setData
* @covers ::set
* @covers ::initWithData
*/
public function testSafeStringHandling() {
// Safe strings are cast when using ::set().
$safe_string = SafeString::create('bar');
$this->config->set('foo', $safe_string);
$this->assertSame('bar', $this->config->get('foo'));
$this->config->set('foo', ['bar' => $safe_string]);
$this->assertSame('bar', $this->config->get('foo.bar'));
// Safe strings are cast when using ::setData().
$this->config->setData(['bar' => $safe_string]);
$this->assertSame('bar', $this->config->get('bar'));
// Safe strings are not cast when using ::initWithData().
$this->config->initWithData(['bar' => $safe_string]);
$this->assertSame($safe_string, $this->config->get('bar'));
}
}
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