Forked from sepiariver/getResourceProps.snippet.php
Created
December 21, 2017 12:13
-
-
Save Burick/c61ed8da4a5a5cd2d980998ffdb6daf5 to your computer and use it in GitHub Desktop.
Gets Resource Properties and sets placeholders
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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