Jump to content
hollyvalero

Trying to sift and display content for users with multiple roles that correspond to a page reference field

Recommended Posts

The set up:

Lots of content shared by a variety of clients.
To view data specific to one client, I've set up a role - Acme - and an associated template acme.php, so when the Acme user logins in, I can pop them directly to their Acme page where they only see Acme stuff. This works fine.

That page loads various content pre-qualified by the client. I have the logins set to pop them to their page automatically.

$bills = $pages->find("template=trackerplus,  client=$page, limit=200, sort=title");

 

The challenge:

I have internal users who manage several clients and want to see that same data but instead of visiting 3 pages, they want one page that lists all of the data for each of their clients only. I  can add the roles: Acme, MegaCorp, for each internal user.


I want to do the same kind of content display, but I need to sort the content

If user jsmith is associated with the roles Acme, MegaCorp
if user kjones is associated with the roles Brown, United

jsmith logs in and sees the client data dump for acme and megacorp only.

 

The field: 

Each content item has a multiple page reference field called client that I need to match to the roles of the user... and I need a list of names, not ids and trying various ways none of which are working...

Assume I need to create a variable, and empty it out... then add the name for each one to the variable...

$clientList = '';
foreach ($user->roles as $role) {
     add $role->name to $clientList;
}
$bills = $pages->find("template=trackerplus, client=$clientList, limit=400, sort=title");

 

 

 

Any assistance would be most welcome! 😓

 

 

Share this post


Link to post
Share on other sites
2 hours ago, hollyvalero said:

$clientList = '';
foreach ($user->roles as $role) {
     add $role->name to $clientList;
}
$bills = $pages->find("template=trackerplus, client=$clientList, limit=400, sort=title");

I take this to mean you want the names of the user's roles, excluding the guest role, as a pipe separated string that you can use within a selector string.

One-liner:

$user_roles_string = $user->roles->find('name!=guest')->implode('|', 'name');

Other ways:

$user_roles = [];
foreach($user->roles as $role) {
    if($role->name === 'guest') continue;
    $user_roles[] = $role->name;
}
$user_roles_string = implode('|', $user_roles);
$user_roles_string = '';
foreach($user->roles as $role) {
    if($role->name === 'guest') continue;
    $user_roles_string .= "$role->name|";
}
$user_roles_string = rtrim($user_roles_string, '|');

 

Share this post


Link to post
Share on other sites
21 minutes ago, Robin S said:

I take this to mean you want the names of the user's roles, excluding the guest role, as a pipe separated string that you can use within a selector string.

One liner:


$user_roles_string = $user->roles->find('name!=guest')->implode('|', 'name');

 

 

That's exactly what I was looking for and it works beautifully!! Thank you so much!  I haven't done much with multiple roles per user... this is terrific!!

  • Like 2

Share this post


Link to post
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

  • Recently Browsing   0 members

    No registered users viewing this page.

×
×
  • Create New...