Problem/Motivation

Steps to reproduce:

  1. Navigate to admin/people/permissions/roles
  2. Show row weights
  3. Assuming a default Drupal install, with roles anonymous user, authenticated user, administrator, with weights 0, 1, 2, respectively:
  4. Add a role of manager (its weight will default to 3)
  5. Set manager's weight to 10. Save.
  6. The weight field will still show as 3.
  7. 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).

Support from Acquia helps fund testing for Drupal Acquia logo

Comments

ChaseOnTheWeb created an issue. See original summary.

ChaseOnTheWeb’s picture

It 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.

ChaseOnTheWeb’s picture

ChaseOnTheWeb’s picture

Status: Active » Needs review
circuscowboy’s picture

Status: Needs review » Reviewed & tested by the community

patch works as advertised and keeps my features happy.

Code looks good.

Status: Reviewed & tested by the community » Needs work

The last submitted patch, 3: use-specified-row-weights-2679983-3.patch, failed testing. View results

Mixologic’s picture

dispatcher issues earlier. Back to rtbc

ChaseOnTheWeb’s picture

Status: Needs work » Reviewed & tested by the community

Per #7 back to rtbc

The last submitted patch, 9: 2679983-9_test-only.patch, failed testing. View results

mcdruid’s picture

Issue tags: +RTBM

Thanks for adding the test. This LGTM!

  • poker10 committed aaddb71 on 7.x
    Issue #2679983 by poker10, ChaseOnTheWeb: Specified role weights not...
poker10’s picture

Status: Reviewed & tested by the community » Fixed
Issue tags: -RTBM

Thanks all!

Status: Fixed » Closed (fixed)

Automatically closed - issue fixed for 2 weeks with no activity.