protected function UserStorageController::preSave

Overrides Drupal\Core\Entity\DatabaseStorageController::preSave().

Overrides DatabaseStorageController::preSave

File

drupal/core/modules/user/lib/Drupal/user/UserStorageController.php, line 131
Definition of Drupal\user\UserStorageController.

Class

UserStorageController
Controller class for users.

Namespace

Drupal\user

Code

protected function preSave(EntityInterface $entity) {

  // Update the user password if it has changed.
  if ($entity
    ->isNew() || $entity->pass->value && $entity->pass->value != $entity->original->pass->value) {

    // Allow alternate password hashing schemes.
    $entity->pass->value = $this->password
      ->hash(trim($entity->pass->value));

    // Abort if the hashing failed and returned FALSE.
    if (!$entity->pass->value) {
      throw new EntityMalformedException('The entity does not have a password.');
    }
  }
  if (!$entity
    ->isNew()) {

    // If the password is empty, that means it was not changed, so use the
    // original password.
    if (empty($entity->pass->value)) {
      $entity->pass->value = $entity->original->pass->value;
    }
  }

  // Store account cancellation information.
  foreach (array(
    'user_cancel_method',
    'user_cancel_notify',
  ) as $key) {
    if (isset($entity->{$key})) {
      $this->userData
        ->set('user', $entity
        ->id(), substr($key, 5), $entity->{$key});
    }
  }
}