We launched new forums in March 2019—join us there. In a hurry for help with your website? Get Help Now!
    • 30023
    • 172 Posts
    Thanks Bob.

    I have the following, in part worked out from your post, in part from your website, in part from the API docs, and in part from var_dump’ing to work out how to get the tv name:

    $document = $modx->getObject('modResource',array('published' => 1,'id' => $docid));
    $tvs = $document->getMany('TemplateVars');
    foreach($tvs as $tv) echo 'Output: '.$tv->renderOutput($document->get('id')).' Name: '.$tv->_fields['name'];
    


    Which gives me the output of the TV and its name, which could lead on to a way of simulating evo’s getTemplateVarOutput. I suspect my way of getting the name could be somewhat improved though.

    More importantly, it only yields the custom TVs. Nothing in modx_site_content is returned. Anyone any ideas?

    --Tim.
      • 3749
      • 24,544 Posts
      I think the first line could be reduced to:

      $document = $modx->getObject('modResource',$docid);


      I’m not sure I understand your other issue. The fields in the site_content table are not actually TVs (though they’re often erroneously referred to that way). They are resource fields and are all available with:

      $resource->get('fieldName');
        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
        • 30023
        • 172 Posts
        Thanks Bob.

        I think I’ll probably write a modx->getTemplateVarOutput that emulates the old one. I used it alot on Evolution. To be honest, its not filling me with confidence how so much has been removed from the API. I hope none of the developers take offence at this - and I realise that saying its been ’removed’ isn’t strictly true given its a new code base - but first impressions are not filling me with enthusiasm or confidence. Still, they are first impressions so I do intend to carry on trying it out a bit longer on a hobby site, though not commercially as yet.

        I take your point about what I term ’standard TVs’ and ’custom TVs’ (you did understand me correctly, and thanks!). I’m just using terminology I’ve seen elsewhere yep, on referencing the offical evo documentation, I can see I’m using it incorrectly.

        For my purposes here though, the ’resource fields’ and ’tvs’ have the same status (which is unsurprising really - your client when editing a document doesn’t see the difference after all). This is why the functionality offered in getTemplateVarOutput (and getTemplateVar(s) for that matter) was good. Whilst the latter functions also returned extra fields for tvs, they all would return both resource field and tv names and values, thus simplifying code.

        thanks again,
        -- Tim.

        PS. Your website documentation is also very good and appreciated.

          • 30023
          • 172 Posts
          I have the following now working, though not widely tested:

          // getTemplateVarOutput partial emulation
          // Returns ALL tvs and resource fields as name/value pairs
          function getTemplateVarOutput($docid, $published)
          	{
          	global $modx;
          	$tv_outputs = array();
          	$document = $modx->getObject('modResource',array('published' => $published,'id' => $docid));
          	$document_tvs = $document->getMany('TemplateVars');
          	foreach($document_tvs as $document_tv) $tv_outputs[$document_tv->get('name')] = $document_tv->renderOutput($document->get('id'));
          	$fields_to_get = array ('id', 'pagetitle', 'isfolder', 'longtitle', 'description', 'alias', 'published', 'introtext', 'content', 'template', 'menuindex', 'searchable', 'cacheable', 'createdby', 'editedby', 'deleted', 'deletedby', 'publishedby', 'createdon', 'publishedon', 'published', 'editedon', 'hidemenu');
          	foreach ($fields_to_get as $field_to_get) $tv_outputs[$field_to_get] = $document->get($field_to_get);
          	return $tv_outputs;
          	}
          


          Its not a full emulation of the old function - it just does what I need it to at the moment.

          I’d prefer a way of getting all resource fields without having to hardcode their names - the only alternative I know of at the moment would be an SQL call - but that appears to suffice for my current requirements. My current lack of knowledge of xPDO prevents me from coding anything better right now.

          -- Tim.
            • 3749
            • 24,544 Posts
            This is off the top of my head and untested, but here’s a shot at doing it in a much more efficient way (and with way less code):

            $resource = $modx->getObjectGraph('modResource', array('TemplateVars' => array()), array('id' => $docid, 'published' => $published));
            return array_merge($resource->toArray(), $resource->TemplateVars->toArray());


            I’m not sure if that will give you the processed TVs or their raw values, but it shows the power of xPDO.
              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
              • 37747
              • 50 Posts
              Hi,
              I know this is a really old thread but I have a very similar problem and I am pretty lost when it comes to the getTemplateVarOutput equivalent in revo as well.

              For my multi-language sites I have a parent document per language that houses some translations and config IDs, in Evo I used a snippet that called ultimateParent, went up the tree to the parent doc, got all the TVs put them in an array and made them accesible using a placeholder. That way if I wanted to call something like "Book now" for a button and have it work in all languages I just added
              [+translate.bookNow+]
              into my template/chunk where the name of the TV is "bookNow" and it has been translated in the language parent file.

              This is the snippet I used before:

              $parent = $modx->runSnippet("UltimateParent");
              $configTVs = $modx->getTemplateVarOutput('*', $id=$parent, $published=1);
              foreach($configTVs as $key => $value) {
              $modx->setPlaceholder('translate.'.$key, $value);
              }
              


              and it works great, now in Revo I am trying to get it to work and this is what I have so far:

              function getConfigTvs($docid, $published) {
              	global $modx;
              	$parent = $modx->runSnippet('UltimateParent', array('topLevel' => '1'));
              	$tvs = array();
              	$resource = $modx->getObject('modResource',array('published' => $published,'id' => $parent));
              	$tvs = $resource->getMany('TemplateVars');
              	foreach($tvs as $tv) {
              		$key = $tv->getValue($id);
                      $value = $tv->renderOutput($id);
              		$modx->setPlaceholder('translate.'.$key, $value);
              	}
              }
              


              and trying to call it in the template or chunks like this:
              [[+translate.bookNow]]
              but no result, no error or blank page either, just no output. I am pretty terrible at PHP and quite new to Revo so go easy on me please smiley
                • 3749
                • 24,544 Posts
                tv->getValue() and tv->renderOutput() both get the TV's value (the first gets the raw value, the second one processes the TV).

                I'm not sure, but I think you want this:

                $key = $tv->get('name');
                $value = $tv->renderOutput($id);  // getValue() would be faster here if you can use the raw value
                  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
                  • 37747
                  • 50 Posts
                  Hi Bob,
                  Thanks, that was a great help, as was the page about objects on your website. This is working great now and here's my final code in case it's useful for anybody. With lots of comments because it sometimes there's a huge gap between my modx builds and I really need to remember what was going on:

                  <?php
                  
                  /*
                   * Call the snippet at the top of your template
                   * Get the translated TV like: [[+translate.bookNow]]
                   * where the tv name is "bookNow"
                   * if the language config file is not the ultimate parent then
                   * you could configure it like this:
                   * $parent = $modx->runSnippet('UltimateParent', array('topLevel' => '3'));
                  */
                  
                  function getConfigTvs($parent) {
                  
                  	global $modx;
                  	$tvs = array();
                  
                  	// don't use the current resource, use the one with id: $parent
                  	$resource = $modx->getObject('modResource', $parent);
                  
                  	// save all the TVs in an array
                  	$tvs = $resource->getMany('TemplateVars');
                  
                  	// get name/value pairs, save to placeholder
                  	foreach($tvs as $tv) {
                  		$key = $tv->get('name');
                          $value = $tv->getValue($id);
                  		$modx->setPlaceholder('translate.'.$key, $value);
                  	}
                  
                  }
                  
                  // define the parent id, in this case the ultimate parent
                  $parent = $modx->runSnippet('UltimateParent');
                  
                  getConfigTvs($parent);
                  


                  I hope somebody finds that useful, for me it's a great way of building multi-language sites where the content/structure may differ from language to language and you want an editor to be able to set up entire new languages. Thanks again Bob, your website and help on the forums is a big part of what makes modx so great.

                  Cheers,
                  Dan
                    • 3749
                    • 24,544 Posts
                    I'm glad it worked for you. Thanks for the kind words.
                      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