Thank you for your patience. I never expected achieving this will be this cumbersome.
Some things are not happening. Depending on your sortby (ASC/DESC) the TV values of the first output is repeated across all Projects under the same category. Some TVs are not been displayed at all even though they are not empty, especially ProjectDetail and ProjectDetailTwo.
The Gallery snippet call is not displaying anything.
Here is the complete code:
$tvId = 21; /* change this to the ID of the category TV */
function getDocs($category, $tvId) {
global $modx;
$tvrs = $modx->getCollection('modTemplateVarResource', array(
'tmplvarid' => $tvId,
'value' => $category
));
$docs = array();
foreach ($tvrs as $tvr) {
$docs[] = $tvr->get('contentid');
}
$c = $modx->newQuery('modResource');
$c->where(
array('id:in' => $docs)
);
$c->sortby('menuindex', 'ASC'); /* I changed this from pagetitle */
$resources = $modx->getCollection('modResource', $c);
$page = $modx->getObject('modResource', $c); /* I added to be able to use $page to get all the TVs as required */
$output = '<h2><b>' . $category . '</b></h2>';
foreach ($resources as $resource) {
$Intro = $resource->get('introtext');
if (!empty($Intro)) {/* trying to ensure no paragraph tag output if introtext is empy */
$Intro = '<p><strong>Overview</strong><br /> ' . $resource->get('introtext') . '</p>';
}
/*start formatting output */
$output .= '<div class="sbu-details">
<h2><strong>' . $resource->get('pagetitle') . '</strong></h2>
<ul>
<li><strong>Project:</strong> ' . $page->getTVValue('ProjectTitle') . '</li>
<li><strong>Location: </strong> ' . $page->getTVValue('ProjectLocation') . '</li>
<li><strong>Client:</strong> ' . $page->getTVValue('ProjectClient') . '</li>
<li><strong>Terrain:</strong> ' . $page->getTVValue('ProjectTerrain') . '</li>
</ul>' . $Intro;
$projectDetail = $page->getTVValue('ProjectDetail');
if (!empty($projectDetail)) { /* trying to ensure no paragraph tag output if ProjectDetail is empy */
$output .= '<p>' . $projectDetail . '</p>';
}
$projectDetailTwo = $page->getTVValue('ProjectDetailTwo');
if (!empty($projectDetailTwo)) { /* trying to ensure no paragraph tag output if ProjectDetailTwo is empy */
$output .= '<p>' . $ProjectDetailTwo. '</p>';
}
$output .= '<p class="galleryIMGs">';
$fields = array( /* to hold Gallery snippet call */
$album => $page->getTVValue('ActivityAlbumTitle'),
$toPlaceholder = 'gallery',
$limit = 1,
$tag = 'lead',
$thumbTpl = 'nest_galItemLink',
$linkToImage = 1
);
$fields2 = array( /* to hold Gallery snippet call */
$album => $page->getTVValue('ActivityAlbumTitle'),
$toPlaceholder = 'gallery2',
$tag = 'follow',
$thumbTpl = 'nest_galItemFollowLink',
$linkToImage = 1
);
$output .= $modx->runSnippet('Gallery', $fields); /* to output Gallery snippet call */
$output .= $modx->runSnippet('Gallery', $fields2); /* to output Gallery snippet call */
$output .= '</p></div>';
/*end formatting output */
/* Use this instead if you want links to the resources:
$url = $modx->makeUrl($doc->get('id'), "", "", "full");
$output .= '<li><a href="' . $url . '">' . $doc->get('pagetitle'). '</a></li>';
*/
}
return $output;
}
/* Get all possible categories */
$c = $modx->newQuery('modTemplateVarResource');
$c->distinct();
$c->where(array('tmplvarid' => $tvId));
$tvrs = $modx->getCollection('modTemplateVarResource', $c);
$output = '';
/* Get the resources in each category */
$usedCategories = array();
foreach ($tvrs as $tvr) {
$category = $tvr->get('value');
if (! in_array($category, $usedCategories)) {
$usedCategories[] = $category;
$output .= getDocs($category, $tvId);
}
}
return $output;