An open API service indexing awesome lists of open source software.

https://github.com/arillo/silverstripe-dataobject-preview

Preview your dataobjects like pages in the SilverStripe CMS.
https://github.com/arillo/silverstripe-dataobject-preview

Last synced: about 1 year ago
JSON representation

Preview your dataobjects like pages in the SilverStripe CMS.

Awesome Lists containing this project

README

          

# Dataobject preview

[![Latest Stable Version](https://poser.pugx.org/arillo/silverstripe-dataobject-preview/v/stable?format=flat)](https://packagist.org/packages/arillo/silverstripe-dataobject-preview)
[![Total Downloads](https://poser.pugx.org/arillo/silverstripe-dataobject-preview/downloads?format=flat)](https://packagist.org/packages/arillo/silverstripe-dataobject-preview)

Shows a preview of your dataobjects like the one you get for pages. Works for GridField and ModelAdmin. Works only for Versioned DataObjects.

For the preview to work you need to implement the CMSPreviewable interface on your DataObject and declare the methods getMimeType, CMSEditLink and PreviewLink($action = null).

You also will need to declare the stages this DataObject should show in the preview pane by setting the appropiate static variables to true.

PreviewLink is the only link we are interested for the preview to work. The DataObjectPreviewController will listen for this links to render your MyDataObject with the MyDataObject.ss template in your theme/templates/\* folder.

Since activating this feature is a bit hacky, we need to also define a custom template for our CustomModelAdmin.

## Requirements

SilverStripe CMS ^4.0

For a SilverStripe 3.x compatible version of this module, please see the [1 branch, or 1.x release line](https://github.com/arillo/silverstripe-arbitrarysettings/tree/1.0).

## Installation

composer require arillo/silverstripe-dataobject-preview:2.0.*

## Example

```php
ClassName), $this->ID);
}

public function getMimeType()
{
return 'text/html';
}

public function CMSEditLink(){
...
}
...
}
```

If our CustomModelAdmin looks like this:

```php



<%t
SilverStripe\CMS\Controllers\CMSPageHistoryController.PREVIEW
'Website preview' %>










```

## Usage

By default, the dataobject preview will look for templates with the dataobject classname directly in the templates folder. So for the example above it will look for themes/yourtheme/templates/Arillo/DataObjectPreview/Models/MyDataObject.ss.
If you would like to customise this behaviour you can do so by implementing your own renderPreview method on the DataObject.

```php
namespace Arillo\DataObjectPreview\Models;
class MyDataObject extends DataObject implements CMSPreviewable
{
...
public function renderPreview()
{
// this will look for themes/yourtheme/templates/Arillo/DataObjectPreview/Models/MyDataObject.ss
return $this->renderWith(MyDataObject::class);
}
}
```

You can overwrite the main template by placing it either in themes/yourtheme/templates/PreviewDataObject.ss or mysite/PreviewDataObject.ss.

- PreviewDataObject.ss -> Container for MyDataObject preview (Like the main Page.ss)

Since SilverStripe 4.11 supports better previews for DataObject, you might want to disable legacy code injection by this module. It can be turned off by:

```
SilverStripe\DataObjectPreview\Extensions\PreviewGridFieldDetailFormExtension:
inject_legacy_code: false
```

Tip: If you are using [silverstripe-gridfield-betterbuttons](https://github.com/unclecheese/silverstripe-gridfield-betterbuttons) you can disable the dataobject preview links since they are no longer needed. Just add this to your config.yml.

```
BetterButtonsActions:
edit:
BetterButtonFrontendLinksAction: false
versioned_edit:
BetterButtonFrontendLinksAction: false
```

## Changelog

V 2.0.2

- added modeladmin support

V 2.0.0

- renamed method previewRender to renderPreview