We launched new forums in March 2019—join us there. In a hurry for help with your website? Get Help Now!
    • 3749
    • 24,544 Posts
    getOption() first checks for a User Setting, then a Context Setting, then a System Setting. I think the problem is that the current context is 'mgr' and there's no context setting there.

    The best solution would probably be @EVAL global $modx; return $modx->runSnippet();
    with a custom snippet to generate the output.

    The resource's context is available with $resource->get('context_key');

    The problem is that AFAIK, you can't use $modx->resource reliably at that point -- only in the front end.

      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
      • 48042
      • 5 Posts
      Ok,I think I know how to do it. Last thing what I need is:
      Any function how to get context_key from documentId? Can't find.
      Thanks a lot.
        • 3749
        • 24,544 Posts
        $doc = $modx->getObject('modResource', $documentId);
        if ($doc) {
            $contextKey = $doc->get('context_key');
        }


        You can leave out the 'if' if you're absolutely sure that the documentId is valid. If it's not, the get() will throw a PHP error.
          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
          • 48042
          • 5 Posts
          SOLVED.

          I needed just something like that to generate a list in the TV when editing a resource.
          @SELECT pagetitle, id FROM modx_site_content WHERE parent=[[++shop]]

          Unfortunately this simple code doesn't work.

          So the solution is:

          TV input option
          @EVAL $documentId = $modx->resource->get('id'); return $modx->runSnippet('getCheckboxes',array('documentId' => $documentId, 'option' => 'shops')); 


          Snippet getCheckboxes
          <?php
          $doc = $modx->getObject('modResource', $documentId);
          if ($doc) {
              $contextKey = $doc->get('context_key');
              $modx->switchContext($contextKey);
              $resId = $modx->getOption($option);
          }
          
          
          $output = $modx->runSnippet (
            "getResources" , array(
               "tpl" => "checkboxTpl", 
               "parents"=> $resId,      
               "limit"=>"80",
               "depth"=>"0",
               "sortby"=>"menuindex",
               "sortdir"=>"ASC"   
               ) 
          );
          return $output;



          Thank you very much Bob and Sottwell!
            • 3749
            • 24,544 Posts
            You might want to do this, especially if the context switch causes any trouble:

            $oldContext = $modx->context->get('key);
            $modx->switchContext($contextKey);
            $resId = $modx->getOption($option);
            $modx->switchContext($oldContext);


            Here's another (maybe better) alternative that would keep you from having to switch contexts at all. This will get the Context Setting directly:

            $contextKey = $doc->get('context_key');
            $context = $modx->getObject('modContext', array('key' => $contextKey));
            $resId = $context->getOption($option);
            


              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