We launched new forums in March 2019—join us there. In a hurry for help with your website? Get Help Now!
    • 18654
    • 191 Posts
    I have a question about the getOption function. According to this page in the wiki I ought to be able to do this:
    $booContext = $modx->getObject('modContext','boo');  
    $booSiteStartId = $booContext->getOption('site_start');
    


    Here is my implementation of this which fails (The key is successfully echoed, so I know that the objects are being retrieved):
    <?php
    //Get Option Attempt 1 - Fails
    $c = $modx->newQuery('modContext');
    $contextObjects = $modx->getCollection('modContext',$c);  
    
    foreach($contextObjects  as $contextObject) {
    	echo $contextObject->get('key') . '<br />';
    	echo $contextObject->getOption('site_name') . '<br /><br />';
    }	
    


    What does work (but it’s not as ideal way to get the info as additional logic would be required to get the specific info that I want) is this:
    <?php
    $c = $modx->newQuery('modContextSetting');
    $modContextSettings = $modx->getCollection('modContextSetting',$c);  
    
    foreach($modContextSettings  as $modContextSetting) {
    	echo $modContextSetting->getOption('site_name') . '<br /><br />';
    }
    ?>	
    


    Is this a bug in revo or are the docs wrong or is my code wrong? (I’m running RC-1 rev6478)

    -matt
      God does not save those who are only imaginary sinners. Be a sinner, and let your sins be strong, but let your trust in Christ be stronger, and rejoice in Christ who is the victor over sin, death, and the world.
      • 22303 MODX Staff
      • 10,725 Posts
      modContext::getOption() is inherited from xPDOObject, and has nothing to do with modX options, or modContextSettings. This is internal options for the xPDOObject instance and will never contain system, context or user settings.

      Unfortunately, you can’t just look at the ContextSettings table itself. To get a setting for a context, you need to load that Context as if you were going to use it, because settings are an aggregation of system, context and user.
      <?php
      $modx->switchContext($context);
      $siteStart = $modx->getOption('site_start');
      ?>

      Beware though, this means you are loading the Context’s settings and entire Resource map into memory.

      If you just want to get a modContextSetting instance directly, you would do:
      <?php
      $object = $modx->getObject('modContextSetting', array('context' => $context, 'key' = 'site_start'));
      if ($object) $siteStart = $object->get('value');
      ?>
        • 18654
        • 191 Posts
        Thanks for the response! Is it just me or are the docs a bit misleading here? Here’s what I ended up up doing:
        <?php
        $contexts = array();
        $c = $modx->newQuery('modContext');
        $c->innerJoin('modContextSetting', 'ContextSettings');
        $c->where(array(
        		'ContextSettings.key' => 'site_type'
        		,'ContextSettings.value' => 'chapter_subdomain'
        	));
        $contextObjects = $modx->getCollection('modContext',$c);  
        
        foreach($contextObjects as $contextObject) {
        	$currentContext = $contextObject->get('key');
        	
        	$currentContextSettings = $modx->getCollection('modContextSetting', array('context_key' => $currentContext));
        	foreach($currentContextSettings as $currentContextSetting) {
        		$currentContextKey =  $currentContextSetting->get('key');
        		$contexts[$currentContext][$currentContextKey] = $currentContextSetting->get('value');
        	}	
        }	
        
        foreach($contexts as $key => $context) {
        	echo $key . "\n";
        	echo $context['site_url'] . "\n\n";
        }
        
        ?>
        


        For the final loop (that currently just echoes key/values) I’ll be getting various settings for external data related to the context such as Google Data API info (calendar name, login info, etc.) and Wordpress API info (username and pass for each blog), and then creating resources / XML files related to that context (ie Google Calendar Events, blog posts, blog comments). That’s why I needed to be able to get values by key for each context.

        Thanks again for your help!

        -matt
          God does not save those who are only imaginary sinners. Be a sinner, and let your sins be strong, but let your trust in Christ be stronger, and rejoice in Christ who is the victor over sin, death, and the world.
          • 3749
          • 24,544 Posts
          OpenGeek will correct me if I’m wrong here, but you may be making this too hard (depending on what you’re trying to do).
          $modx->getOption('setting_name')
          will get the setting value for the current context. You can switch to a context first if you need to (then switch back if necessary).

          Context settings will override system settings automatically and user settings will override context settings, so you’d be getting whichever one has the highest priority.
            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
            • 18654
            • 191 Posts
            I think OpenGeek acknowledged that approach here:
            Quote from: OpenGeek at Apr 02, 2010, 09:12 AM

            <?php
            $modx->switchContext($context);
            $siteStart = $modx->getOption('site_start');
            ?>

            Beware though, this means you are loading the Context’s settings and entire Resource map into memory.
            However, note the caution, "Beware though, this means you are loading the Context’s settings and entire Resource map into memory." My understanding of this caution is that this approach would be much more processor intensive, presumably causing the script to take longer to run. In my case, as our organization grows I may be looping through as many as 50 contexts so the extra time could add up.
              God does not save those who are only imaginary sinners. Be a sinner, and let your sins be strong, but let your trust in Christ be stronger, and rejoice in Christ who is the victor over sin, death, and the world.
              • 3749
              • 24,544 Posts
              Good point. I didn’t realize you were dealing with that many contexts.
                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
                • 22303 MODX Staff
                • 10,725 Posts
                This is still a problem as raw context data is merged as system, then context, then user, and is processed for dynamic setting values, i.e. if a system setting is defined with the value "https://{http_host}{base_url}" then http_host and base_url will be set from those existing setting values. This only works if the setting is already set in the context you are trying to determine the setting value for, IOW the dynamic setting you attempt to use must already be set in memory for the proper context, and natural order of the settings in the database affects this ordering at the moment.

                I think the error in the documentation was actually a subconscious suggestion from Shaun that we need a way to hide this complexity behind a method that is easy for developers to call, e.g.:
                <?php
                $value = $modx->getContextOption($context, $key);
                ?>
                  • 36613
                  • 328 Posts
                  I try this and work:

                  $ctx = $modx->getContext('sports');

                  if you want site_start of context sport use:

                  $ctx->getOption('site_start');
                    • 13428 ☆ A M B ☆
                    • 1,031 Posts
                    Quote from: opengeek at Apr 02, 2010, 04:12 AM

                    If you just want to get a modContextSetting instance directly, you would do:
                    <!php
                    $object = $modx->getObject('modContextSetting', array('context' => $context, 'key' = 'site_start'));
                    if ($object) $siteStart = $object->get('value');
                    ?>

                    This has to be:

                    <!php
                    $object = $modx->getObject('modContextSetting', array('context_key' => $context, 'key' = 'site_start'));
                    if ($object) $siteStart = $object->get('value');
                    ?>