You are here

protected function ConfigEntityStorage::doSave in Drupal 9

Same name and namespace in other branches
  1. 8 core/lib/Drupal/Core/Config/Entity/ConfigEntityStorage.php \Drupal\Core\Config\Entity\ConfigEntityStorage::doSave()
  2. 10 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 258

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;
}