Skip to content

Instantly share code, notes, and snippets.

@sepiariver
Last active December 21, 2017 12:13
Show Gist options
  • Save sepiariver/0e0c3928f7d3ba6bd27a to your computer and use it in GitHub Desktop.
Save sepiariver/0e0c3928f7d3ba6bd27a to your computer and use it in GitHub Desktop.
Gets Resource Properties and sets placeholders
<?php
/**
* getter function for resource properties. sets placeholders with all values.
* optionally allows direct return of one element within the namespaced properties sub-array
* @author @sepiariver
*
**/
// OPTIONS
$id = (int) $modx->getOption('id', $scriptProperties, 0);
$res = ($id) ? $modx->getObject('modResource', abs($id)) : $modx->resource;
if (!($res instanceof modResource)) {
$modx->log(modX::LOG_LEVEL_ERROR, 'getResourceProps snippet could not load resource object on line: ' . __LINE__);
return;
}
// set default namespace for legacy content from modx.com import
$namespace = $modx->getOption('namespace', $scriptProperties, 'customprops');
// specifying a key will make the snippet return directly a single value
$key = $modx->getOption('key', $scriptProperties, '');
// for single props only
$toPlaceholder = $modx->getOption('toPlaceholder', $scriptProperties, '');
// default prefix for placeholders
$prefix = $modx->getOption('prefix', $scriptProperties, 'customprops');
// get properties
$props = array();
$props[$prefix] = $res->getProperties($namespace);
if (!$props || !is_array($props)) {
$modx->log(modX::LOG_LEVEL_ERROR, 'getResourceProps snippet could not get an array from properties on line: ' . __LINE__);
return;
}
// RETURN STUFF
if (empty($key)) {
$modx->toPlaceholders($props);
return;
} else {
if (empty($toPlaceholder)) return $props[$prefix][$key];
$modx->setPlaceholder($toPlaceholder, $props[$prefix][$key]);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment