Jump to content

Page selector getting users with specific role


Whistletoe
 Share

Recommended Posts

Hello everyone!

My first post here. I found Processwire a while ago and have recently been getting to know the system. So far i like it a lot.

Right now i'm trying to make a "page"-field where an admin can select and attach users with a specific role. I can get a list of all users, and filter by different fields, but i don't know how to access the roles. As i understand it, selectors can not be used to find user roles, so i wonder if anyone has another approach. I could add a new field to each user with the role names, but that seems a bit awkward.

Thanks in advance!

Link to comment
Share on other sites

  • 1 year later...

Just a quick note for future drive-by readers.  For some reason

$users = wire('pages')->find("template=user,roles=buyer,sort=email");

didn't work for me, but

$users = wire('users')->find("template=user,roles=buyer,sort=email");

did.  Not sure why.

Both work here - what version of PW are you running?

One thing to note though is that you shouldn't be overwriting $users as this contains all users in the system. If you redefine it, you won't have access to all users via $users further down in your code.

  • Like 1
Link to comment
Share on other sites

  • 4 months later...

i can verify that the "users" template doesn't work the same way as regular templates:

  • i have a "student" role
  • when i put "roles=student" as the custom selector, the field seems to work perfectly: it shows all users with the "student" role, and shows nobody else
  • however, when i'm trying to save the page, i get this message: Error saving field "student" - Page 1017 is not valid for student

if i use custom php (when of course i can't use autocomplete) it works perfectly with all of these:

  • return wire("users")->find("roles=student");
  • return wire("pages")->find("roles=student");
  • return $pages->find("roles=student"); // which, incidentally, is the same that the custom selector is supposed to resolve

in short, it must be something about the custom selector validation code; if i find some time i'll look some more into it

  • Like 1
Link to comment
Share on other sites

  • 9 months later...

Hi I was wondering if there's a workaround for this?

I created a new field called blog_author which is a page field. 
It has a selector of "'template=user, roles=editor".

When I try and save down a blog post that has the new blog_author field in it, it's returning the following error.

Error saving field "blog_author" - Page 1415 is not valid for blog_author (Page 1415 does not match findPagesSelector: template=user, roles=blog-author)

I can see the blog authors and select one without errors.

The error occurs when I'm saving.

Have tried to make the template type a basic page field type (instead of user) , but it returns the same error.

Link to comment
Share on other sites

After much searching, it looks like I needed a *= operator instead of = for Roles? AAArgh  ...

i.e. Not roles=blog-author but roles*=blog-author.

https://processwire.com/talk/topic/10628-error-saving-page-fields/

Seems a bit inconsistent on how Selector is supposed to work, at least going by the examples & documentation I've seen.

Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

×
×
  • Create New...