Skip to content

Instantly share code, notes, and snippets.

@Flygenring
Last active March 28, 2022 21:47
Show Gist options
  • Save Flygenring/5712119 to your computer and use it in GitHub Desktop.
Save Flygenring/5712119 to your computer and use it in GitHub Desktop.
MODX Snippet to get a field from a resource based on a supplied resource id, meant to be used as an output filter.
<?php
/**
* Gets a field from a resource based on a supplied resource id, meant to be used as an output filter.
*
* Based on a resource ID a field can be retrieved. This can be standard fields as 'pagetitle' or 'alias',
* but a few custom fields are also available; 'siblingNextId', 'childrenFirstId', and 'childrenCount'.
* Only resources that are not marked as 'deleted' or 'hidemenu', and that is marked as 'published' are returned.
* Examples:
* [[*id:toResourceField]] => returns the resource pagetitle
* [[*id:toResourceField=`alias`]] => returns the resource alias
* [[*id:toResourceField=`childrenFirstId`]] => returns the ID of the first child of the resource.
* If the resource has no children, the resource ID is returned
* [[*id:toResourceField=`childrenCount`]] => returns the number of children for the resource
* [[*id:toResourceField=`siblingNextId`]] => returns the ID of the next sibling of the resource.
* If the resource has no 'next' sibling, it returns the first child of the parents next sibling.
* If the parent has no 'next' sibling, it returns the grandparent.
* As snippet:
* [[toResourceField? &input=`[[*id]]` &options=`alias`]]
*
* @author Jacob Flygenring <jacob@bettercollective.com>
* @since 2013-05-27
*
* @param int $input The resource id to get the field from.
* @param string $options The name of the field to get. Defaults to 'pagetitle'.
*
* @return (int|string) The value of the chosen field in the given resource.
*/
if(!function_exists('siblingNextId')) {
function siblingNextId($intResourceId) {
global $modx;
list($intParentId) = $modx->getParentIds($intResourceId, 1);
$objResource = $modx->getObject('modResource', $intResourceId);
$intMenuIndex = $objResource->get('menuindex');
$arrCriteria = array(
'parent' => $intParentId
, 'deleted' => false
, 'hidemenu' => false
, 'published' => true
, 'menuindex' => ($intMenuIndex + 1)
);
$objXpdoQuery = $modx->newQuery('modResource');
$objXpdoQuery->where($arrCriteria);
$objXpdoQuery->limit(1);
$objResource = $modx->getObject('modResource', $objXpdoQuery);
if(null != $objResource) {
$intReturn = $objResource->get('id');
}
else {
$intReturn = 0;
}
return $intReturn;
}
}
if(!function_exists('childrenFirstId')) {
function childrenFirstId($intResourceId) {
global $modx;
$arrCriteria = array(
'parent' => $intResourceId
, 'deleted' => false
, 'hidemenu' => false
, 'published' => true
);
$objXpdoQuery = $modx->newQuery('modResource');
$objXpdoQuery->where($arrCriteria);
$objXpdoQuery->sortby('menuindex', 'ASC');
$objXpdoQuery->limit(1);
$objResource = $modx->getObject('modResource', $objXpdoQuery);
if(null != $objResource) {
$intReturn = $objResource->get('id');
}
else {
$intReturn = $intResourceId;
}
return $intReturn;
}
}
if(!function_exists('childrenCount')) {
function childrenCount($intResourceId) {
global $modx;
$arrCriteria = array(
'parent' => $intResourceId
, 'deleted' => false
, 'hidemenu' => false
, 'published' => true,
);
return $modx->getCount('modResource', $arrCriteria);
}
}
$intResourceId = isset($input) ? (int)$input : 0;
if($intResourceId > 0) {
$strField = (strlen($options) > 0) ? $options : 'pagetitle';
switch($strField) {
case 'siblingNextId':
$intMaxLevels = 2;
$intCurrentLevel = $intMaxLevels;
do {
$mixReturn = siblingNextId($intResourceId);
if(0 == $mixReturn) {
list($intResourceId) = $modx->getParentIds($intResourceId, 1);
$intCurrentLevel -= 1;
}
}
while(0 == $mixReturn && $intCurrentLevel > 0);
if($intCurrentLevel != $intMaxLevels) {
// A next sibling wasn't found, but a parents next sibling was, so just get first child from that parent
if($mixReturn > 0) {
$mixReturn = childrenFirstId($mixReturn);
}
// Neither a next sibling or a parents next sibling was found, so return grand parent
else {
$mixReturn = $intResourceId;
}
}
break;
case 'childrenFirstId':
$mixReturn = childrenFirstId($intResourceId);
break;
case 'childrenCount':
$mixReturn = childrenCount($intResourceId);
break;
default:
$objResource = $modx->getObject('modResource', $intResourceId);
$mixReturn = $objResource->get($strField);
break;
}
return $mixReturn;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment