We launched new forums in March 2019—join us there. In a hurry for help with your website? Get Help Now!
    • 26610
    • 25 Posts
    Hi

    I'd like to have a TV (Multi-Checkbox) where I can reference on listed Resources.

    I'm working with several Contexts and the same Templates should be used on all Contexts.
    That means, in the Context-Settings I'd like to Reference the "Main Page", of which Sub-Pages should be listed to choose from.

    As Input Options of my TV "downloads_references":
    @EVAL return $modx->runSnippet('listResourcesContext',array('setting_key' => 'site_references'));


    Snippet "listResourcesContext"
    <?php
    $contextKey = $modx->getOption('setting_key', $scriptProperties, 'default');
    
    $document = $modx->getObject('modResource',$modx->resource->get("id"));
    $key = $document->get('context_key');
    $ctx = $modx->getContext($key);
    
    $parent = $modx->getOption('parent',$scriptProperties,$ctx->getOption($contextKey));
    $parentObj = $modx->getObject('modResource',$parent);
    if (!($parentObj instanceof modResource)) { return ''; }
    $resArray = $parentObj->getMany('Children');
    $resources = array();
    foreach($resArray as $res) {
      if ($res instanceof modResource) {
        $resources[] = $res->get('pagetitle') . '==' . $res->get('id');
      }
    }
    $out = implode("||",$resources);
    return $out;



    When the Resource was already saved, it is possible to choose from the resource list.

    But if I want to Create a New Resource, I get a 500 error....
    Can anybody help me? Maybe there's something wrong with the Snippet?

    Thank you very much in advance
    seliin

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

    [ed. note: seliin last edited this post 7 years ago.]
      • 26610
      • 25 Posts
      I need some help please... smiley
      • discuss.answer
        • 42562
        • 1,145 Posts
        donshakespeare Reply #3, 7 years ago
        Check first if the resource in question, that is, currently worked on, is indeed a resource.
        New resources are not resources, they are wanna-bes. They become resources only after the first save.

        Your errors start when you try to "get" non-existent fields values from a wanna-be "resource" that does not have an id or anything at all.

        <?php
        if(is_object($modx->resource)){ //<--check first if this is a resource before we start getting and giving anything
          $document = $modx->getObject('modResource',$modx->resource->get("id"));
        
          $contextKey = $modx->getOption('setting_key', $scriptProperties, 'default');
           
          $key = $document->get('context_key');
          $ctx = $modx->getContext($key);
           
          $parent = $modx->getOption('parent',$scriptProperties,$ctx->getOption($contextKey));
          $parentObj = $modx->getObject('modResource',$parent);
          if (!($parentObj instanceof modResource)) { return ''; }
          $resArray = $parentObj->getMany('Children');
          $resources = array();
          foreach($resArray as $res) {
            if ($res instanceof modResource) {
              $resources[] = $res->get('pagetitle') . '==' . $res->get('id');
            }
          }
          $out = implode("||",$resources);
          return $out;
        }

          TinymceWrapper: Complete back/frontend content solution.
          Harden your MODX site by passwording your three main folders: core, manager, connectors and renaming your assets (thank me later!)
          5 ways to sniff / hack your own sites; even with renamed/hidden folders, burst them all up, to see how secure you are not.
          • 26610
          • 25 Posts
          Thank you so much!!