Search code examples
phpcontent-management-systemadminpreviewsilverstripe

Silverstripe 3.1 - Disable the preview pane


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.


Solution

  • 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;
    }