Craig McCoy

Programmer / Developer & Zombie Survivalist

MODX Plugin - Limit Depth In Document Tree

Sep/2010 08

So you want to limit how deep your users can nest documents because it breaks stuff like menus and makes the site look bad?

Well lets do it!

First, you will need to snag this modx core update for revolution that the modx team made for me:

Issue: http://bugs.modx.com/browse/MODX-2323
Fix: http://github.com/modxcms/revolution/commit/afee16512e118da18747f91b95adba5d281208fd

Next, create a new plugin, and bind it to the event "onBeforeDocFormSave" which fires off.. well.. before the doc form gets saved.

The code for the plugin is below:

get('id');  
//switch contect
$modx->switchContext($resource->get('context_key'));
//find out how deep we currently are
$depth = count($modx->getParentIds($id));

$modx->switchContext('mgr');

if($depth > howdeep) {
	$modx->event->output($error_msg);
}
?>

Feel free to use this any way you want. If you have any questions, hit me up on twitter @captcodemonkey