We launched new forums in March 2019—join us there. In a hurry for help with your website? Get Help Now!
    • 33379
    • 110 Posts
    Hi,

    I'm have set up Login Register, Update Profile and Login forms which all work, but I'd like the Users to be able to select their User Group in either the Register or Profile Update forms rather that having it assigned in the Register snippet call. Looking around the forum it seems the best way to do this would be with a posthook snippet although there is no actual example I can find. As a novice with PHP and MODx snippet writing I would welcome any enlightenment as to what is wrong with this:

    My form dropdown selector html:

        <div class="formLabel1">
          <label for="usergroup">Business Category<span class="error"></span>
          </label> 
        </div>
        <div class="formInput1">
          <select name="usergroup">
    	<option id="usergroup1" name="usergroup1" value="testGroup" />Test Group</option>
    	<option id="usergroup2" name="usergroup2" value="otherTestGroup" />Other Test Group</option>
          </select>
        </div>
    


    My Register call:

    [[!Register? 
      &submitVar=`registerbtn` 
      &activationResourceId=`4` 
      &activationEmailTpl=`visitorActivateEmailTpl` 
      &activationEmailSubject=`Thanks for Registering!` 
      &submittedResourceId=`5` 
      &postHooks=`lgnPhUserGroup1` 
    ]]
    


    My" lgnPhUserGroup1" snippet (where the problem most likely is):

    <?php
    $user = $hook->getValue('register.username');
    $userGroup = $hook->getValue('register.usergroup');
    $user->joinGroup($userGroup);
    $user->save();
    ?>
    


    Cheers- Zaphodx

    Revo 2.1.3
    PHP 5.2.17
    SQL 5.0.92-community
      • 3749
      • 24,544 Posts
      I think you're *very* close, but you need the user object, not the username so try this:

      <?php
      $user = $hook->getValue('register.user');
      
        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
        • 33379
        • 110 Posts
        Hi Bob,

        Thanks for that, I now get to the Registration Confirmation Page (I wasn't getting this before, but I was getting the confirmation email and the User was being created), but the User is still not added to the selected User Group. Is "register.user" a universal object or value, I don't have a field with this name in the form?
          • 3749
          • 24,544 Posts
          It should be available with getHook() according to the docs. If not, this should work:

          <?php
          $userName = $hook->getValue('register.username');
          $user = $modx->getObject('modUser', array('username'=>$userName));
          $userGroup = $hook->getValue('register.usergroup');
          $user->joinGroup($userGroup);
          $user->save();
          ?>
          
          
            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
            • 33379
            • 110 Posts
            Thanks again Bob, unfortunately, I now get this error message:

            "Fatal error: Call to a member function joinGroup() on a non-object in /home/wyevalle/public_html/core/cache/includes/elements/modsnippet/22.include.cache.php on line 10"

            this being the "22.include.cache.php" code:

            <?php
            function elements_modsnippet_22($scriptProperties= array()) {
            global $modx;
            if (is_array($scriptProperties)) {
            extract($scriptProperties, EXTR_SKIP);
            }
            $userName = $hook->getValue('register.username');
            $user = $modx->getObject('modUser', array('username'=>$userName));
            $userGroup = $hook->getValue('register.usergroup');
            $user->joinGroup($userGroup);
            $user->save();
            }
            


            Does this throw up any clues?

              • 33379
              • 110 Posts
              Going to sleep on it now, it's just past midnight here. Will give it another shot in the morning smiley
                • 3749
                • 24,544 Posts
                That means getObject() is failing. Either the username is not coming through from the register snippet, or the user doesn't exist yet.

                Putting this in before the getObject() call should tell you which it is:

                die('USER NAME: ' . $userName);
                  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
                  • 33379
                  • 110 Posts
                  Hi Bob,

                  OK, so that produces the message "USER NAME:".

                  Maybe it would be better to put this selector in the Login.UpdateProfile form, that way the User already exists. I'll try it and see if it works that way.
                    • 3749
                    • 24,544 Posts
                      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
                      • 33379
                      • 110 Posts
                      Yes, I looked at this, but I want the user to be able to select a group (a category in reality) so I'd have to either create ten versions of the call/form each with a different property in the call or find a way to change the call property dynamically. Alternatively, I could create an extended field in the User Profile called "Category", but I understand that extended fields are not necessarily that useful (although I'd be quite happy to be told otherwise smiley)

                      There's probably more than one way to achieve this so I will continue to explore options and report back with the solution, or any more questions...

                      Thanks again Bob