You are here

function track_field_changes_insert_db in Track Field Changes 8

Same name and namespace in other branches
  1. 7 track_field_changes.module \track_field_changes_insert_db()
2 calls to track_field_changes_insert_db()
track_field_changes_entity_insert in ./track_field_changes.module
Implements hook_entity_insert().
track_field_changes_entity_update in ./track_field_changes.module
Implements hook_entity_update().

File

./track_field_changes.module, line 330

Code

function track_field_changes_insert_db($entity_type_id, $entity_id, $field_name, $old_field, $new_field, $type, $log) {
  $user = \Drupal::currentUser();

  // If this is true, we're only storing one entry per entity.
  $track_field_changes_disable_multiple = \Drupal::config('track_field_changes.settings')
    ->get($entity_type_id . '.' . $entity_id . '.disable_multiple');
  if ($track_field_changes_disable_multiple) {
    $result = \Drupal::database()
      ->select('track_field_changes_audit', 't')
      ->fields('t')
      ->condition('entity_type', $entity_type_id, '=')
      ->condition('entity_id', $entity_id, '=')
      ->execute()
      ->fetchAssoc();
    if ($result['entity_id'] == $entity_id) {
      \Drupal::database()
        ->update('track_field_changes_audit')
        ->fields([
        'entity_type' => $entity_type_id,
        'entity_id' => $entity_id,
        'timestamp' => \Drupal::time()
          ->getRequestTime(),
        'uid' => $user
          ->id(),
        'field_name' => $field_name,
        'before_value_text' => $old_field,
        'after_value_text' => $new_field,
        'type' => $type,
        'log' => $log,
      ])
        ->condition('entity_type', $entity_type_id, '=')
        ->condition('entity_id', $entity_id, '=')
        ->execute();
    }
    else {
      \Drupal::database()
        ->insert('track_field_changes_audit')
        ->fields([
        'entity_type' => $entity_type_id,
        'entity_id' => $entity_id,
        'timestamp' => \Drupal::time()
          ->getRequestTime(),
        'uid' => $user
          ->id(),
        'field_name' => $field_name,
        'before_value_text' => $old_field,
        'after_value_text' => $new_field,
        'type' => $type,
        'log' => $log,
      ])
        ->execute();
    }
  }
  else {
    \Drupal::database()
      ->insert('track_field_changes_audit')
      ->fields([
      'entity_type' => $entity_type_id,
      'entity_id' => $entity_id,
      'timestamp' => \Drupal::time()
        ->getRequestTime(),
      'uid' => $user
        ->id(),
      'field_name' => $field_name,
      'before_value_text' => $old_field,
      'after_value_text' => $new_field,
      'type' => $type,
      'log' => $log,
    ])
      ->execute();
  }
}