gorriecoe/silverstripe-preview

Adds a preview options to a Dataobject.

Installs: 204

Dependents: 1

Suggesters: 0

Security: 0

Stars: 1

Watchers: 3

Forks: 0

Open Issues: 0

Type:silverstripe-vendormodule

dev-master 2018-06-19 21:34 UTC

This package is auto-updated.

Last update: 2024-03-29 03:50:48 UTC


README

Adds preview options to a Dataobject or SiteTree. Basically provides more advanced options to summary content.

Installation

Composer is the recommended way of installing SilverStripe modules.

composer require gorriecoe/silverstripe-preview

Requirements

  • silverstripe/cms ^4.0

Suggestion

Maintainers

Usage

Adding preview to a DataObject

MyDataObject:
  extensions:
    - gorriecoe\Preview\Extensions\Previewable

Note that Previewable automatically extends SiteTree

Default preview fields

Adding default fields to a DataObject or SiteTree

SilverStripe\CMS\Model\SiteTree:
  extensions:
    - gorriecoe\Preview\Extensions\PreviewDefaultFields

This will add Image, Title, Content and Label fields to a summary tab.

Defining your own preview data

By default preview data will inherit from the DataObject. For example $MyDataObject.Preview.Image will use $MyDataObject.Image. However if you want the preview image to have fallback options, you can define it in your DataObject with the following static variable define.

class MyDataObject extends DataObject
{
    private static $has_one = [
        'PreviewImage' => Image::class,
        'HeaderImage' => Image::class
    ];

    private static $preview_data = [
        'Image' => [
            'PreviewImage',
            'HeaderImage'
        ]
    ];
}

In the example above $MyDataObject.Preview.Image will return the Image from PreviewImage first, if that doesn't exist then return HeaderImage and finally if that doesn't exist then return null.

Template

Access preview data via template simply change the scope of the data using one of the following methods.

<% loop MyDataObjects %>
    <h2>
        {$Preview.Title}
    </h2>
<% end_loop %>
<% loop MyDataObjects %>
    <% with Preview %>
        <h2>
            {$Title}
        </h2>
    <% end_with %>
<% end_loop %>

Advanced Usage

Check out silverstripe/silverstripe-action code for an example of advanced usage. In it you will see you can prepend additional fallback options from another dataobject.