You are here

function track_field_changes_entity_update in Track Field Changes 8

Implements hook_entity_update().

File

./track_field_changes.module, line 34

Code

function track_field_changes_entity_update(\Drupal\Core\Entity\EntityInterface $entity) {
  $field_change_settings = \Drupal::config('track_field_changes.settings')
    ->get($entity
    ->getEntityTypeId() . '.' . $entity
    ->bundle());

  // If entity is audited.
  if (!empty($field_change_settings)) {

    // Get log.
    $log = isset($entity->track_field_changes) ? $entity->track_field_changes : '';
    if (!empty($field_change_settings['basic_revision']) && !empty($log)) {
      track_field_changes_insert_db($entity
        ->getEntityTypeId(), $entity
        ->id(), 'log', '', '', 'br', $log);
    }
    if (!empty($field_change_settings['fields_audit']) && !empty($field_change_settings['fields'])) {

      /** @var \Drupal\Core\Entity\EntityFieldManager $entity_field_manager */
      $entity_field_manager = \Drupal::service('entity_field.manager');
      $selected_fields = $field_change_settings['fields'];
      $original = isset($entity->original) ? $entity->original : \Drupal::entityTypeManager()
        ->getStorage($entity
        ->getEntityTypeId())
        ->loadUnchanged($entity
        ->id());
      foreach ($selected_fields as $field_name) {
        $field_info = $entity_field_manager
          ->getFieldDefinitions($entity
          ->getEntityTypeId(), $entity
          ->bundle())[$field_name];
        $field_type = $field_info
          ->getType();
        $new_field = $entity
          ->get($field_name)
          ->getValue();
        $original_field = $original
          ->get($field_name)
          ->getValue();
        if (!track_field_changes_compare_field($field_type, $original_field, $new_field)) {
          $field_before = $original_field;
          $field_after = $new_field;
          if ($field_type == 'entity_reference') {
            $settings = $field_info
              ->getSettings();
            $entity_type = $settings['target_type'];
            $field_before['target_type'] = $entity_type;
            $field_after['target_type'] = $entity_type;
          }
          if ($field_type == 'file') {
            for ($i = 0; $i < count($field_after); $i++) {

              /** @var \Drupal\file\Entity\File $file */
              $file = \Drupal::entityTypeManager()
                ->getStorage('file')
                ->load($field_after['fid']);
              $field_after['filename'] = $file
                ->getFilename();
              $field_after['uri'] = $file
                ->getFileUri();
              $field_after['filemime'] = $file
                ->getMimeType();
              $field_after['filesize'] = $file
                ->getSize();
              $field_after['status'] = $file->status;
              $field_after['timestamp'] = $file
                ->getCreatedTime();
            }
          }
          track_field_changes_insert_db($entity
            ->getEntityTypeId(), $entity
            ->id(), $field_name, json_encode($field_before), json_encode($field_after), 'fr', $log);
        }
      }
    }
  }
}