We launched new forums in March 2019—join us there. In a hurry for help with your website? Get Help Now!
    • 8425
    • 159 Posts
    Hello,
    I'm creating a plugin that listens to OnUserSave event. I need to know if it is a new user event or an update user. Does the code below would work:
     $eventName = $modx->event->name;
         $mode = $modx->event->mode;
         switch($eventName) {
           case 'OnUserSave':
             // New user created
             if ($mode == modSystemEvent::MODE_NEW) {
      
             } else if ($mode == modSystemEvent::MODE_UPD) {
    
             }
           break;     

    Thanks for your help
      • 3749
      • 24,544 Posts
      It should work but, it could be a little simpler. $mode is already set for you by MODX, and since there are only two possibilities for $mode, the "else if" can just be an "else". Also, if the plugin is only connected to one event, you don't need to get the event name in the plugin, and if you're not doing anything when updating a user, you can skip the "else" altogether:

      <?php
              if ($mode == modSystemEvent::MODE_NEW) {
                   /* new user created *./
                  /* do something */
        
              } else  {
                   /* Updating existing user */
                  /* do something else */ 
              }
      
        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
        • 8425
        • 159 Posts
        Thanks a lot Bob!