We launched new forums in March 2019—join us there. In a hurry for help with your website? Get Help Now!
    • 42562
    • 1,145 Posts
    So you read this
    http://tracker.modx.com/issues/1852
    and this
    https://forums.modx.com/thread/77178/check-to-see-if-resource-id-exists-as-a-template-variable-value-in-snippet-call
    and even this
    http://bobsguides.com/blog.html/2014/04/09/get-tv-values-the-fast-way/
    And you are still scratching your head?
    Yea, me too... I scratched my beard bald!


    1. I have a plugin that must fire only if a resource has a template to which is attached a particular TV
    2. This resource will have its own value in the TV, filled or empty
    3. This TV must have a default_value
    4. I have the TV name and resource ID that's all

    A Perfectly Working Example
    if ($modxEventName == 'OnDocFormPrerender') {
      $tvName = $modx->getObject('modTemplateVar', array('name' =>'tvNAME')); // you could wrap this with an IF before you continue
      $tvId = $tvName->get('id');
      $tvValue   = $resource->getTVValue($tvId);
      $tvTemplateId = $modx->getObject('modTemplateVarTemplate', array("tmplvarid" => $tvId))->get("templateid");
      $resourceTemplateId = $resource->get("template");
    
      if ($tvValue  || $tvValue == ''){
        if ($tvTemplateId == $resourceTemplateId) {
              //do stuff
        }
      }
    }


    Cheers,
    what's in a name? [ed. note: donshakespeare last edited this post 8 years, 2 months ago.]
      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.
      • 4172
      • 5,888 Posts
      one joined query, could also work, something like that(untested):

      <?php
      
      $tvname = 'TVname';
      $c = $modx->newQuery('modTemplateVar');
      $c->leftjoin('modTemplate','TemplateVarTemplates');
      $c->leftjoin('modTemplateVarResource','TemplateVarResources');
      $c->select($c->xpdo->getSelectColumns('modTemplateVar','modTemplateVar', ''));
      $c->select($c->xpdo->getSelectColumns('modTemplateVarResource','TemplateVarResources', 'tvvalue_'));
      $c->where(array(
          'TemplateVarResources.contentid'=>$resource->get('id'),
          'modTemplateVar.name'=>$tvname,
          'TemplateVarTemplates'=>$resource->get('template')
          ));
      if ($tv = $modx->getObject('modTemplateVar',$c)){
          $value = $tv->get('tvvalue_value');
          $default = $tv->get('default_text');
          //do stuff
      }    
      
        -------------------------------

        you can buy me a beer, if you like MIGX

        http://webcmsolutions.de/migx.html

        Thanks!