We launched new forums in March 2019—join us there. In a hurry for help with your website? Get Help Now!
    • 17851
    • 213 Posts
    I'm trying to get an extended field value when a user logs in. My call:

    [[!Login? &postHooks=`LoginSetSession` ]]


    LoginSetSession:

    $profile = $hook->getValue('login.profile');
    $ext = $profile->get('extended');
    
    $dealerid = $ext['dealerid'];
    $profileid = $ext['profileid'];
    
    $_SESSION['profileid'] = $profileid;
    $_SESSION['dealerid'] = $dealerid;
    return true;
      Mark Macatee
      President
      Power 10 Solutions
      http://www.power10solutions.com
      • 16348
      • 64 Posts
      Have you considered to get the extended fields from the user profile when you want to use them instead of using sessions? If the user is logged in, the profile data is available via:
      $profile = $modx->user->getOne('Profile');
        • 17851
        • 213 Posts
        Yes, and I'll probably end up doing that. I just realized I didn't really describe my problem. The problem is my code errors out when I login. In the Apache log, I get something like "get is called on a non-object". So I was trying to understand why I was getting an error.
          Mark Macatee
          President
          Power 10 Solutions
          http://www.power10solutions.com
          • 16348
          • 64 Posts
          You get that error because the first row in your code doesn't work. Your variable "profile" does not contain a xpdo object and that is why you can't call the get method.
          I don't know when in the process the logged in user is available in the modx object. You could try to replace the first row in your hook with my code above. Something like this could work:
          $profile = $modx->user->getOne('Profile');
          if(!empty($profile)) {
            $ext = $profile->get('extended');
           
            $dealerid = $ext['dealerid'];
            $profileid = $ext['profileid'];
           
            $_SESSION['profileid'] = $profileid;
            $_SESSION['dealerid'] = $dealerid;
          
            $status = true;
          } else
            // Something went wrong
            $status = false;
          }
          return $status;
          
            • 3749
            • 24,544 Posts
            According to the docs, your original code should work, unless it's somehow executing for the (anonymous) user, who has no profile, or you're testing it by previewing from the Manager or in a window of the same browser where you're logged into the Manager.

            Another possibility -- if you're creating the users yourself in code, they may not have User Profiles.
              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