We launched new forums in March 2019—join us there. In a hurry for help with your website? Get Help Now!
    • 43592
    • 97 Posts
    Dear Forum members,

    I've a little problem with user group names. The following script works fine so far but the It lists the wrong user group name for users, who are in more than one group.
    Especially the "2nd level admin" ( its a user group) is listed in my $output and I dont want that, because that user is in another preffered group for listing.
    Of course the "getUserGroupNames()" gets an array and I thought I could solve that over the user-group ranking in the ACL's to get the $output right ("array part" $benutzergruppe[0]).
    My thinking was wrong so far .. Any ideas?

    //get usergroup name
    $benutzergruppe = $modx->getObject('modUser', $userArray['id'])->getUserGroupNames();
    
    
    //Ausgabe
    $output .="<tr><td><strong>".$userArray['fullname']."</strong></td><td>".$userArray['fax']."</td><td>".$benutzergruppe[0]."</td><td>".$userArray['zip']."</td><td>".$userArray['city']."</td><td>".$userArray['phone']."</td><td>".$emailadr."</td><td>".$link."</td><td>".$userArray['state']." (".$userArray['country'].")</td></tr>";
    
    }
    

    This question has been answered by Bruno17. See the first response.

      • 4172
      • 5,888 Posts
      if you want to get the primaryGroup of a user:

      current user:

      $groupObject = $modx->user->getPrimaryGroup();
      //do something with the groupObject


      or
      specific user:

      if ($user = $modx->getObject('modUser',$userid)){
          $groupObject = $user->getPrimaryGroup();
          //do something with the groupObject 
      }
        -------------------------------

        you can buy me a beer, if you like MIGX

        http://webcmsolutions.de/migx.html

        Thanks!
        • 43592
        • 97 Posts
        I did

        //$benutzergruppe = $modx->getObject('modUser', $userArray['id'])->getUserGroupNames();
        
        $benutzergruppe = $modx->getObject('modUser', $userArray['id'])->getPrimaryGroup();
        


        and got blank field out with

        return $benutzergruppe[0];


        What did I do wrong?
        • discuss.answer
          • 4172
          • 5,888 Posts
          seems the user has to be authenticated for getPrimaryGroup.
          try:

          $user = $modx->getObject('modUser', $userArray['id']);
          
          $userGroup = $user->getOne('PrimaryGroup');
          if (!$userGroup) {
              $c = $modx->newQuery('modUserGroup');
              $c->innerJoin('modUserGroupMember','UserGroupMembers');
              $c->where(array(
                  'UserGroupMembers.member' => $user->get('id'),
              ));
              $c->sortby('UserGroupMembers.rank','ASC');
              $userGroup = $modx->getObject('modUserGroup',$c);
          }
                  
          return $userGroup->get('name');
          [ed. note: Bruno17 last edited this post 10 years, 5 months ago.]
            -------------------------------

            you can buy me a beer, if you like MIGX

            http://webcmsolutions.de/migx.html

            Thanks!
            • 43592
            • 97 Posts
            Thanks a lot! That did it!