Skip to content

Instantly share code, notes, and snippets.

@sepiariver
Last active December 19, 2023 12:48
Show Gist options
  • Save sepiariver/7bfb20d8dd915be230b1 to your computer and use it in GitHub Desktop.
Save sepiariver/7bfb20d8dd915be230b1 to your computer and use it in GitHub Desktop.
Get the ID of the "Root Resource", aka "Ultimate Parent", with slightly different feature set.
<?php
/*
* @author @sepiariver
*
* GPL license, no warranties, no liability, etc.
*
* USAGE EXAMPLE:
* [[rootResource? &toPlaceholder=`root_resource`]]
* //followed by something like
* [[getResources? &parents=`[[+root_resource]]` ... ]]
*
*/
$id = $modx->getOption('id', $scriptProperties, $modx->resource->get('id'));
$toPlaceholder = $modx->getOption('toPlaceholder', $scriptProperties, '');
$pids = array_reverse($modx->getParentIds($id));
$output = (isset($pids[1]) && !empty($pids[1])) ? $pids[1] : $id;
if (empty($toPlaceholder)) return $output;
$modx->setPlaceholder($toPlaceholder, $output);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment