Last active
April 4, 2019 02:07
-
-
Save ninty9notout/00037f7de76ca603170fb4a42d2cf0c0 to your computer and use it in GitHub Desktop.
Remove the preview field from SilverStripe
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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