• user settings through a plugin [Solved]#

  • zoadie Reply #1, 3 months, 1 week ago

    Reply
    I have an plugin on UserSave event that creates a ResourcesX for every UserX. See details here http://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?



  • BobRay Reply #2, 3 months, 1 week ago

    Reply
    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


  • zoadie Reply #3, 3 months, 1 week ago

    Reply
    Thanks!
    Works perfectly!