Come together with the global Drupal community in Rotterdam, 28 Sept – 1 Oct 2026. Sessions, contribution, connection, and Early Bird savings until 8 June.
One problem is that a user can have multiple roles. Should the [role] token return 1) all roles separated by commas, 2) the role with the lowest/highest rid value in the database 3) the role with the lowest/highest "name".
So, we'll need the proposal to handle these kinds of problems
Perhaps we need lots of tokens:
[role-name] - the name with highest rid value
[role-names] - ", " delimited list of roles like "anonymous", "authenticated", "admin"
[rid] - the highest rid for this user
[rids] - comma delimited list of rids
That could be overly heavy/complex, but we need some specific rules so we will return a consistent value.
Hi,
Moshe in my opinion Role weights is not the correct thing, I'll explain by my own example. In my site I have a designer, photographer, writer, etc;. There isn't really a higher role and lower one - just different tasks, thus different access.
As a remark, here is a another possible use for this feature. As a non-developer, this is so far the easiest way I've though in order to accomplish the redirecting task.
Here is a patch against token_user.inc to add two tokens for user roles :
['role-names'] is a comma separated list of names
['roles-ids'] is a comma separated list of rids
The problem is that any character can be used in the name of a role, so if a name includes a comma, there is a confusion. I don't know if it would be appropriate to escape them in the token list.
I use this patch with the module workflow_ng that provides a very handy REGEX feature that makes it easy to match a given role in the list.
While I like the implementation and appreciate your providing this code, the issue is that we need agreement on a good way to implement this.
As you point out, your implementation makes it relatively difficult to deal with the result because it's impossible to know if the , is from the separation of roles or the name of the role.
I'm marking this as code needs work not because I'd like to see some more work in the code, but really because I'd like to see some discussion about token should handle tokens which are likely to return multiple values.
menupath and catpath are two existing tokens which probably do the wrong thing (delimit with / characters) but since they are used by pathauto that's considered acceptable.
Perhaps the answer is that consuming modules should have the ability to specify what they want as the delimiter. I welcome input of ideas on the subject.
Hi,
I think that this concept is a valid one, in the sense that a typical user would not create a role name with a comma. It's possible for him to do so, but in that rare case I would recommend this user to use the ['roles-ids'].
Having a simple solution that serves 99% of the use cases is better IMO.
In my particular case, i have a bunch of roles of the type : "Author of X", "Author of Y", "Editor of X", "Editor of Y" and so on. No matter the delimiter, i use workflow_ng REGEX feature to know whether the user is an author or an editor, or whether he belongs to group X or Y (he may have several roles).
But we can imagine a module that need to loop on each role name, which raises the question of being able to separate the names with the security of not cutting some of them. The token module could have a parameter called, say, "Token lists delimiter", which would default to a comma or a slash, and which would be available through a simple API call to other modules. This way, the administrator of the site could use any character in the names of the roles, and configure a special, unused character as a separator in token lists.
But as Amitaibu says, using rids instead of names is a simpler way to retrieve the user's roles if we need to be sure to separate each role from another. And then, get the names from their ids.
I think that this issue should be "won't fixed" in favor of that one. If someone has an idea of how this can be usefully implemented without knowing weights, I'm all ears, but I don't see how the current code is all that useful.
I've provided a patch over on that issue implementing tokens for "heaviest" and "lightest" role ids/names. Anyone who requested this functionality is encouraged to test it and I'll commit and roll Role Weights 5.x-1.3. Thanks!
If there's a better way to do this now, I would appreciate being pointed in that direction or giving some suggestions. But this was the best solution I could find today. So I hope the solution to this problem isn't completely abandoned and the status can be changed from "won't fix" to "needs work". ;-)
Comments
Comment #1
gregglesThis is certainly possible
One problem is that a user can have multiple roles. Should the [role] token return 1) all roles separated by commas, 2) the role with the lowest/highest rid value in the database 3) the role with the lowest/highest "name".
So, we'll need the proposal to handle these kinds of problems
Perhaps we need lots of tokens:
[role-name] - the name with highest rid value
[role-names] - ", " delimited list of roles like "anonymous", "authenticated", "admin"
[rid] - the highest rid for this user
[rids] - comma delimited list of rids
That could be overly heavy/complex, but we need some specific rules so we will return a consistent value.
Comment #2
moshe weitzman commentedperhaps let role_weight module expose the new tokens. it knows which role is most important for a given user.
Comment #3
amitaibuHi,
Moshe in my opinion Role weights is not the correct thing, I'll explain by my own example. In my site I have a designer, photographer, writer, etc;. There isn't really a higher role and lower one - just different tasks, thus different access.
As a remark, here is a another possible use for this feature. As a non-developer, this is so far the easiest way I've though in order to accomplish the redirecting task.
Comment #4
ricflomag commentedHere is a patch against token_user.inc to add two tokens for user roles :
The problem is that any character can be used in the name of a role, so if a name includes a comma, there is a confusion. I don't know if it would be appropriate to escape them in the token list.
I use this patch with the module workflow_ng that provides a very handy REGEX feature that makes it easy to match a given role in the list.
Hope it helps.
Comment #5
ricflomag commentedI've forgotten to attach the patch...
Comment #6
ricflomag commentedPlease read ['roles-names'] instead of ['role-names'].
Comment #7
gregglesWhile I like the implementation and appreciate your providing this code, the issue is that we need agreement on a good way to implement this.
As you point out, your implementation makes it relatively difficult to deal with the result because it's impossible to know if the , is from the separation of roles or the name of the role.
I'm marking this as code needs work not because I'd like to see some more work in the code, but really because I'd like to see some discussion about token should handle tokens which are likely to return multiple values.
menupath and catpath are two existing tokens which probably do the wrong thing (delimit with / characters) but since they are used by pathauto that's considered acceptable.
Perhaps the answer is that consuming modules should have the ability to specify what they want as the delimiter. I welcome input of ideas on the subject.
Comment #8
amitaibuHi,
I think that this concept is a valid one, in the sense that a typical user would not create a role name with a comma. It's possible for him to do so, but in that rare case I would recommend this user to use the ['roles-ids'].
Having a simple solution that serves 99% of the use cases is better IMO.
Comment #9
ricflomag commentedIn my particular case, i have a bunch of roles of the type : "Author of X", "Author of Y", "Editor of X", "Editor of Y" and so on. No matter the delimiter, i use workflow_ng REGEX feature to know whether the user is an author or an editor, or whether he belongs to group X or Y (he may have several roles).
But we can imagine a module that need to loop on each role name, which raises the question of being able to separate the names with the security of not cutting some of them. The token module could have a parameter called, say, "Token lists delimiter", which would default to a comma or a slash, and which would be available through a simple API call to other modules. This way, the administrator of the site could use any character in the names of the roles, and configure a special, unused character as a separator in token lists.
But as Amitaibu says, using rids instead of names is a simpler way to retrieve the user's roles if we need to be sure to separate each role from another. And then, get the names from their ids.
Comment #10
fagoalthough the order of roles can't be controlled, I think the posted patch would already be a useful addition.
Anyway, what token could really need is proper (unique) way for handling multiple values.
Comment #11
gregglesfago - I agree. Now, do you have a proposal for that way? ;)
Comment #12
fagoYes I have! Sry, that it took that long, but here it is:
http://groups.drupal.org/node/5744
Comment #13
gregglesI created an issue for this in role weights since that would be the best place to do it #318777: Provide tokens for the highest weighted role.
I think that this issue should be "won't fixed" in favor of that one. If someone has an idea of how this can be usefully implemented without knowing weights, I'm all ears, but I don't see how the current code is all that useful.
Comment #14
pfaocleI've provided a patch over on that issue implementing tokens for "heaviest" and "lightest" role ids/names. Anyone who requested this functionality is encouraged to test it and I'll commit and roll Role Weights 5.x-1.3. Thanks!
Comment #15
wickwood commentedThanks for the patch in (#5), it helped me solve a problem I was having with the Mailchimp Module: #539136: Role as a Merge Variable (#7)
If there's a better way to do this now, I would appreciate being pointed in that direction or giving some suggestions. But this was the best solution I could find today. So I hope the solution to this problem isn't completely abandoned and the status can be changed from "won't fix" to "needs work". ;-)
All the Best,
Steve