Acts on an entity before the presave hook is invoked.
Used before the entity is saved and before invoking the presave hook.
Overrides ConfigStorageController::preSave
public function preSave(EntityInterface $entity) {
if (!isset($entity->weight) && ($roles = entity_load_multiple('user_role'))) {
// Set a role weight to make this new role last.
$max = array_reduce($roles, function ($max, $entity) {
return $max > $entity->weight ? $max : $entity->weight;
});
$entity->weight = $max + 1;
}
parent::preSave($entity);
}