How to display a list users in a group on a page

Hi I am hoping someone can point me in the right direction. I want to display a list of users in a specific group on a page. I have had a look in the documentation and tried this code but I have obviously done it wrong as I get an error undefined

Whoops \ Exception \ ErrorException (E_WARNING)

Undefined property: Concrete\Core\Page\View\PageView::$app

<?php 
            // Version 9
            $repository = $this->app->make(\Concrete\Core\User\Group\GroupRepository::class);
            $group = $repository->getGroupByName('Administrators');
            ?>
            
            <?php
            $users = $group->getGroupMembers('Administrators');
             
            foreach ($userss as $user) {
              echo  $user['Administrators'];
            }
            ?>

I hope someone can help me
Many thanks
Jonathan

Looks like you need:

$app = \Concrete\Core\Support\Facade\Application::getFacadeApplication();

If being done in a controller $this->app->make() will work. Otherwise set $app and call $app->make()

Hi Justin
Thank for your quick response. I am actually trying to do it on a page template. Is that the wrong way? I am not sure what you meant if you could explain or give me an example that would be great. Sorry for being a bit dim
Thanks
Jonathan

In your case you’d want to set $app as I describe above and then this line:

$this->app->make(\Concrete\Core\User\Group\GroupRepository::class);

becomes:

$app->make(\Concrete\Core\User\Group\GroupRepository::class);

If you’re working, for instance, with a custom block, in the controller.php file $this->app is ready to use so there’s no need to set it…

Hi Justin, thanks for your help it is now working:

<?php 
            // Version 9
            $repository = app()->make(\Concrete\Core\User\Group\GroupRepository::class);
            $group = $repository->getGroupByName('Administrators');
            ?>
            
            <?php
          $users = $group->getGroupMembers('Administrators');
             
            foreach ($users as $user) {
              echo $user->getAttribute('full_name');
           }
          ?>

One other quick question, is it possible to make it list in alphabetical order or is that the default?

Many thanks for your
Jonathan

Something like this should do the trick…

$userNames = [];
foreach ($users as $user) {
     $userNames[] = $user->getAttribute('full_name');
}
asort($userNames);
foreach ($userNames as $uName) {
     echo $uName;
}
1 Like

Hi Justin, perfect that did the trick thank you for you help very much appreciated
Jonathan