public function RoleStorageController::preSave

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

File

drupal/core/modules/user/lib/Drupal/user/RoleStorageController.php, line 21
Contains \Drupal\user\RoleStorageController.

Class

RoleStorageController
Controller class for user roles.

Namespace

Drupal\user

Code

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);
}