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

    In a revo plugin, I would like to create a template variable in each existing template. I know how to create the TV in a specific template, but I don’t know how to get an array of all templates id’s. Any example?

    Thanks in advance,
    S.
      • 3749
      • 24,544 Posts
      I think this is what you want:

      <?php
      
      $modx->getObject('modTemplateVar', array('name'=>'MyTv'));  // if the tv exists already
      
      // If not:
      //$fields = array('name'=>'MyTv',
      //    'type'=>'text',
      //    'default_text=>'Some Text',
      //    'caption'='My TV',
      //    'description'='This is my TV'
      // );
      //$tv = $modx->newObject('modTemplateVar', $fields); 
      //$tv->save();
      
      $templates = $modx->getCollection('modTemplate');
      
      foreach($templates as $template) {
          $intersect = $modx->newObject('modTemplateVarTemplate');
          $intersect->addOne($tv);
          $intersect->addOne($template);
          $intersect->save();
      }


      Note that this doesn’t set the TVs value for any resource. For that you need:

      $tv->setValue($id,'value');  // where $id is the ID of a resource


      $ids = $modx->getChildIds('0');  // will get an array containing all resources IDs.
        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
        • 25910
        • 26 Posts
        Mmh,

        $modx->getCollection seems to be my new friend...

        thanks a lot for the example, I think it’s exactly what I want.

        Best regards,
        S.