We launched new forums in March 2019—join us there. In a hurry for help with your website? Get Help Now!
    • 38017
    • 18 Posts
    I was able to implement this wonderful tutorial into a the backend (https://rtfm.modx.com/revolution/2.x/making-sites-with-modx/customizing-content/template-variables/creating-a-multi-select-box-for-related-pages-in-your-template)

    However I was wondering if anyone had experience with pulling in TVs also with this? This outputs perfectly but I can't seem to be able to grab specific tv's related to the document.

    Hitting a wall and any help would be appreciated.

    if (empty($input)) { return ''; }
    $tpl = $modx->getOption('tpl',$scriptProperties,'relatedPagesTpl');
    if ($modx->getChunk($tpl) == '') { return 'Missing Tpl!'; }
    
    $ids = explode(',', $input);
    
    $output = array();
    
    foreach ($ids as $key => $value) {
      $resource = $modx->getObject('modResource',array('published' => 1,'id' => $value)) [b]&& $modx->getObject('modTemplateVar',array('name'=>'tvname1'))[/b];
    
      if ($resource instanceof modResource) {
        $output[] = $modx->getChunk($tpl,$resource->toArray());
      }
    }
    
    return implode('',$output);

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

      • 3749
      • 24,544 Posts
      TVs are complicated. You're getting the TV object itself, but that just contains the default value (in the default_text field). It doesn't contain the value of the TV for any particular resource. That's in a different object: modTemplateVarResource.

      $tvId = 12; /* Id of the TV */
      $tvr = $modx->getObject('modTemplateVarResource', array('contentId' => $resource->get('id'), 'tmplvarid' => $tvId));
      $tvValue = $tvr->get('value');
      


      It's more complicated if any of the TVs are set to their default value, since those TVs will not have a modTemplateVarResource record. So ...

      $tvr = $modx->getObject('modTemplateVarResource', array('contentId' => $resource->get('id'), 'tmplvarid' => $tvId));
      if ($tvr) {
         $tvValue = $tvr->get('value');
      } else {
         $tv = $modx->getObject('modTemplateVar');
         $tvValue = $tv->get('default_text');
      }


      You can get the TV objects attached to a given resource with $resource->getMany('TemplateVars'), and you can get the modTemplateVarResource objects, with $resource->getMany('TemplateVarResources'), but if you know the IDs of the TVs you want, it's almost always easier to use the the method above.

        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
      • discuss.answer
        • 38017
        • 18 Posts
        Thank you for the quick response I have read your website and have done it on different snippets that I input the doc id directly.

        I should have searched this a little different, the answer is in this forum thread: https://forums.modx.com/thread/?thread=70087&page=1

        if (empty($input)) { return 'This article is so unique, that we couldn\'t find anything related to it!'; }
        $tpl = $modx->getOption('tpl',$scriptProperties,'relatedPagesTpl');
        if ($modx->getChunk($tpl) == '') { return 'We found some related pages, but don\'t know how to present it.'; }
        
        $ids = explode(',', $input);
        
        $output = array();
        
        foreach ($ids as $key => $value) {
          $resource = $modx->getObject('modResource',array(
            'published' => 1,
            'id' => $value));
        
          if ($resource instanceof modResource) {
            $ta = $resource->toArray();
            $ta = array_merge($ta,$resource->getTemplateVars());
            $output[] = $modx->getChunk($tpl,$ta);
        
          }
        }
        
        return implode('',$output);
        
        [ed. note: rcarnrick last edited this post 7 years, 9 months ago.]
          • 3749
          • 24,544 Posts
          That's a good solution. smiley

          I forgot about getTemplateVars(). I think it's relatively new.
            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