$config_name =
$prefix .
$entity->
id();
if ($id !==
$entity->
id()) { // Renaming a config object needs to cater for:
// - Storage needs to access the original object.
// - The object needs to be renamed/copied in ConfigFactory and reloaded.
// - All instances of the object need to be renamed.
$this->configFactory->
rename($prefix .
$id,
$config_name);
} $config =
$this->configFactory->
getEditable($config_name);
// Retrieve the desired properties and set them in config.
$config->
setData($this->
mapToStorageRecord($entity));
$config->
save($entity->
hasTrustedData());
// Update the entity with the values stored in configuration. It is possible
// that configuration schema has casted some of the values.
if (!
$entity->
hasTrustedData()) { $data =
$this->
mapFromStorageRecords([$config->
get()]);
$updated_entity =
current($data);
foreach (array_keys($config->
get()) as $property) { $value =
$updated_entity->
get($property);
$entity->
set($property,
$value);
}