Problem/Motivation
Steps to reproduce:
- Navigate to admin/people/permissions/roles
- Show row weights
- Assuming a default Drupal install, with roles anonymous user, authenticated user, administrator, with weights 0, 1, 2, respectively:
- Add a role of manager (its weight will default to 3)
- Set manager's weight to 10. Save.
- The weight field will still show as 3.
Technically, manager's weight was updated to 10 in the role table, but user_admin_roles() doesn't bother to check what the weight was in the table (because it gets its info from user_roles(), which doesn't disclose weight), which seems to be by design as the foreach loop uses a zero-based counter.
I'm using a Features-based workflow where I sometimes provide a role that holds various permissions for the content types, taxonomies, etc. defined by that feature. Since features get turned on on some sites but not others, and the sites may have roles besides ones defined by features, this means that every time I visit the role admin page, those weights get changed and the feature is overridden. This is inconsistent with how weights work elsewhere in Drupal, where gaps are permitted.
Proposed resolution
Changing user_roles()' signature is probably out of the question. Right now I have this in an alter hook:
$role_weights = db_query('SELECT r.rid, r.weight FROM {role} r')->fetchAllKeyed();
foreach ($role_weights as $rid => $weight) {
if (isset($form['roles'][$rid])) {
$form['roles'][$rid]['#role']->weight = $weight;
$form['roles'][$rid]['#weight'] = $weight;
$form['roles'][$rid]['weight']['#default_value'] = $weight;
}
}
Doing a second query in user_admin_roles() to get the weight would likely have a negligible performance impact, which shouldn't matter for a page that only admins need to access occasionally.
User interface changes
This should be an invisible change for users who use the drag and drop handles. For users of numeric row weights, this will make the role admin weight behavior consistent with weights elsewhere in Drupal (e.g., field UI, menu, taxonomy).
Comments
Comment #2
ChaseOnTheWebIt appears that Drupal 8 does not have this issue, as roles were rewritten to use the entity system and \Drupal\user\RoleForm was completely overhauled.
Comment #3
ChaseOnTheWebPatch attached.
Comment #4
ChaseOnTheWebComment #5
circuscowboy CreditAttribution: circuscowboy as a volunteer commentedpatch works as advertised and keeps my features happy.
Code looks good.
Comment #7
Mixologicdispatcher issues earlier. Back to rtbc
Comment #8
ChaseOnTheWebPer #7 back to rtbc
Comment #9
poker10 CreditAttribution: poker10 at ActivIT s.r.o. commentedPatch #3 looks good, thanks. Added just a simple test to show the real failure here. Reuploading with the test only version. Original patch is unchanged.
Comment #11
mcdruidThanks for adding the test. This LGTM!
Comment #13
poker10 CreditAttribution: poker10 at ActivIT s.r.o. commentedThanks all!