We launched new forums in March 2019—join us there. In a hurry for help with your website? Get Help Now!
    • 38190
    • 15 Posts
    I have an plugin on UserSave event that creates a ResourcesX for every UserX. See details here https://forums.modx.com/thread/74062/onusersave-am-i-doing-it-right-solved#dis-post-411474

    Now I want to add to each UserX the setting ('tree_root_id', $resourceXid). I added the following code in the same plugin and it adds the setting but it keeps creating new Resources over and over because it's onUserSave event.
     //allowing each user to see only their own resource in the resource tree
    $userSettings = $modx->newObject('modUserSetting');
    $userSettings->set('key', 'tree_root_id');
    $userSettings->set('value', $resource->get('id'));
    $userSettings->set('xtype', 'textfield');
    $userSettings->set('namespace', 'core');
    $userSettings->set('area', 'user');
     
    // use addMany() because settings are the many in a one-to-many relationship from user
    $user->addMany($userSettings);
    $user->save();
    


    If I remove the last line, doesn't add the setting.

    How shall I do it?

    [ed. note: zoadie last edited this post 14 years, 8 months ago.]
      • 3749
      • 24,544 Posts
      Put this at the top:


      if ($mode != modSystemEvent::MODE_NEW) {
         return;
      }


      Or wrap the code for new users in:

      if ($mode === modSystemEvent::MODE_NEW) {
          // new user code here
      }







      Then it will only execute when creating a new user.


      ---------------------------------------------------------------------------------------------------------------
      PLEASE, PLEASE specify the version of MODX you are using . . . PLEASE!
      MODx info for everyone: http://bobsguides.com/modx.html
        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
        • 38190
        • 15 Posts
        Thanks!
        Works perfectly!