function _serial_generate_value in Serial Field 7
Same name and namespace in other branches
- 6 serial.inc \_serial_generate_value()
Generates a unique serial value (unique per entity bundle).
Parameters
string $entity_type: Type of entity (e.g. node)
string $bundle: Containing bundle (e.g. content type).
string $field_name: The field name.
bool $delete: Indicates if temporary records should be deleted.
Return value
int the unique serial value number.
Throws
\Exception
2 calls to _serial_generate_value()
- serial_field_presave in ./serial.module 
- Implements hook_field_presave().
- _serial_init_old_entities in ./serial.inc 
- Initializes the value of a new serial field in existing entities.
File
- ./serial.inc, line 154 
- Internal functions for the Serial module.
Code
function _serial_generate_value($entity_type, $bundle, $field_name, $delete = TRUE) {
  $transaction = db_transaction();
  try {
    // Get the name of the relevant table.
    $table = _serial_get_table_name($entity_type, $bundle, $field_name);
    // Insert a temporary record to get a new unique serial value.
    $uniqid = uniqid('', TRUE);
    $sid = db_insert($table)
      ->fields(array(
      'uniqid' => $uniqid,
    ))
      ->execute();
    // If there's a reason why it's come back undefined, reset it.
    $sid = isset($sid) ? $sid : 0;
    // Delete the temporary record.
    if ($delete && $sid && $sid % 10 == 0) {
      db_delete($table)
        ->condition('sid', $sid, '<')
        ->execute();
    }
    // Return the new unique serial value.
    return $sid;
  } catch (Exception $e) {
    $transaction
      ->rollback();
    watchdog_exception('serial', $e);
    throw $e;
  }
}