0

I have several views of users and I want to show all users with a certain role (example, "show_me_first") before all other users in all of these views.

I thought this would be easy, but apparently there is no "User: Role" as a sort criteria (I'm using Views 3.5 on Drupal 7.15). I can filter by role, but I don't want to filter; I just want to show users with the role "show_me_first" before all other users.

How can I implement this? Minimal view customization is best because I will have to repeat this for a lot of views. If there is a way to do it without using roles, I would be open to that as well.

1
  • You may need to write your own views handler for handling this sort option. Commented Sep 5, 2012 at 8:22

3 Answers 3

1

A common use case is to show content from "premium" users before content from other users. This is more binary than a true weighting. If this is your use case, consider using Views' attachments.

  1. Create your view. Add relationship for Content: Author.
  2. In main display, set filter by role to indicate that the author is not a "premium" user (e.g. User: Roles / none of / "premium", "other premium role", etc.)
  3. Add new views display of type "attachment"
  4. In attachment display, alter filter by role to include the roles excluded in main display
  5. In attachment settings, select your main display for "Attach to" setting; Select "Before" for Attachment position
2

It looks like it's not possible with one View (Sort By User Role, Sort By User Role: A New Idea?) which does kinda suck.

One option is to make two Views, one that filters on the user role you want at the top, and then one that filters OUT the user role.

Another option (this one would probably be easier to maintain) would be to create a field on the user accounts called Weight (you could hide it with Field Permissions), and then use Rules to populate that field with a high number for your chosen role when a user is created or edited, then you should be able to sort on that field. If you do this though don't forget to create a second rule to remove/change the value in that field if the role is removed form a user.

tl;dr

  • Create Weight field on users
  • When a user is created, fire off a rule that looks for a particular user role
  • If it finds it, put a number in the Weight field
  • Sort your view on the Weight field
  • Make an "opposite" rule to remove the Weight value on Role removal.
  • Profit
1

If memory serves me correctly, you use a Flags for other things along these same lines. So, if so, how about a global 'show me first' flag that you/your admins can set on a user? Kinda like your own custom sticky bit. A quick look here only allows one to sort on the flagged time, but you could use this flag to set a hidden from the user field that you could sort on.

Your Answer

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

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