Implements Drupal\Core\Entity\EntityStorageControllerInterface::save().
EntityMalformedException When attempting to save a configuration entity that has no ID.
Overrides EntityStorageControllerInterface::save
public function save(EntityInterface $entity) {
$prefix = $this
->getConfigPrefix();
// Configuration entity IDs are strings, and '0' is a valid ID.
$id = $entity
->id();
if ($id === NULL || $id === '') {
throw new EntityMalformedException('The entity does not have an ID.');
}
// Load the stored entity, if any.
// At this point, the original ID can only be NULL or a valid ID.
if ($entity
->getOriginalID() !== NULL) {
$id = $entity
->getOriginalID();
}
$config = $this->configFactory
->get($prefix . $id);
$is_new = $config
->isNew();
if (!$is_new && !isset($entity->original)) {
$this
->resetCache(array(
$id,
));
$result = $this
->load(array(
$id,
));
$entity->original = reset($result);
}
if ($id !== $entity
->id()) {
// Renaming a config object needs to cater for:
// - Storage controller 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, $prefix . $entity
->id());
}
$this
->preSave($entity);
$this
->invokeHook('presave', $entity);
// Retrieve the desired properties and set them in config.
foreach ($entity
->getExportProperties() as $key => $value) {
$config
->set($key, $value);
}
if (!$is_new) {
$return = SAVED_UPDATED;
$config
->save();
$this
->postSave($entity, TRUE);
$this
->invokeHook('update', $entity);
// Immediately update the original ID.
$entity
->setOriginalID($entity
->id());
}
else {
$return = SAVED_NEW;
$config
->save();
$entity
->enforceIsNew(FALSE);
$this
->postSave($entity, FALSE);
$this
->invokeHook('insert', $entity);
}
unset($entity->original);
return $return;
}