0

I want control over the order in which roles' columns are displayed on the Permissions page..

I want to add code to my module installation file so that, after newly-configured roles' yaml settings have been applied (they list the permissions for each new role), it displays all the roles in the order I want.

How do I do this?

Motivation

The roles' yaml settings are part of a module created by Features. Enabling the module in the target Drupal 8 system adds the new roles in the wrong order.

It is not possible to include the weights of the anonymous; authenticated and Administrator roles in the feature / module's yaml files as this causes a conflict that prevents the module from being enabled. So the solution is to apply the weights programmatically.

[edit] I mistakenly thought that the order of the roles' columns related to an hierarchy / inheritance of permissions. Corrected by @Berdir, thanks. Nevertheless I still want to display the roles' columns in the way I choose, so I have edited this question to reflect this.

1 Answer 1

1

Role weight is only used for some lists in the backend. It has no effect on the permissions that a user has.

The permissions or a user are always the sum of all permissions of all roles that he has, you can not remove permissions with roles.

All "users" either have the anonymous (when they're not a user, basically) or the authenticated user role. You can give anonymous users more permissions than authenticated users. But you can never remove any permission granted by the authenticated user role through another role.

This doesn't answer your question but considering the above, I don't think you are asking the right question. Whatever problem you are trying to solve exactly, setting role weights is not the answer ;) If this is not enough to solve your problem, then try explain the actual problem that you are trying to solve.

Edit:

Anyway, to actually set the role weight in e.g. hook_install(), you can do this:

$role = \Drupal\user\entity\Role::load('anonymous');
$role->setWeight(0);
$role->save();

Again, keep in mind that this has no effect on the permissions a user has, it just affects how the roles are listed on the role and permission administration pages.

2
  • I imported a Feature in which I had additional Roles. I wanted the imported roles to be ordered exactly the same way in the target system as in the source system (i.e. listed in the front-end UI of the target system as in the source.). Both systems were created using the Standard Profile so there is Anonymous and Authenticated roles that cannot be in the Feature or it conflicts. So I can't see a way, other than in install code, to control the Roles' weights. Arguably the target system should not be created from the Standard Profile then I wouldn't (mightn't?) have this issue.
    – iainH
    Nov 13, 2015 at 8:21
  • Still not sure why you need it but added a quick code example to do it programatically.
    – Berdir
    Nov 13, 2015 at 10:02

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.