By chance I tried to accomplish exactly the same thing yesterday evening. And like Nomark I tried everything that’s possible and some impossible things, too, to get this working. getResources, Wayfinder and even things like $modx->getChildIds() etc. failed (no result) although they worked perfectly in a Resource or a Template.
Finally I unpacked the primitive tools and did it like that:
First, I created a Snippet I called SiteStructureForTv. I looks like this:
<?php
/**
* This Snippet is called by the Manager (TV definition)
* and can't be moved to a file. Also, Snippets like
* getResources or Wayfinder aren't able to do the work here.
* The same applies to $modx->getChildIds() etc. - it just
* remains empty...
*/
function getTvSiteStructure($dbh, $parentId = 0, $level = 1, $maxLevel = 2)
{
global $table_prefix;
if ($level > $maxLevel) {
return '';
}
$indent = '--';
$thisLevelIndent = (1 == $level) ? '' : str_repeat($indent, $level - 1).' ';
$sql = "SELECT
`id`,
`menutitle`
FROM
`".$table_prefix."site_content`
WHERE
`parent` = :parent
AND `published` = :published
AND `hidemenu` = :hidemenu
AND `deleted` = :deleted
ORDER BY
`menuindex`";
$stmt = $dbh->prepare($sql);
$params = array(
':parent' => $parentId,
':published' => 1,
':hidemenu' => 0,
':deleted' => 0
);
if ($stmt->execute($params)) {
$menuItems = $stmt->fetchAll();
}
$tvSiteStructure = '';
foreach ($menuItems as $menuItem) {
$tvSiteStructure .= $thisLevelIndent;
// Although the charset is used when connecting to the database,
// utf8_encode() is necessary here (?)
$tvSiteStructure .= utf8_encode($menuItem['menutitle']).'=='.$menuItem['id'].'||';
$tvSiteStructure .= ($level == $maxLevel) ? '' : getTvSiteStructure($dbh, $menuItem['id'], $level + 1, $maxLevel);
}
return $tvSiteStructure;
}
global $database_type,
$database_server,
$dbase,
$database_user,
$database_password,
$database_connection_charset;
$dsn = $database_type.':host='.$database_server
.';dbname='.trim($dbase, '`')
.';charset='.$database_connection_charset;
try {
$dbh = new PDO($dsn, $database_user, $database_password);
} catch (PDOException $e) {
echo 'Connection failed: '.$e->getMessage();
}
$tvSiteStructure = getTvSiteStructure($dbh, 0, 1, 5);
// Remove pipe symbols at the end of the string
$tvSiteStructure = trim($tvSiteStructure, '|');
return $tvSiteStructure;
It’s not even possible to include this from a file; you have to enter this in MODx directly!
In my Template Variable AssociatedPages I chose "checkbox" as input type; the input option values field looks like this:
@EVAL return $modx->runSnippet('SiteStructureForTv');
Doing this it works for me - finally!
I’m sure this can be solved more elegantly (e. g. via xPDO), but for now I have lost enough time with it; maybe I’ll look at this later. If someone finds a better solution, please feel free to post it!
Cheers,
Jan