Skip to content

Instantly share code, notes, and snippets.

@ninty9notout
Last active April 4, 2019 02:07
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ninty9notout/00037f7de76ca603170fb4a42d2cf0c0 to your computer and use it in GitHub Desktop.
Save ninty9notout/00037f7de76ca603170fb4a42d2cf0c0 to your computer and use it in GitHub Desktop.
Remove the preview field from SilverStripe
<?php
use SilverStripe\Core\Extension;
use SilverStripe\Forms\Form;
class CMSMainExtension extends Extension
{
/**
* Removes the CMS preview by removing the literal field which
* renders the page. This needs to be attached to `CMSMain`:
*
* ```
* SilverStripe\CMS\Controllers\CMSMain:
* extensions:
* - CMSMainExtension
* ```
*
* For it to work, the `hide_cms_preview` static has to be set.
* Either add it to non-editable classes in .yml by:
*
* ```
* SilverStripe\CMS\Model\RedirectorPage:
* hide_cms_preview: true
* ```
*
* Or to your own classes:
*
* ```
* private static $hide_cms_preview = true;
* ```
*
* Don't forget to run `flush=all`
*
* @param Form &$form
*/
public function updateEditForm(Form &$form)
{
$classNameField = $form->Fields()->dataFieldByName('ClassName');
if (!$classNameField) {
return false;
}
$className = $classNameField->Value();
if (!$className || !class_exists($className)) {
return false;
}
if (!$className::config()->uninherited('hide_cms_preview')) {
return false;
}
$form->Fields()->removeByName('SilverStripeNavigator');
$form->removeExtraClass('cms-previewable');
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment