We launched new forums in March 2019—join us there. In a hurry for help with your website? Get Help Now!
    • 49359
    • 16 Posts
    Please Help me ;(
    I use Login modx. When user login i can see this [[+username]], but I need User Group ((
     [[!Register?
        &submitVar=`registerbtn`
        &activationResourceId=`[[*id]]`
        &activationEmailTpl=`myActivationEmailTpl`
        &activationEmailSubject=`Спасибо за регистрацию!`
        &usergroups=`Users`
        &successMsg=`Спасибо за регистрацию!`
        &validate=`nospam:blank,
        username:required:minLength=^4^,
        password:required:minLength=^4^,
        password_confirm:password_confirm=^password^,
        fullname:required,
        email:required:email`
        &placeholderPrefix=`reg.`
        ]]
    register chunk

    <?php
    $user = $modx->getUser();
    return $user->get('usergroup');
    dont work

    [[+usergroup]]
    dont work too

    Help(((

    This question has been answered by multiple community members. See the first response.

      • 49529
      • 196 Posts
      Hi.
      You should use
      $modx->getUserGroups(); // for group IDs
      


      or

      $modx->getUserGroupNames(); // for names
      


      Don't forget that user can be a member of several groups, so returned value will be array and not a particular ID or name. You should iterate through this array to get actual results, ever if there is only one array entry.
        • 3749
        • 24,544 Posts
        For a particular user's group memberships, that should be:

        $user->getUserGroups();
        $user->getUserGroupNames();
          Did I help you? Buy me a beer
          Get my Book: MODX:The Official Guide
          MODX info for everyone: http://bobsguides.com/modx.html
          My MODX Extras
          Bob's Guides is now hosted at A2 MODX Hosting
        • discuss.answer
          • 49359
          • 16 Posts
          Quote from: BobRay at Feb 24, 2015, 05:16 PM
          For a particular user's group memberships, that should be:

          $user->getUserGroups();
          $user->getUserGroupNames();
          Fatal error: Call to a member function getUserGroups() on a non-object in /home/arthost/promoil.test-lemon.com/www/core/cache/includes/elements/modsnippet/58.include.cache.php on line 2

          I do it with snippet

          <?php
          $user = (!empty($userId)) ? $modx->getObject('modUser', $userId) : $modx->user;
           
          if (is_object($user)) {
          
              $modx->toPlaceHolder('user.toJSON', $user->toJSON());
           
           
          
              $profile = $user->getOne('Profile');
           
             
              
              $arry = $user->getUserGroupNames();
              $modx->toPlaceHolder('user.groupnames', $arry[0]);
           
            
              
          }
          


          [[+user.groupnames]]
          • discuss.answer
            • 3749
            • 24,544 Posts
            $modx->user is always set in the front end, but may not work if the user isn't logged in (and you're not logged in if you're previewing from the Manager).

            Try this:

            if ($modx->user->get('username') !== '(anonymous)') {
                 $modx->setPlaceholder('user.groupnames', implode(', ', $modx->user->getUserGroups()));
            } else {
                $modx-setPlaceholder('user.goupnames', '(anonymous) user - no groups';
            }
            


            FYI, it's toPlaceholder(), not toPlaceHolder().

            [update] Just re-read your post and noticed you're using the Register snippet. Depending on where you are in the process, the user who is registering may not belong to any groups yet or have an ID. In fact the user may be the (anonymous) user at the point where your snippet runs.
              Did I help you? Buy me a beer
              Get my Book: MODX:The Official Guide
              MODX info for everyone: http://bobsguides.com/modx.html
              My MODX Extras
              Bob's Guides is now hosted at A2 MODX Hosting