I need to have a template that automatically sets its parent ID to a particular resource (MODX Revolution). I have figured out how to do this with a plugin I created that runs a script with the OnDocFormSave system event:
<?php
// only run this code for the template with the ID of 6
if ($resource->get('template') != 6) {
return;
}
// set parent resource
$parent_resource_id = 81;
$my_parent = $modx->getObject('modResource',$parent_resource_id);
$my_parent_title = $my_parent->get('pagetitle');
$resource->set('parent-cmb', $my_parent_title . ' (' . $parent_resource_id . ')');
The code works but the issue is I have a user who is limited by permissions from creating a resource at the root level. This limitation takes precedence before the plugin runs so they get an error if they do not manually set the Parent Resource (and setting the Parent Resource is a step many clients seem to have trouble remembering).
I know there are system events for when the template is selected, ideally I could figure out a way to set the parent resource at this stage instead of when it is saved. Hoping someone can point me in the right direction.