function ConfigEntityStorage::doSave

Same name and namespace in other branches
  1. 9 core/lib/Drupal/Core/Config/Entity/ConfigEntityStorage.php \Drupal\Core\Config\Entity\ConfigEntityStorage::doSave()
  2. 8.9.x core/lib/Drupal/Core/Config/Entity/ConfigEntityStorage.php \Drupal\Core\Config\Entity\ConfigEntityStorage::doSave()
  3. 11.x core/lib/Drupal/Core/Config/Entity/ConfigEntityStorage.php \Drupal\Core\Config\Entity\ConfigEntityStorage::doSave()

Performs storage-specific saving of the entity.

Parameters

int|string $id: The original entity ID.

\Drupal\Core\Entity\EntityInterface $entity: The entity to save.

Return value

bool|int If the record insert or update failed, returns FALSE. If it succeeded, returns SAVED_NEW or SAVED_UPDATED, depending on the operation performed.

Overrides EntityStorageBase::doSave

File

core/lib/Drupal/Core/Config/Entity/ConfigEntityStorage.php, line 263

Class

ConfigEntityStorage
Defines the storage class for configuration entities.

Namespace

Drupal\Core\Config\Entity

Code

protected function doSave($id, EntityInterface $entity) {
  $is_new = $entity->isNew();
  $prefix = $this->getPrefix();
  $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);
    }
  }
  return $is_new ? SAVED_NEW : SAVED_UPDATED;
}

Buggy or inaccurate documentation? Please file an issue. Need support? Need help programming? Connect with the Drupal community.