You are here

function user_revision_user_presave in User Revision 8

Same name and namespace in other branches
  1. 7.2 user_revision.module \user_revision_user_presave()
  2. 7 user_revision.module \user_revision_user_presave()

Implements hook_ENTITY_TYPE_presave() for user entity.

File

./user_revision.module, line 87
User Revision module.

Code

function user_revision_user_presave(UserInterface $user) {
  if (!count($user
    ->get('revision_uid')
    ->getValue())) {
    $user
      ->set('revision_uid', \Drupal::currentUser()
      ->id());
  }
  if (isset($user->original) && !$user
    ->isNewRevision()) {

    // If we are updating an existing user without adding a new revision, we
    // need to make sure $entity->revision_log is reset whenever it is empty.
    // Therefore, this code allows us to avoid clobbering an existing log
    // entry with an empty one.
    $user->revision_log = $user->original->revision_log->value;
  }

  // Check revision timestamp
  if (!$user
    ->get('revision_timestamp')
    ->getValue()) {
    $user
      ->set('revision_timestamp', time());
  }
}