The trick is to fire on two events, OnLoadWebPageCache, and OnLoadWebDocument. You need to have your "switch" indicator be passable or global (I use $_REQUEST). Finally, if the cache is fired while the switch is active, simply clear the cache (for that document) and sendForward to the same page. This will negate the cache, and cause a new OnLoadWebDocument to fire with the cleared object.
Basically, the refresh() call is really important. You want it to only apply to that document within that context. This makes the refresh extremely fast. It has to be extremely fast, otherwise you get a "get() called on non-object".
Finally, we clear the cache a second time, in case the switch is off and you want the original page. Otherwise, the cache binds to the "switched to" template permanently. This is not what we want.
This is the code:
//Get the System Event
$eventName = $modx->event->name;
switch($eventName) {
case 'OnLoadWebDocument':
if (!empty($switch)) //<-- Just checking if we HAVE a switch.
{//Refresh the Cache
$modx->getCacheManager()->refresh(array(
'auto_publish' => array('contexts' => array($modx->context->key)),
'context_settings' => array('contexts' => array($modx->context->key)),
'resource' => array('contexts' => array($modx->context->key), 'ids'=> array($modx->resource->get('id'))),
) );
if ($switch == 'true') //<-- Now checking the switch's value.
{//Switch the template
$modx->resource->set('template', $idDegradeTo);
$_REQUEST[$keyDegrade] = 'false';
$switch = 'false'; //<-- Turning switch off making it a run-once.
}
}
break;
case 'OnLoadWebPageCache':
if (!empty($switch)) //<-- Just checking that we HAVE a switch.
{
if (!empty($modx->resource))
{ $modx->getCacheManager()->refresh(array(
'auto_publish' => array('contexts' => array($modx->context->key)),
'context_settings' => array('contexts' => array($modx->context->key)),
'resource' => array('contexts' => array($modx->context->key), 'ids'=> array($modx->resource->get('id'))),
));
// Re-Forward to the Resource
if ($switch == 'true') //<-- Checking the switch's value.
$modx->sendForward($modx->resource->get('id'));
}
}
break;
}