1.20.x field_test.entity.inc field_test_entity_save(&$entity)

Saves a test_entity.

A new entity is created if $entity->ftid and $entity->is_new are both empty. A new revision is created if $entity->revision is not empty.

Parameters

$entity: The entity to save.

File

modules/field/tests/field_test/field_test.entity.inc, line 282
Defines an entity type.

Code

function field_test_entity_save(&$entity) {
  field_attach_presave('test_entity', $entity);

  if (!isset($entity->is_new)) {
    $entity->is_new = empty($entity->ftid);
  }

  if (!$entity->is_new && !empty($entity->revision)) {
    $entity->old_ftvid = $entity->ftvid;
    unset($entity->ftvid);
  }

  $update_entity = TRUE;
  if ($entity->is_new) {
    backdrop_write_record('test_entity', $entity);
    backdrop_write_record('test_entity_revision', $entity);
    $op = 'insert';
  }
  else {
    backdrop_write_record('test_entity', $entity, 'ftid');
    if (!empty($entity->revision)) {
      backdrop_write_record('test_entity_revision', $entity);
    }
    else {
      backdrop_write_record('test_entity_revision', $entity, 'ftvid');
      $update_entity = FALSE;
    }
    $op = 'update';
  }
  if ($update_entity) {
    db_update('test_entity')
      ->fields(array('ftvid' => $entity->ftvid))
      ->condition('ftid', $entity->ftid)
      ->execute();
  }

  // Save fields.
  $function = "field_attach_$op";
  $function('test_entity', $entity);
}