We launched new forums in March 2019—join us there. In a hurry for help with your website? Get Help Now!
    • 1331
    • 129 Posts
    I'm using 2.2.9 and I'm working on an auto-populated list of contexts in a multi-site setup. I have gotten the list of all modContext 'key' values already. These show up immediately when a new context is created, which is exactly what I wanted. Now, I am trying to figure out how to grab things like "site_start" and "site_name" for inclusion in my foreach.

    Has anyone done something like this?
      • 1331
      • 129 Posts
      The goal here is to have a list of contexts, all linked to the ID that is set as the site_start for that given context.

      Here's what I have so far

      <?php
      $contexts = $modx->getCollection('modContext', array('key:NOT IN' => array('web', 'mgr')));
      foreach ($contexts as $context) {
          $listContext .= "<li>" . $context->get('key') . "</li>";
      }
      
      return $listContext;


      Now, I just came across this block of code to try to get the settings. I'm not really sure how I can tie these two things into one foreach loop because I'm not going to be explicitly declaring the context. It'll need to happen for all active contexts.

      $context = $modx->getObject('modContext','web');
      $settings = $context->getMany('ContextSetting');
      foreach ($settings as $setting) {
          echo 'Setting name: '.$setting->get('key').' <br />';
          echo 'Setting value: '.$setting->get('value').' <br />';
      }
        • 3749
        • 24,544 Posts
        You already have the context objects as the result of your getCollection() call so there's no need to call getObject() for them.

        Just put that second bit of code inside the first foreach() loop, leaving out the first line. (BTW, I think it's 'ContextSettings' not 'ContextSetting' -- I could be wrong). Also, the dots go outside the quotes in the two echo statements.

        There's a method here that might be a little faster if you know the context keys of the contexts you want to show.

        Since you want all settings, you'd have to modify the code. it would look something like this:

        $contexts = array(
             'context1',
             'context2',
             'context3',
        );
        
        foreach ($contexts as $context) {
            $settings = $modx->getcollection('modContextSetting',
                array(
                    'context_key' => $ctx,
                )
            );
            echo '____________________________________<br />';
            echo 'Context: ' . $context->get('key') . '<br />';
            foreach ($settings as $setting) {
                echo '    Setting name: ' . $setting->get('key') . '<br />';
                echo '    Setting value: ' . $setting->get('value') . '<br />'; 
            }
        
        }


          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
          Here is a different take on it using getCollectionGraph() to do it in one query and return a simple map of contexts with settings as key value pairs:

          <?php
                  $contexts = array();
                  /** @var modContext $contextsGraph */
                  $query = $modx->newQuery('modContext');
                  $query->where(array('modContext.key:NOT IN' => array('web', 'mgr', 'backup')));
                  $query->sortby($modx->escape('modContext') . '.' . $modx->escape('key'), 'ASC');
                  $contextsGraph = $modx->getCollectionGraph('modContext', '{"ContextSettings":{}}', $query);
                  foreach ($contextsGraph as $context) {
                      $contextSettings = array();
                      foreach ($context->ContextSettings as $cSetting) {
                          $contextSettings[$cSetting->get('key')] = $cSetting->get('value');
                      }
                      foreach ($contextSettings as $key => &$value) {
                          $matches = array();
                          if (preg_match_all('/{(.+)?}/', $value, $matches)) {
                              foreach ($matches[1] as $settingKey) $value = str_replace('{' . $settingKey .'}', $modx->getOption($settingKey), $value);
                          }
                      }
                      $contexts[$context->get('key')] = $contextSettings;
                  }
          
          [ed. note: opengeek last edited this post 12 years, 11 months ago.]
            • 1331
            • 129 Posts
            Sweet! Thanks guys. I'm going on test these in a few.

            Just to clarify: at the moment, I'm not really looking to display all the settings at once. Although I may have a need to it shortly. I just pasted that code from the RTFM.

            I just want all the settings to be available to me for use as I construct my foreach loop.

            My first use case will be to have something like the following:

            <li><a href="(site start value)">Some context</a></li>
              • 3749
              • 24,544 Posts
              In that case, the code in my blog post should do just what you want: http://bobsguides.com/blog.html/2013/06/19/displaying-modx-context-settings/,
              using the snippet there and this tag:


              <li><a href="[[~[[getContextSetting? &context=`someContext` $setting=`site_start`]]]]">Some context</a></li>
              [ed. note: BobRay last edited this post 12 years, 11 months 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
                • 1331
                • 129 Posts
                Oh snap! This looks like the work is already done for me. Haha. Thanks Bob.
                  • 1331
                  • 129 Posts
                  Bob, this is strange. When I try to use the snippet
                  [[getContextSetting? &context=`myContext`, $setting=`site_start`]]


                  It just returns "No Context Set". No matter what I enter there. Using Revo 2.2.9.
                    • 3749
                    • 24,544 Posts
                    Try taking the comma out of the tag and changing $setting to &setting. (my bad).

                      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
                      • 1331
                      • 129 Posts
                      Ahh, that did it. Thanks again