Skip to content

Instantly share code, notes, and snippets.

@chrisdempsey
Forked from sepiariver/rootResource.php
Created December 19, 2023 12:48
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save chrisdempsey/c9eeb987ed3aedcfb0cacdae157ea42a to your computer and use it in GitHub Desktop.
Save chrisdempsey/c9eeb987ed3aedcfb0cacdae157ea42a 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