2
votes

I'm currently developing my own user management module for Silverstripe 3.1. I'm having big problems with several specific page types... but all my issues are caused by the CMS preview pane.

Is there any way to disable this preview pane, either on a page-by-page or site-wide basis?

Here's a more detailed description of my problem to give a little context.

Scenario 1) I have "LogoutPage" type which allows users to add a link to navigation areas. This page type process the logout with a simple call to $memeber->logout() on the init() function, then redirects the user to a location specified by the administrator in the CMS.

My problem is that when the preview is generated in the CMS, init() is called from the preview pane, forcing the user to be logged out and removing the ability to edit the details of the LogoutPage.

Scenario 2) I have also written a one-stop "MemberProfilePage" type which also handles registrations, password reminders and logins when a current user is not found. This relies on code samples like those below to produce alternating Title, MenuTitle and Content variables when there is a user logged in to the system.

Unfortunately the preview pane generates an error as it does not have access to the parent class at this point, which could also be solved by simply disabling the preview pane for this page type.

public function getTitle(){
    if($m = Member::currentUser()){
        return parent::getTitle() ;
    } else {
        return $this->NotLoggedInTitle ;
    }
}

public function getMenuTitle(){
    if($m = Member::currentUser()){
        return parent::getMenuTitle() ;
    } else {
        return $this->NotLoggedInMenuTitle ;
    }
}

public function getContent(){
    if($m = Member::currentUser()){
        return parent::getContent() ;
    } else {
        return $this->NotLoggedInContent ;
    }
}

I've tried for about the last 2 hours to use the advice shown on this forum post but nothing I do seems to work. It's getting to the point where I can't justify spending any more time on this and may have to roll back to 3.0, which is definitely not ideal at this point.

4
in case it's possible for you to manage the pages in a modeladmin, you could use github.com/icecaster/silverstripe-versioned-gridfield (doesn't have a preview pane, but a link to the preview page instead)schellmax
Unfortunately this solution is inadequate for what I am trying to achieve. It's quite important I maintain a familiar working environment for my clients so the model-admin approach is not going to be an option at this point in the project. Thanks for the response though as I'm sure this module will help me in the future.Forkoff.co.uk

4 Answers

8
votes

I created the following Silverstripe extension so that on my pages I could disable the pane with a config value. This is probably the cleanest way to pro grammatically disable it.

config.yml

CMSMain:
  extensions:
    - CMSMainExtension

CMSMainExtension.php

class CMSMainExtension extends Extension {
    public function updateEditForm($form) {
        $classNameField = $form->Fields()->dataFieldByName('ClassName');
        if ($classNameField) {
            $className = $classNameField->Value();
            if ($className && class_exists($className) && $className::config()->hide_preview_panel)
            {
                $form->Fields()->removeByName(array('SilverStripeNavigator'));
                $form->removeExtraClass('cms-previewable');
            }
        }
    }
}

Example use:

class ContentPage extends Page {
    private static $db = array(
    );

    private static $hide_preview_panel = true;
}
2
votes

Try the following:

mysite/javascript/DisablePreview.js

jQuery( function() {
    console.log( 'Disable Preview' );
    jQuery( '.cms-preview' ).entwine( '.ss.preview' ).changeMode( 'content' );
    jQuery( '.cms-preview' ).entwine( '.ss.preview' ).disablePreview();
} );

mysite/_config.php

LeftAndMain::require_javascript('mysite/javascript/DisablePreview.js');

flush the cache.

2
votes

this slightly modified version of LevBs answer also hides the preview-mode selection.
A bit hackish, but does the trick.

jQuery( function() {
    jQuery( '.cms-preview' ).entwine( '.ss.preview' ).changeMode( 'content' );
    jQuery( '.cms-preview' ).entwine( '.ss.preview' ).disablePreview();
    //remove preview switch for SomePageClass
    if( 'SomePageClass' === jQuery('input[name="ClassName"]').val() ){
        jQuery('.preview-mode-selector').remove();
    }
} );
1
votes

The answer from Mx Gherkins and LevB only works on initial load of the CMS and does not take into account once a second page is clicked for editing.

Ideally you would be looking to fire the code on some sort of page change event, but I can't find any useful info on this. Instead I added a stylesheet to always hide the preview button but certainly not ideal. Also means you have to disable for all pages:

mysite/javascript/DisablePreview.js

jQuery( function() {
    jQuery( '.cms-preview' ).entwine( '.ss.preview' ).changeMode( 'content' );
    jQuery( '.cms-preview' ).entwine( '.ss.preview' ).disablePreview();
    jQuery( '.preview-mode-selector' ).remove(); 
} );

mysite/css/DisablePreview.css

.preview-mode-selector {
    visibility: hidden;
}

mysite/_config.php

LeftAndMain::require_javascript('mysite/javascript/DisablePreview.js');
LeftAndMain::require_css('mysite/css/DisablePreview.css');