On March 26, 2019 we launched new MODX Forums. Please join us at the new MODX Community Forums.
Subscribe: RSS
  • 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.

    • 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.
      • 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
          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
            $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