Skip to content

Instantly share code, notes, and snippets.

@rtripault
Created November 25, 2010 09:51
Show Gist options
  • Star 4 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save rtripault/715140 to your computer and use it in GitHub Desktop.
Save rtripault/715140 to your computer and use it in GitHub Desktop.
MODx Revolution snippet to list resources of a given parent in order to output it in Option Values of a TV
<?php
/* build a query to get Resources with a given parent that are published and not deleted */
$query = $modx->newQuery('modResource', array('parent' => 147, 'deleted' => false, 'published' => true));
/* to optimize, you can select only the columns you need */
// $query->select($modx->getSelectColumns('modResource', 'modResource', '', array('id', 'class_key', 'context_key')));
/* get the modResource objects with the given parent */
$children = $modx->getCollection('modResource', $query);
/* loop through the children and push the id onto $resourceIds */
$resourceIds = "";
$i=0;
foreach ($children as $child) {
if ($i!=0) {
$resourceIds .= "||";
}
$resourceIds .= $child->get('pagetitle');
$resourceIds .= "==";
$resourceIds .= $child->get('id');
$i++;
}
return $resourceIds;
@EVAL return $modx->runSnippet('getAside');
Don't forget to set Ouput Type as Delimiter «,» in order to be able to use the ouput in a getResources call
@opengeek
Copy link

FWIW:

$modx->getChildIds(147, 1);

will get the same data without a single query being executed. Much more efficient.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment