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->entityInfo['config_prefix'] . '.';
// 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 = config($prefix . $id);
$config
->setName($prefix . $entity
->id());
if (!$config
->isNew() && !isset($entity->original)) {
$this
->resetCache(array(
$id,
));
$result = $this
->load(array(
$id,
));
$entity->original = reset($result);
}
$this
->preSave($entity);
$this
->invokeHook('presave', $entity);
// Retrieve the desired properties and set them in config.
foreach ($this
->getProperties($entity) as $key => $value) {
$config
->set($key, $value);
}
if (!$config
->isNew()) {
$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);
}
// Add this entity to the manifest file if necessary.
$config = config('manifest.' . $this->entityInfo['config_prefix']);
$manifest = $config
->get();
if (!in_array($this->entityInfo['config_prefix'] . '.' . $entity
->id(), $manifest)) {
$manifest[$entity
->id()] = array(
'name' => $this->entityInfo['config_prefix'] . '.' . $entity
->id(),
);
$config
->setData($manifest)
->save();
}
unset($entity->original);
return $return;
}