We launched new forums in March 2019—join us there. In a hurry for help with your website? Get Help Now!
    • 49659
    • 86 Posts
    I'm trying to add some code in the 'SetUserPlaceholders' snippet to retrieve the array of group names a web context user is a member of. I've tried the following, but it is not returning the current values:

    ...
    if ($data->User) {
        $modx->toPlaceholders($data->User, $prefix);
    }
    //START new code block
    $user = (!empty($userId)) ? $modx->getObject('modUser', $userId) : $modx->user;
    if (is_object($user)) {
       $grpnam_arry = print_r($user->getUserGroupNames(), true);
       $modx->setPlaceholder('user.groupnames', $grpnam_arry);
       //debug entry:
       $modx->log(modX::LOG_LEVEL_ERROR, $grpnam_arry);
    }
    //END new code block
    
    return '';
    


    For example, if a particular user was first assigned to a group 'A' when the user was first created, but was later removed from the 'A' group and added to the 'B' and 'C' groups, the above code continues to return only a single value in the array, and that value is the old 'A' group. This is the value that persists in the log, even though the user is now a member of only groups 'B' and 'C':

    Array
    (
    [0] => A
    )

    Thanks in advance for any tweaks to the code that will give me current group name values!

    Karl Forsyth

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

    • discuss.answer
      • 49659
      • 86 Posts
      Disregard. I was able to get the groups listing using the 'Peoples.PeopleGroups' add-on.