Skip to content

Instantly share code, notes, and snippets.

@herooutoftime
Last active March 13, 2016 22:46
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 herooutoftime/6021154 to your computer and use it in GitHub Desktop.
Save herooutoftime/6021154 to your computer and use it in GitHub Desktop.
MODX real preview

THERE IS A NEW PREVIEW IN TOWN: https://gist.github.com/herooutoftime/59eebf7c0bbf1299410c

MODx real preview:

Up to now MODx lacks a real preview (which shows unsaved changes to a resource). This plugin is a workaround and simulates a preview behavior.

Usage:

  • Create a new resource, call it 'Preview'
  • Create a new system setting with key 'previewer.resource_id' and save with above resource ID
  • Create a new plugin with below gist and append to 'OnDocFormRender', 'OnWebPageComplete', 'OnBeforeDocFormSave'
  • When saving a resource there should either a new pop-up / tab come up with the resource changes viewable in frontend

Please use with caution and test before implementing in a live-system

/**
* Plugin: Previewer
* Enables a real preview of unsaved changes by updating a fake resource with the given data
*/
$prev = $modx->getOption('previewer.resource_id');
$event = $modx->event->name;
switch($event) {
case 'OnDocFormRender':
$prevRes = $modx->getObject('modResource', $prev);
$prevArr = $prevRes->toArray();
$prevJso = $modx->toJSON($prevArr);
$prevUrl = $modx->makeUrl($prev, 'web', array(), 'full');
$_POST['id'] = $prev;
$js =<<<JS
<script type="text/javascript">
Ext.onReady(function () {
var task = new Ext.util.DelayedTask(function(){
window.open('$prevUrl');
});
var modab = Ext.getCmp("modx-action-buttons");
modab.add('-');
modab.add({
xtype: 'button'
,text: 'Preview'
,method: 'remote'
,process: 'update'
,listeners: {
click: function(btn) {
Ext.getCmp('modx-panel-resource').getForm().findField("modx-resource-id").setValue($prev);
Ext.getCmp('modx-panel-resource').getForm().findField("modx-resource-alias").setValue('preview');
task.delay(1000);
}
,success: function(data) {
console.log(data);
return false;
}
}
,id: 'modx-abtn-real-preview'
,checkDirty: true
});
modab.doLayout();
});
</script>
JS;
$modx->regClientStartupHTMLBlock($js);
break;
case 'OnWebPageComplete':
$res = $modx->resource;
if($res->get('id') !== $prev)
return;
if($_GET['redirect'] && $_GET['redirect'] == 1)
return;
// $modx->sendRedirect('http://modx.com',array('type' => 'REDIRECT_META'));
$modx->sendRedirect($modx->makeUrl($prev, 'web', array('redirect' => 1), 'full'), array('type' => 'REDIRECT_META'));
break;
case 'OnBeforeDocFormSave':
if($id != $modx->getOption('previewer.resource_id'))
return;
$modx->log(MODX_LOG_LEVEL_ERROR, $id . ' ' . $modx->getOption('previewer.resource_id'));
$resource->set('parent', 0);
$resource->set('alias', 'preview');
$resource->set('uri_override', 1);
$resource->set('uri', 'preview');
$resource->save();
break;
}
return;
@osabate
Copy link

osabate commented Oct 15, 2014

Is this plugin safe to use on Production environments?

@herooutoftime
Copy link
Author

@osabate No. As stated above this should be really tested heavily before used on a production environment.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment