We launched new forums in March 2019—join us there. In a hurry for help with your website? Get Help Now!
    • 49407
    • 159 Posts
    I am trying to pass the $user object to a Snippet. So far it fails.

    Could I be doing it wrong or do Snippets only allow passing arrays and other non-objects?

    // Create the user object
                $user   = $modx->getObject( 'modUser', array( 'username' => $userName ));
                if ( !is_object($user) || !($user instanceof xPDOObject) ) {
                    $errorMsg = 'Failed to create object of type: modUser';
                    $modx->log(modX::LOG_LEVEL_ERROR, $errorMsg, '', 'utilityProcessor Reader Portal');
                    return;
                }
                $userID = $user->get('id');
                
                $userAliasMeta = $modx->getObject('UserMeta', array( 'user_id' => $userID, 'key' => 'alias' ));
                if ( !is_object($userMeta) || !($userMeta instanceof xPDOObject) ) {
                    $errorMsg = 'Failed to create object of type: UserMeta. There may not be an alias meta for this user.';
                    $modx->log(modX::LOG_LEVEL_ERROR, $errorMsg, '', 'utilityProcessor Reader Portal');
                }
                $userAlias = $userAliasMeta->get('value');
                
                $modx->setPlaceholders(array('oztitle' => $userAlias));
                
                $userMeta = $modx->getCollection('UserMeta', array( 'user_id' => $userID ));
                if ( !is_object($userMeta) || !($userMeta instanceof xPDOObject) ) {
                    $errorMsg = 'Failed to create object of type: UserMeta. There may not be any meta for this user.';
                    $modx->log(modX::LOG_LEVEL_ERROR, $errorMsg, '', 'utilityProcessor Reader Portal');
                }


    Then
    $output .=  '[[!viewProfilePreProcessor?&user='.$user.'&userMeta='.$userMeta.']]';
            $output .=  '[[!$profile_view]]';
            $modx->setPlaceholders( array( 'utility.pageClass' => 'profile', 'utility.output' => $output ));
            return;

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

    • discuss.answer
      • 49407
      • 159 Posts
      Well, I read somewhere else, no, you can't pass objects to a Snippet.
        • 44195
        • 293 Posts
        What about using $modx->runSnippet() ?
          I'm lead developer at Digital Penguin Creative Studio in Hong Kong. https://www.digitalpenguin.hk
          Check out the MODX tutorial series on my blog at https://www.hkwebdeveloper.com
          • 3749
          • 24,544 Posts
          Snippet return values are always strings, but even if you were able to return a user object, you browser wouldn't know what to do with it.

          Is UserMeta a custom table (rather then the regular user profile)? If you've set it up for use with xPDO, you should be able to make a single call to getCollectionGraph() and ditch most of your code.

          BTW, you are not creating those objects. You are retrieving them from the database. Creating objects is done with $modx->newObject() or by calling the create processor.

          I'm not sure what you're trying to do, but at first glance, it looks like you are going the long way around to produce a simple user profile.

          You might look at ClassExtender for your UserMeta data.
            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
            • 4172
            • 5,888 Posts
            you can pass objects to snippets, but like muzzstick said, you would do

            $params = array();
            $params['userMeta'] = $userMeta;
            
            $output = $modx->runSnippet('yoursnippet',$params);
              -------------------------------

              you can buy me a beer, if you like MIGX

              http://webcmsolutions.de/migx.html

              Thanks!