Skip to content

Instantly share code, notes, and snippets.

@digibutt
Created October 21, 2011 02:45
Show Gist options
  • Save digibutt/1302987 to your computer and use it in GitHub Desktop.
Save digibutt/1302987 to your computer and use it in GitHub Desktop.
getResourceAsPlaceholder
<?php
/*
Call it like this:
[[getResourceAsPlaceholder?
&id=`10`
&prefix=`doc.`
&separator=`.`
&merge=`1`
&longtitle=`lorem ipsum`
]]
Merge will set longtitle to lorem ipsum if the resource field is empty.
*/
if(!$resource = $modx->getObject('modResource', $id)){
return;
}
$prefix = $modx->getOption('prefix', $scriptProperties, '');
$separator = $modx->getOption('separator', $scriptProperties, '');
$tvs = $resource->getMany('TemplateVars');
$resourceArray = array();
foreach($tvs as $tvId => $tv){
$resourceArray[$tv->get('name')] = $tv->renderOutput($resource->get('id'));
}
$resourceArray = array_merge($resourceArray, $resource->toArray());
if($merge == '1'){
foreach($resourceArray as $key => $val){
$resourceArray[$key] = ($scriptProperties[$key] == '') ? $val: $scriptProperties[$key];
}
}
$modx->toPlaceholders($resourceArray, $prefix, $separator);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment