Skip to content

Instantly share code, notes, and snippets.

@eonarik
Last active June 15, 2018 17:05
Show Gist options
  • Save eonarik/7d19043e75ca9fd104a3fdfffa41d040 to your computer and use it in GitHub Desktop.
Save eonarik/7d19043e75ca9fd104a3fdfffa41d040 to your computer and use it in GitHub Desktop.
modx snippet out tvs in category
<?php
// v.1.0.2
// вывод тв полей из определенного раздела
$category = $modx->getOption('category', $scriptProperties, null);
$resourceId = $modx->getOption('resourceId', $scriptProperties, $modx->resource->id);
$limit = $modx->getOption('limit', $scriptProperties, 0);
$tvs = $modx->getOption('tvs', $scriptProperties, null);
$tpl = $modx->getOption('tpl', $scriptProperties, '');
$tpl = str_replace(array('{{', '}}', '^'), array('[[', ']]', '`'), $tpl);
$out = array();
$where = array();
$q = $modx->newQuery('modTemplateVarResource');
$alias = $q->getAlias();
$q->innerJoin('modTemplateVar', 'tv', "tv.id = {$alias}.tmplvarid");
$q->select(array(
"{$alias}.id",
"{$alias}.value as value",
"tv.name as name",
"tv.caption as caption",
));
if (!empty($resourceId))
{
$where[] = "({$alias}.contentid = {$resourceId})";
}
if (!empty($category))
{
$where[] = "(tv.category = {$category})";
}
if (!empty($tvs))
{
$where[] = "(tv.id IN ({$tvs}))";
}
$q->where($where);
if ($limit > 0)
{
$q->limit($limit);
}
$chunk = null;
if (preg_match('/^(@INLINE|@CODE)/i', $tpl))
{
$tpl = preg_replace('/^(@INLINE|@CODE)/i', '', $tpl);
$chunk = $modx->newObject('modChunk');
$chunk->setCacheable(false);
$chunk->set('snippet', $tpl);
}
else
{
$chunk = $modx->getObject('modChunk', array('name' => $tpl));
}
$i = 0;
foreach ($modx->getIterator($alias, $q) as $object)
{
$arr = $object->toArray();
$arr['idx'] = $i;
$out[] = $chunk->process($arr);
$i++;
}
return implode("\n", $out);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment