You are here

protected function CiviEntityStorage::doSave in CiviCRM Entity 8.3

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 ContentEntityStorageBase::doSave

File

src/CiviEntityStorage.php, line 132

Class

CiviEntityStorage
Defines entity class for external CiviCRM entities.

Namespace

Drupal\civicrm_entity

Code

protected function doSave($id, EntityInterface $entity) {

  /** @var \Drupal\civicrm_entity\Entity\CivicrmEntity $entity */
  $return = $entity
    ->isNew() ? SAVED_NEW : SAVED_UPDATED;
  $params = $entity
    ->civicrmApiNormalize();
  $non_base_fields = array_filter($entity
    ->getFieldDefinitions(), function (FieldDefinitionInterface $definition) {
    return !$definition
      ->getFieldStorageDefinition()
      ->isBaseField();
  });
  $non_base_fields = array_map(function (FieldDefinitionInterface $definition) {
    return $definition
      ->getName();
  }, $non_base_fields);
  $result = $this
    ->getCiviCrmApi()
    ->save($this->entityType
    ->get('civicrm_entity'), $params);
  if ($entity
    ->isNew()) {
    $entity->{$this->idKey} = (string) $result['id'];
  }
  $this
    ->doSaveFieldItems($entity, $non_base_fields);
  return $return;
}