We launched new forums in March 2019—join us there. In a hurry for help with your website? Get Help Now!
    • 33238
    • 388 Posts
    Hello everyone, using pdoResources I need a list of the main page of every context (site_start), any idea how can I get it?

    Every context has this details:


    Name:
    Key:
    Value:
    
    Base URL
    base_url
    /
     
    Culture key
    cultureKey
    en
    
    http Host
    http_host
    myurl.com
     
    Safe URL
    safe_url
    https://myurl.com/
     
    Site start
    site_start
    219
     
    Site URL
    site_url
    https://myurl.com/


    but I need to do it dynamically, not using the &resources=`1, 21`
    that means, new context added, it will be in the list automatically.

    cheers.

    This question has been answered by BobRay. See the first response.

      --
      ysanmiguel.com
    • discuss.answer
      • 3749
      • 24,544 Posts

      I think this would do it:

      [[pdoResources? &parents=`[[getStarts]]` ... ]]



      /* getStarts snippet */
      
      $query = $modx->newQuery('modContext');
      $query->where(array('modContext.key:NOT IN' => array('web', 'mgr')));
      $query->sortby($modx->escape('modContext') . '.' . $modx->escape('key'), 'ASC');
      
      
      $starts = array();
      $key = 'site_start';
      
      $contexts = $modx->getCollection('modContext', $query);
      
      foreach ($contexts as $context) {
      
          $fields = array(
              'context_key' => $context->get('key'),
              'key' => $key,
           );
           $contextSetting = $modx->getObject('modContextSetting', $fields);
      
          if ($contextSetting) {
              $starts[] = $contextSetting->get('value');
          }
      }
      
      return implode(',', $starts);



      This works in CLI mode, but it might run into trouble in the front end with anonymous users because they might not have permission to load, list, and view contexts. If so, you could give them permission or bypass the permissions system by using PDO to get the 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
        • 33238
        • 388 Posts
        Thank you very much Bob. [ed. note: Ysanmiguel last edited this post 6 years, 8 months ago.]
          --
          ysanmiguel.com