diff -u b/core/modules/user/lib/Drupal/user/RoleStorageController.php b/core/modules/user/lib/Drupal/user/RoleStorageController.php --- b/core/modules/user/lib/Drupal/user/RoleStorageController.php +++ b/core/modules/user/lib/Drupal/user/RoleStorageController.php @@ -15,22 +15,24 @@ */ class RoleStorageController extends ConfigStorageController { - /** - * Overrides ConfigStorageController::save(). - */ - public function save(EntityInterface $entity) { - // Prevent leading and trailing spaces in role names. - $entity->label = trim($entity->label); - - if (!isset($entity->weight)) { - $roles = entity_load_multiple('entity_roles'); + public function create(array $values) { + $entity = parent::create($values); + $roles = entity_load_multiple('user_role'); + if (count($roles)) { // 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; } - + return $entity; + } + /** + * Overrides ConfigStorageController::save(). + */ + public function save(EntityInterface $entity) { + // Prevent leading and trailing spaces in role names. + $entity->label = trim($entity->label); return parent::save($entity); } diff -u b/core/modules/user/user.admin.inc b/core/modules/user/user.admin.inc --- b/core/modules/user/user.admin.inc +++ b/core/modules/user/user.admin.inc @@ -834,9 +834,8 @@ $form['roles'] = array( '#tree' => TRUE, ); - $max_weight = 0; + foreach ($roles as $rid => $role) { - $max_weight = max($max_weight, $role->weight); $form['roles'][$rid]['#role'] = $role; $form['roles'][$rid]['#weight'] = $role->weight; $form['roles'][$rid]['name'] = array( @@ -870,7 +869,6 @@ $add_role = entity_create('user_role', array( 'id' => NULL, 'label' => NULL, - 'weight' => $max_weight + 1, )); $add_form = user_admin_role(array(), $form_state, $add_role); $add_form['actions']['submit']['#submit'] = array('user_admin_role_submit');