You are here

function field_test_entity_save in SimpleTest 7

1 call to field_test_entity_save()
field_test_entity_form_submit in tests/field_test.module
Submit handler for field_test_set_field_values().

File

tests/field_test.module, line 216

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) {
    drupal_write_record('test_entity', $entity);
    drupal_write_record('test_entity_revision', $entity);
    $op = 'insert';
  }
  else {
    drupal_write_record('test_entity', $entity, 'ftid');
    if (!empty($entity->revision)) {
      drupal_write_record('test_entity_revision', $entity);
    }
    else {
      drupal_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);
}