function PasswordItem::preSave
Same name in other branches
- 8.9.x core/lib/Drupal/Core/Field/Plugin/Field/FieldType/PasswordItem.php \Drupal\Core\Field\Plugin\Field\FieldType\PasswordItem::preSave()
- 10 core/lib/Drupal/Core/Field/Plugin/Field/FieldType/PasswordItem.php \Drupal\Core\Field\Plugin\Field\FieldType\PasswordItem::preSave()
- 11.x core/lib/Drupal/Core/Field/Plugin/Field/FieldType/PasswordItem.php \Drupal\Core\Field\Plugin\Field\FieldType\PasswordItem::preSave()
Overrides FieldItemBase::preSave
File
-
core/
lib/ Drupal/ Core/ Field/ Plugin/ Field/ FieldType/ PasswordItem.php, line 40
Class
- PasswordItem
- Defines the 'password' entity field type.
Namespace
Drupal\Core\Field\Plugin\Field\FieldTypeCode
public function preSave() {
parent::preSave();
$entity = $this->getEntity();
if ($this->pre_hashed) {
// Reset the pre_hashed value since it has now been used.
$this->pre_hashed = FALSE;
}
elseif (!$entity->isNew() && empty($this->value)) {
// If the password is empty, that means it was not changed, so use the
// original password.
$this->value = $entity->original->{$this->getFieldDefinition()
->getName()}->value;
}
elseif ($entity->isNew() || strlen(trim($this->value)) > 0 && $this->value != $entity->original->{$this->getFieldDefinition()
->getName()}->value) {
// Allow alternate password hashing schemes.
$this->value = \Drupal::service('password')->hash(trim($this->value));
// Abort if the hashing failed and returned FALSE.
if (!$this->value) {
throw new EntityMalformedException('The entity does not have a password.');
}
}
// Ensure that the existing password is unset to minimise risks of it
// getting serialized and stored somewhere.
$this->existing = NULL;
}
Buggy or inaccurate documentation? Please file an issue. Need support? Need help programming? Connect with the Drupal community.