0
votes

I'm getting the page with ID 3. This page has 3 subpages. So, on the page with ID 3 I call a snippet to get the subpages like this:

$c = $modx->newQuery('modResource');
$c->leftJoin('modTemplateVarResource', 'TV1', array('TV1.contentid = modResource.id'));
$c->leftJoin('modTemplateVar', 'TV2', 'TV1.id = TV1.tmplvarid AND TV1.`value` = "XXX"');
$c->where(array(
   'parent' => 3,
   'deleted' => false,
   'published' => true,
   'TV1.value' => 'YYY'
));
$c->limit($limit);
$c->prepare();

$output = "";
$resources = $modx->getCollection('modResource', $c);
foreach ($resources as $resource) {
    $resourceArray = $resource->toArray();
    $output .= $modx->getChunk($tpl, $resourceArray);

}

return $output;

So, when I use get chunk I cannot access the Template Variabel with [[*MyVar]]. Did i have to tell the getCollection to grab them? Or did I have to make a new query to grab the Template Vars and give it to the properties in getChunk two?

Greetings

2
Added this in the for each loop $page = $modx->getObject('modResource', $resource->get('id')); $tv = $page->getTVValue('TVXXX'); $resourceArray['TVXXX'] = $tv; Is there a better solution?Hackbard

2 Answers

1
votes

You can`t use [[*MyVar]] in chunk for this case, because of this placeholders are always contains values for current resource.

You must replace placeholders in chunk to [[+MyVar]] and select them from database.

Simple way to do this using package pdoTools:

$pdo = $modx->getService('pdoFetch');
$tpl = '@INLINE <p>[[+pagetitle]]</p> [[+tv.date]] [[+tv.image]]';
$output = '';

$resources = $pdo->getCollection('modResource',
    // Where conditions
    array(
        'published' => 1,
        'deleted' => 0,
    ),
    // Other options
    array(
        'includeTVs' => 'date,image',
        'processTVs' => true,
        'tvPrefix' => 'tv.',
        'limit' => 10,
    )
);

foreach ($resources as $resource) {
    $output .= $pdo->getChunk($tpl, $resource);
}

return $output;

General options of pdoTools classes.

0
votes

What you want to do is to use getCollectionGraph:

http://rtfm.modx.com/xpdo/2.x/getting-started/using-your-xpdo-model/retrieving-objects/getcollectiongraph

There is actually an example in there to get your TVs.

Here is another example showing how to build the criteria:

http://rtfm.modx.com/xpdo/2.x/class-reference/xpdo/xpdo.getcollectiongraph