We launched new forums in March 2019—join us there. In a hurry for help with your website? Get Help Now!
    • 38237
    • 83 Posts
    How to access user specific settings?

    settings can be retrieved
    $modx->getOption('setting_key');


    I tried
    $user->getOption('setting_key');


    But it returns the original settings not the user specific one.

    I tried clearing cache, logout and deleting cache folder contents; but no luck.

    NB. this is a customer setting, with boolean value. In settings it is "false" for that user it is true but i always get both values as 0

    This question has been answered by multiple community members. See the first response.

    • discuss.answer
      • 28042 ☆ A M B ☆
      • 24,524 Posts
      See Bob Ray's new blog post http://bobsguides.com/blog.html/2014/08/14/understanding-the-modx-getoption%28%29-method/ - in particular the section "Settings?".
        Studying MODX in the desert - http://sottwell.com
        Tips and Tricks from the MODX Forums and Slack Channels - http://modxcookbook.com
        Join the Slack Community - http://modx.org
        • 38237
        • 83 Posts
        Suzan,

        Thank you so much for your very fast response and assistance.

        But how can I call $modx->getoption() for a specific user?
        • discuss.answer
          • 38237
          • 83 Posts
          $user->getSettings();
          $user->getSettings();
            • 22303 MODX Staff
            • 10,725 Posts
            Settings are automatically aggregated, you should never need to call $user->getSettings().
              • 22303 MODX Staff
              • 10,725 Posts
              That said, settings are not meant to be changed at runtime. These are "configuration" settings. If you change them at runtime and expect a user to see them, you will need to log the user out and back in, or force reloading of their entire session including the settings changes.
                • 38237
                • 83 Posts
                Jason,

                Thank you for your contribution, the question was for getting userSettings other than the logged it one. I don't need it to be visible, i just wanted to get and set their values for an extra, which hopefully will be submitted by the middle of next week. You know documentation is not the best, so i had to keep digging here and there and found it in the forum somewhere.

                Thank you again.
                • discuss.answer
                  • 3749
                  • 24,544 Posts
                  I didn't see this answer above, so I thought I'd post a way to do it.

                  /* Get the raw value of the user setting */
                  $userId = 12;
                  $settingKey = 'some_setting';
                  $newValue = 'someValue';
                  
                  $fields = array(
                      'user' => $userId,
                      'key' => $settingKey,
                  }
                  
                  $userSetting = $modx->getObject('modUserSetting', $fields);
                  if ($userSetting) {
                      $settingValue = $userSetting->get('value');
                  
                      /* Put a new value in the setting */
                      $userSetting->set('value', $newValue);
                      $userSetting->save();
                  }


                  If you just need the value, and don't need to change anything, this will be much faster and more efficient:

                  /* Get the raw value of the user setting */
                  $userId = 12;
                  $settingKey = 'some_setting';
                  
                  $query = $modx->newQuery('modUserSetting', array(
                      'user' => $userId,
                      'key' => $settingKey,
                  ));
                  $query->select('value');
                  $settingValue = $modx->getValue($query->prepare());


                  As OpenGeek says, $modx->getOption() will get the value for the current user, but with getOption(), there's no way to tell whether you're getting a User Setting, a Context Setting, or a System Setting. The methods above will always get you a User Setting or null if one is not set. [ed. note: BobRay last edited this post 12 years, 1 month ago.]
                    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
                    • 38237
                    • 83 Posts
                    Bob,

                    You solution is of course correct, but if the user setting doesn't exist and setting key matches a system setting, the system one will be returned. At least it is like that according to my testing. Please correct me if I'm wrong .
                      • 3749
                      • 24,544 Posts
                      We were both typing at the same time. See the edit to my post. wink
                        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