diff --git a/Readme.md b/Readme.md index 391055b..a13ab8a 100644 --- a/Readme.md +++ b/Readme.md @@ -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. @@ -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 -
-
-
<%t SilverStripe\CMS\Controllers\CMSPageHistoryController.PREVIEW 'Website preview' %>
-
-
-
- -
-
-
-
-
-
+
+
+
+ <%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. @@ -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. @@ -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 diff --git a/src/controllers/DataObjectPreviewController.php b/src/controllers/DataObjectPreviewController.php index 88f23e5..d5153dd 100644 --- a/src/controllers/DataObjectPreviewController.php +++ b/src/controllers/DataObjectPreviewController.php @@ -1,6 +1,8 @@ 'show' + 'show/$ClassName/$ID/$OtherClassName/$OtherID' => 'show', ]; public static function strip_namespacing($namespaceClass) @@ -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'); } } diff --git a/src/extensions/PreviewGridFieldDetailFormExtension.php b/src/extensions/PreviewGridFieldDetailFormExtension.php index dbc6416..267a57d 100644 --- a/src/extensions/PreviewGridFieldDetailFormExtension.php +++ b/src/extensions/PreviewGridFieldDetailFormExtension.php @@ -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'); } }