Skip to content

Instantly share code, notes, and snippets.

@jrotering
Created March 26, 2013 20:21
Show Gist options
  • Save jrotering/5248862 to your computer and use it in GitHub Desktop.
Save jrotering/5248862 to your computer and use it in GitHub Desktop.
Simple snippet that returns the name (or any other field you specify) of any MODX object. Example: [[getObjectName?&obj=`modTemplate`&id=`[[*template]]`]]
<?php
$fn = (isset($field)) ? $field : ($obj == 'modTemplate') ? 'templatename' : 'name';
if ($object = $modx->getObject($obj, $id)) {
if ($value = $object->get($fn)) {
return $value;
}
else {
$modx->log(modX::LOG_LEVEL_ERROR, 'getObjectName - could not find field "' . $fn . '" on object #' . $id . ' of type "' . $obj . '"');
}
}
else {
$modx->log(modX::LOG_LEVEL_ERROR, 'getObjectName - could not retrieve object #' . $id . ' of type "' . $obj . '"');
}
return '';
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment