Skip to content

Commit

Permalink
Merge pull request #12 from arillo/master
Browse files Browse the repository at this point in the history
Merge
  • Loading branch information
bumbus authored Jan 25, 2024
2 parents a20c57a + de70f46 commit 885eed3
Show file tree
Hide file tree
Showing 3 changed files with 115 additions and 49 deletions.
60 changes: 42 additions & 18 deletions Readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@

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).
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.

Expand Down Expand Up @@ -78,24 +78,38 @@ class CustomModelAdmin extends ModelAdmin {
}
```

we need to create the corresponding template in mysite/templates/Arillo/DataObjectPreview/Admins/CustomModelAdmin.ss with this content copied from version 4.7.0 of the silverstripe/cms module. (Beware that this can vary depending on the version and may be changed over time.)
We need to create the corresponding template in mysite/templates/Arillo/DataObjectPreview/Admins/Includes/CustomModelAdmin_PreviewPanel.ss with this content copied from version 4.7.0 of the silverstripe/cms module. (Beware that this can vary depending on the version and may be changed over time.)

```html
<div class="cms-preview fill-height flexbox-area-grow" data-layout-type="border">
<div class="panel flexbox-area-grow fill-height">
<div class="preview-note"><span><!-- --></span><%t SilverStripe\CMS\Controllers\CMSPageHistoryController.PREVIEW 'Website preview' %></div>
<div class="preview__device">
<div class="preview-device-outer">
<div class="preview-device-inner">
<iframe src="about:blank" class="center" name="cms-preview-iframe"></iframe>
</div>
</div>
</div>
</div>
<div class="toolbar toolbar--south cms-content-controls cms-preview-controls"></div>
<div class="cms-preview-overlay ui-widget-overlay-light"></div>
<div
class="cms-preview fill-height flexbox-area-grow"
data-layout-type="border"
>
<div class="panel flexbox-area-grow fill-height">
<div class="preview-note">
<span><!-- --></span><%t
SilverStripe\CMS\Controllers\CMSPageHistoryController.PREVIEW
'Website preview' %>
</div>
<div class="preview__device">
<div class="preview-device-outer">
<div class="preview-device-inner">
<iframe
src="about:blank"
class="center"
name="cms-preview-iframe"
></iframe>
</div>
</div>
</div>
</div>
<div
class="toolbar toolbar--south cms-content-controls cms-preview-controls"
></div>
<div class="cms-preview-overlay ui-widget-overlay-light"></div>
</div>
```

## 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.
Expand All @@ -116,7 +130,15 @@ class MyDataObject extends DataObject implements CMSPreviewable

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)
- 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.

Expand All @@ -131,7 +153,9 @@ BetterButtonsActions:
## Changelog

V 2.0.2
* added modeladmin support

- added modeladmin support

V 2.0.0
* renamed method previewRender to renderPreview

- renamed method previewRender to renderPreview
52 changes: 35 additions & 17 deletions src/controllers/DataObjectPreviewController.php
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
<?php
namespace SilverStripe\DataObjectPreview\Controllers;


use SilverStripe\i18n\i18n;
use SilverStripe\Control\Controller;
use SilverStripe\Control\Middleware\HTTPCacheControlMiddleware;
use SilverStripe\Versioned\Versioned;
Expand All @@ -10,9 +12,11 @@ class DataObjectPreviewController extends Controller
{
protected $dataobject;

private static $allowed_actions = [ 'show' ];
private static $allowed_actions = ['show'];
private static $url_segment = 'show';

private static $url_handlers = [
'show/$ClassName/$ID/$OtherClassName/$OtherID' => 'show'
'show/$ClassName/$ID/$OtherClassName/$OtherID' => 'show',
];

public static function strip_namespacing($namespaceClass)
Expand All @@ -36,45 +40,59 @@ protected function init()
}
}

public function getDataObject()
{
return $this->dataobject;
}

public function show($request)
{
$class = urldecode($request->param('ClassName'));
$class = str_replace('-', '\\', $class);
if (!class_exists($class)){
throw new InvalidArgumentException(sprintf(
'DataObjectPreviewController: Class of type %s doesn\'t exist',
$class
));
if (!class_exists($class)) {
throw new InvalidArgumentException(
sprintf(
'DataObjectPreviewController: Class of type %s doesn\'t exist',
$class
)
);
}

$id = $request->param('ID');
if (!ctype_digit($id))
{
throw new InvalidArgumentException('DataObjectPreviewController: ID needs to be an integer');
if (!ctype_digit($id)) {
throw new InvalidArgumentException(
'DataObjectPreviewController: ID needs to be an integer'
);
}

$this->dataobject = $class::get()->filter(array('ID' => $id))->First();
$this->dataobject = $class
::get()
->filter(['ID' => $id])
->First();

if ($this->dataobject && $this->dataobject->Locale) {
i18n::set_locale($this->dataobject->Locale);
}

$r = false;
switch (true)
{
case (!$this->dataobject):
switch (true) {
case !$this->dataobject:
$r = false;
break;
case ($this->dataobject->hasMethod('renderPreview')):
case $this->dataobject->hasMethod('renderPreview'):
$r = $this->dataobject->renderPreview();
break;

default:
$r = $this->dataobject->renderWith([
$class,
self::strip_namespacing($class)
self::strip_namespacing($class),
]);
break;
}

return $this->customise([
'Rendered' => $r
'Rendered' => $r,
])->renderWith('PreviewDataObject');
}
}
52 changes: 38 additions & 14 deletions src/extensions/PreviewGridFieldDetailFormExtension.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,31 +2,55 @@

namespace SilverStripe\DataObjectPreview\Extensions;

use SilverStripe\Admin\ModelAdmin;
use SilverStripe\Control\Controller;
use SilverStripe\Core\Extension;
use SilverStripe\CMS\Controllers\SilverStripeNavigator;
use SilverStripe\ORM\CMSPreviewable;
use SilverStripe\Forms\LiteralField;
use SilverStripe\View\Requirements;

class PreviewGridFieldDetailFormExtension extends Extension
{
/**
* Shall the legacy pre ss 4.11 preview code be used?
*
* @var boolean
*/
private static $inject_legacy_code = true;

public function updateItemEditForm(&$form)
{
$fields = $form->Fields();
if ($this->owner->record instanceof CMSPreviewable && !$fields->fieldByName('SilverStripeNavigator'))
{
$fields->removeByName('SilverStripeNavigator');
$ctrl = Controller::curr();
if(!$ctrl instanceof ModelAdmin){
$navigator = new SilverStripeNavigator($this->owner->record);
$field = new LiteralField('SilverStripeNavigator', $navigator->renderWith($ctrl->getTemplatesWithSuffix('_SilverStripeNavigator')));
$field->setAllowHTML(true);
$fields->push($field);
$form->addExtraClass('cms-previewable');
$form->addExtraClass('cms-previewabledataobject');
$form->removeExtraClass('cms-panel-padded center');
}
if (
$this->owner->record instanceof CMSPreviewable &&
!$fields->fieldByName('SilverStripeNavigator')
) {
$this->injectNavigatorAndPreview($form, $fields);
}
}

private function injectNavigatorAndPreview(&$form, &$fields)
{
if (!$this->owner->config()->inject_legacy_code) {
return;
}

Requirements::javascript(
'arillo/silverstripe-dataobject-preview:client/javascript/GridField.Preview.js'
);
//@TODO: Do we need to verify we are in the right controller?
$template = Controller::curr()->getTemplatesWithSuffix(
'_SilverStripeNavigator'
);
$navigator = new SilverStripeNavigator($this->owner->record);
$field = new LiteralField(
'SilverStripeNavigator',
$navigator->renderWith($template)
);
$field->setAllowHTML(true);
$fields->push($field);
$form->addExtraClass('cms-previewable');
$form->addExtraClass('cms-previewabledataobject');
$form->removeExtraClass('cms-panel-padded center');
}
}

0 comments on commit 885eed3

Please sign in to comment.