Output User Profile Information from specific User Group

Background Information:

I’m working on a blog like page and I want an about page where the different authors are listed with their image and some other info. Every author has their own MODX user profile so this is where the information should be pulled from.

  • MODX Revolution 2.6.5-pl
  • PHP 7.2.16
  • MySQL 5.7.25
  • Extras: Ace, archivist, Articles, CKEditor, FormIt, getpage, getresources, MIGX, Quip, SimpleCart, taglister

I want to achieve:

Probably an array (?) which grabs all user profiles, preferably from a specific user group so I can control which ones are shown and pull the wanted information from there. To make things more specific let’s say I want to grab their Name and their Photo.

What I got so far:

Not too much honestly, I found this post in the old forums which seems to do sort of similar things and changed it as far as I could understand it to:

$profile = $modx->getObject('modUserProfile', array('internalKey' => $uid));
if ($profile) {
  $username= $profile->get('username');
  $photo= $profile->get('photo');
}

All of this is in a snippet [[getUserInfo]] but as it is right now it doesn’t do anything. I tried to echo the $username into my markup, but that didn’t show up. Is this the right start to tackle my issue? If so:

  1. What am I doing wrong?
  2. How do I define which user (group) gets picked?

Thanks alot for your help!

1 Like

I just turned this up:

I’d switch line 20 to foreach($users as $user) and then you should be able to use something very similar to it.

$username = $user->get('username');
$photo = $user->getOne('Profile')->get('photo');

Profiles are effectively split into two tables. Username is part of the ‘user’ table and ‘photo’ is part of the profile. It appears you’re querying the profile for the username above which would have lead to nothing being returned.

Here’s a breakdown of fields to help with what should come from which part of the user object (‘core’ vs ‘profile’): https://docs.modx.com/revolution/2.x/administering-your-site/security/users

1 Like

Thanks alot @jcdm, that (almost) worked great! :pray:

What’s not showing yet is the photo of the user. Here’s what my snippet looks like so far:

<?php
  $usergroup = 2;
  $c = $modx->newQuery('modUser');
  $c->innerJoin ('modUserProfile','Profile');
  $c->innerJoin ('modUserGroupMember','UserGroupMembers');
  $c->innerJoin ('modUserGroup','UserGroup','`UserGroupMembers`.`user_group` = `UserGroup`.`id`');
  // $c->leftJoin ('modUserGroupRole','UserGroupRole','`UserGroupMembers`.`role` = `UserGroupRole`.`id`');    // don't need this atm

  $c->where(array(
    'active' => true,
    'UserGroupMembers.user_group' => $usergroup,
    // 'UserGroupMembers.role' => '5',    // don't need this atm
  ));

  $users = $modx->getCollection('modUser',$c);

  foreach($users as $user) {
    $username = $user->get('username');
    $photo = $user->getOne('Profile')->get('photo');
    $quote = $user->getOne('Profile')->get('city');         //renamed profile field city == quote
    $about = $user->getOne('Profile')->get('address');      //renamed profile field address == about
    
    echo '<div class="team red">
            <div class="profile">
                <img src="' . $photo . '" alt="' . $username . '">
            </div>
            <div class="quote">
                <h3>' . $username . '</h3>
                <p>"' . $quote . '"</p>
            </div>
            <div class="description">
                <p>' . $about . '</p>
            </div>  
        </div>';
  }

The two additional variables $quote and $about work fine. I just don’t get the image. The compiled code is empty: <img src=""

Am I missing something?

Update: I added a few more users to the role which is displayed, and oddly one of them has the image shown, so the problem might be somewhere else. Investigating…

Also is this the appropriate way to get a PHP variables output in the HTML structure (by writing the HTML in the snippet)?

Hmmm…one photo working but others not. That is intriguing. Do they all seem to have the same path in the field value? Are the names fairly long perchance?

I don’t see anything you’ve done wrong with the photo field. If the others are working then getting nothing from the photo field seems odd to me.

The best way to manage output is to move the HTML into a chunk. So, create a new chunk and put the HTML in there and give it a name.

Then, put your variables into an array instead and call getChunk with that array:

$userInfo = array();
$userInfo['username'] = $user->get('username');
...
echo $modx->getChunk('nameOfTheChunk',$userInfo);

Then in the chunk, username (and any other fields you’ve set) will be available in a placeholder. Placeholders look like [[+this]]

e.g. <div class="team red"><h3>[[+username]]</h3></div>

While there’s nothing wrong with what you’ve done, it makes it tricky to tweak presentation without messing with logic. The getChunk method is your friend for separating the two.

Let us know where you get to with the similarities/differences in photo paths.

1 Like

You might look at the Peoples snippet. I think it does most of what you want out of the box.

1 Like

So turns out for some reason the image path wasn’t set anymore within the user profiles (except the one who worked obviously). Not sure why that happened, but will have a lookout for that in the future.

Thanks a ton! You opened up a whole new world for me there, made my day! Always wondered how to combine chunks with snippets until now… :blush:

@bobray Now that it’s working I’ll probably keep it as it is, but I will definitely look into that if I need something similar in the future! Thanks alot!

Here’s a helpful link on snippets that includes the chunk stuff I mentioned above.
https://docs.modx.com/revolution/2.x/developing-in-modx/basic-development/snippets/how-to-write-a-good-snippet

Turns out there’s at least one thing I’ve mislead you on now that I take a glance myself - specifically, do not print or echo.

Better to amass output via something like $output .= $modx->getChunk(...) then end with return $output; apparently. :+1:t2:

1 Like

Thanks alot for guiding me to this anyway, I’ll look more into it!

I would like to mark your first response as the correct answer, but I can’t find a way to do that, am I not allowed to do this?

Hey,

We had enabled this feature for the support sections. But your thread was started in the General section. I’ve enabled it in this section as a test, and we’ll monitor going forward from here to see if it is appropriate for this section.

1 Like

@elizabeth I see, so was my thread in the wrong section?
If so feel free to move it of course! :blush:

1 Like

This thread is incredibly useful for dealing with users of a site.

There seem two key issues that are explained well: Getting the user tables (two tables not one, because of the extended profile), which is obviously important for many purposes.

The second thing is the use of chunks of html in snippets, for the output. This is something Modx does really well, if you know the code.

@vibedesign I wouldn’t say wrong section, we just hadn’t considered every avenue is all.

1 Like

This topic was automatically closed 2 days after discussion ended and a solution was marked. New replies are no longer allowed. You can open a new topic by clicking the link icon below the original post or solution and selecting “+ New Topic”.