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.
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.
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.