Jump to content

Specific PHP Selector for pagefield dont work


Orkun
 Share

Recommended Posts

I'm trying to achieve a specific selection for a pagefield for specific users.

My pagefield should detect: If the current user is a superuser return all address-pages and when it's not a superuser, return all address-pages which the current user has created. It works fine for the superuser but i have no selection in all the other users.

if(wire('user')->isSuperuser()){
      return $pages->find("template=address");
}
else{
     return $pages->find("template=address, created_users_id=wire('user')->id");
}
Link to comment
Share on other sites

You probably want to differ by roles, which is just this:

if($user->hasRole("editor")) …

Edit: Should've read more closely.

Does the selector work if you try it in the backed under Pages>Find? Are the pages hidden, unpublished or otherwise view access controlled?

Link to comment
Share on other sites

You should be able to change the last line to look like this:

return $pages->find("template=address, created_users_id={$user->id}");

$user is another one of the ProcessWire variables that is available wherever $pages is, and references the current logged in user. It acts like the wire('user') function, but is a variable, which you can use directly in double-quoted strings.

Link to comment
Share on other sites

Ah now I see it. You cannot use functions inside of strings (wire() is a function, even though it's used as a variable).

$user is another one of the ProcessWire variables that is available wherever $pages is, and references the current logged in user.

That's not correct. The PHP selector computation is (maybe the only) place, where only the $pages variable is available at local scope (see here). To use the other api variables it's best to use wire(…), but accessing via the class scope does work too.

So both of these would work:

return $pages->find("template=address, created_users_id={$this->user->id}");
return $pages->find("template=address, created_users_id=" . wire('user')->id);
  • Like 3
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...