From fe9fdb6adfdfad61a21973e99b472954eb8ea968 Mon Sep 17 00:00:00 2001 From: DM Date: Thu, 31 Dec 2020 17:36:27 +0100 Subject: [PATCH 1/8] make modeladmin preview work in SilverStripe 4 --- Readme.md | 61 +++++++++++++++--- .../DataObjectPreviewController.php | 2 +- .../Admin/Includes/LeftAndMain_EditForm.ss | 63 ------------------- 3 files changed, 54 insertions(+), 72 deletions(-) delete mode 100644 templates/SilverStripe/Admin/Includes/LeftAndMain_EditForm.ss diff --git a/Readme.md b/Readme.md index a02c354..391055b 100644 --- a/Readme.md +++ b/Readme.md @@ -5,10 +5,14 @@ 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. 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 @@ -23,9 +27,18 @@ For a SilverStripe 3.x compatible version of this module, please see the [1 bran ```php ClassName, $this->ID); + return Controller::join_links(Director::baseURL(), 'cms-preview', 'show', urlencode($this->ClassName), $this->ID); } public function getMimeType() @@ -50,23 +63,53 @@ class MyDataObject extends DataObject implements CMSPreviewable } ``` -## Overwrites +If our CustomModelAdmin looks like this: -We override following core template to include the preview toolbar also when previewing a DataObject: silverstripe-admin/templates/SilverStripe/Admin/Includes/LeftAndMain_EditForm.ss +```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/MyDataObject.ss. +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/Includes/MyDataObject.ss - return $this->renderWith('Includes/'.MyDataObject::class); + // this will look for themes/yourtheme/templates/Arillo/DataObjectPreview/Models/MyDataObject.ss + return $this->renderWith(MyDataObject::class); } } ``` @@ -87,6 +130,8 @@ BetterButtonsActions: ## Changelog -V 2.0.0 +V 2.0.2 +* added modeladmin support +V 2.0.0 * renamed method previewRender to renderPreview diff --git a/src/controllers/DataObjectPreviewController.php b/src/controllers/DataObjectPreviewController.php index 4f6a971..88f23e5 100644 --- a/src/controllers/DataObjectPreviewController.php +++ b/src/controllers/DataObjectPreviewController.php @@ -38,7 +38,7 @@ protected function init() public function show($request) { - $class = $request->param('ClassName'); + $class = urldecode($request->param('ClassName')); $class = str_replace('-', '\\', $class); if (!class_exists($class)){ throw new InvalidArgumentException(sprintf( diff --git a/templates/SilverStripe/Admin/Includes/LeftAndMain_EditForm.ss b/templates/SilverStripe/Admin/Includes/LeftAndMain_EditForm.ss deleted file mode 100644 index cf61bc9..0000000 --- a/templates/SilverStripe/Admin/Includes/LeftAndMain_EditForm.ss +++ /dev/null @@ -1,63 +0,0 @@ -<% if $IncludeFormTag %> -
-<% end_if %> -
-
- <% include SilverStripe\\Admin\\BackLink_Button %> - <% with $Controller %> - <% include SilverStripe\\Admin\\CMSBreadcrumbs %> - <% end_with %> -
- <% if $Fields.hasTabset %> - <% with $Fields.fieldByName('Root') %> -
-
    - <% loop $Tabs %> - class="$extraClass"<% end_if %>>$Title - <% end_loop %> -
-
- <% end_with %> - <% end_if %> - - -
- - <% with $Controller %> - $EditFormTools - <% end_with %> - -
- <% if $Message %> -

$Message

- <% else %> - - <% end_if %> - -
- <% if $Legend %>$Legend<% end_if %> - <% loop $Fields %> - $FieldHolder - <% end_loop %> -
-
-
- -
- <% if $Actions %> -
- <% loop $Actions %> - $FieldHolder - <% end_loop %> - <% if $Controller.LinkPreview %> - - <%t SilverStripe\Admin\LeftAndMain.PreviewButton 'Preview' %> » - - <% end_if %> - <% include SilverStripe\\Admin\\LeftAndMain_ViewModeSelector SelectID="preview-mode-dropdown-in-content" %> -
- <% end_if %> -
-<% if $IncludeFormTag %> -
-<% end_if %> From 61f84c5048343e4e70c918e20235013a47d2640b Mon Sep 17 00:00:00 2001 From: banglashi Date: Wed, 10 Feb 2021 23:22:18 +0100 Subject: [PATCH 2/8] add proper overwrite template location --- Readme.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Readme.md b/Readme.md index 391055b..af7966b 100644 --- a/Readme.md +++ b/Readme.md @@ -78,7 +78,7 @@ 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
From 18d340bc6f0b077c1e527d27e16beb80a7ae9410 Mon Sep 17 00:00:00 2001 From: banglashi Date: Wed, 12 May 2021 17:25:55 +0200 Subject: [PATCH 3/8] Update PreviewGridFieldDetailFormExtension.php --- src/extensions/PreviewGridFieldDetailFormExtension.php | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/src/extensions/PreviewGridFieldDetailFormExtension.php b/src/extensions/PreviewGridFieldDetailFormExtension.php index dbc6416..8431ecd 100644 --- a/src/extensions/PreviewGridFieldDetailFormExtension.php +++ b/src/extensions/PreviewGridFieldDetailFormExtension.php @@ -8,17 +8,17 @@ use SilverStripe\CMS\Controllers\SilverStripeNavigator; use SilverStripe\ORM\CMSPreviewable; use SilverStripe\Forms\LiteralField; +use SilverStripe\View\Requirements; class PreviewGridFieldDetailFormExtension extends Extension { public function updateItemEditForm(&$form) { $fields = $form->Fields(); - if ($this->owner->record instanceof CMSPreviewable && !$fields->fieldByName('SilverStripeNavigator')) - { - $fields->removeByName('SilverStripeNavigator'); + if ($this->owner->record instanceof CMSPreviewable && !$fields->fieldByName('SilverStripeNavigator')) { $ctrl = Controller::curr(); - if(!$ctrl instanceof ModelAdmin){ + if ($ctrl instanceof ModelAdmin) { + Requirements::javascript('arillo/silverstripe-dataobject-preview:client/javascript/GridField.Preview.js'); $navigator = new SilverStripeNavigator($this->owner->record); $field = new LiteralField('SilverStripeNavigator', $navigator->renderWith($ctrl->getTemplatesWithSuffix('_SilverStripeNavigator'))); $field->setAllowHTML(true); From a638f7fd3d903d410ae94525010e16b5650e91d0 Mon Sep 17 00:00:00 2001 From: dm Date: Wed, 12 May 2021 23:46:45 +0200 Subject: [PATCH 4/8] revert back --- .../PreviewGridFieldDetailFormExtension.php | 27 ++++++++++--------- 1 file changed, 15 insertions(+), 12 deletions(-) diff --git a/src/extensions/PreviewGridFieldDetailFormExtension.php b/src/extensions/PreviewGridFieldDetailFormExtension.php index 8431ecd..d13e5de 100644 --- a/src/extensions/PreviewGridFieldDetailFormExtension.php +++ b/src/extensions/PreviewGridFieldDetailFormExtension.php @@ -2,7 +2,6 @@ namespace SilverStripe\DataObjectPreview\Extensions; -use SilverStripe\Admin\ModelAdmin; use SilverStripe\Control\Controller; use SilverStripe\Core\Extension; use SilverStripe\CMS\Controllers\SilverStripeNavigator; @@ -16,17 +15,21 @@ public function updateItemEditForm(&$form) { $fields = $form->Fields(); if ($this->owner->record instanceof CMSPreviewable && !$fields->fieldByName('SilverStripeNavigator')) { - $ctrl = Controller::curr(); - if ($ctrl instanceof ModelAdmin) { - Requirements::javascript('arillo/silverstripe-dataobject-preview:client/javascript/GridField.Preview.js'); - $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'); - } + $this->injectNavigatorAndPreview($form, $fields); } } + + private function injectNavigatorAndPreview(&$form, &$fields) + { + 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'); + } } From a58c90bab92120a94fecd80b9aafdb0e6e39777d Mon Sep 17 00:00:00 2001 From: Bumbus Date: Thu, 3 Jun 2021 14:29:25 +0200 Subject: [PATCH 5/8] added url_segment --- src/controllers/DataObjectPreviewController.php | 2 ++ 1 file changed, 2 insertions(+) diff --git a/src/controllers/DataObjectPreviewController.php b/src/controllers/DataObjectPreviewController.php index 88f23e5..595c180 100644 --- a/src/controllers/DataObjectPreviewController.php +++ b/src/controllers/DataObjectPreviewController.php @@ -11,6 +11,8 @@ class DataObjectPreviewController extends Controller protected $dataobject; private static $allowed_actions = [ 'show' ]; + private static $url_segment = 'show'; + private static $url_handlers = [ 'show/$ClassName/$ID/$OtherClassName/$OtherID' => 'show' ]; From d90ac37f9b06326256151062d5e6ced79b48cb37 Mon Sep 17 00:00:00 2001 From: Bumbus Date: Thu, 2 Dec 2021 17:00:32 +0100 Subject: [PATCH 6/8] added dataobject getter --- .../DataObjectPreviewController.php | 44 ++++++++++++------- 1 file changed, 27 insertions(+), 17 deletions(-) diff --git a/src/controllers/DataObjectPreviewController.php b/src/controllers/DataObjectPreviewController.php index 595c180..6fa09dd 100644 --- a/src/controllers/DataObjectPreviewController.php +++ b/src/controllers/DataObjectPreviewController.php @@ -10,11 +10,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) @@ -38,45 +38,55 @@ 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(); $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'); } } From fbe08afd2e387a878d47c05a021d1ccd0f89b785 Mon Sep 17 00:00:00 2001 From: Bumbus Date: Fri, 13 May 2022 15:57:26 +0200 Subject: [PATCH 7/8] set i18n locale if record locale exists, fixes t-strings in previews --- src/controllers/DataObjectPreviewController.php | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/src/controllers/DataObjectPreviewController.php b/src/controllers/DataObjectPreviewController.php index 6fa09dd..d5153dd 100644 --- a/src/controllers/DataObjectPreviewController.php +++ b/src/controllers/DataObjectPreviewController.php @@ -1,6 +1,8 @@ filter(['ID' => $id]) ->First(); + if ($this->dataobject && $this->dataobject->Locale) { + i18n::set_locale($this->dataobject->Locale); + } + $r = false; switch (true) { case !$this->dataobject: From de70f46689069d245411a57459f848eea0ec0069 Mon Sep 17 00:00:00 2001 From: Bumbus Date: Tue, 28 Jun 2022 17:27:01 +0200 Subject: [PATCH 8/8] added inject_legacy_code flag for usage with ss 4.11 or greater --- Readme.md | 8 +++++ .../PreviewGridFieldDetailFormExtension.php | 29 ++++++++++++++++--- 2 files changed, 33 insertions(+), 4 deletions(-) diff --git a/Readme.md b/Readme.md index c42e373..a13ab8a 100644 --- a/Readme.md +++ b/Readme.md @@ -132,6 +132,14 @@ You can overwrite the main template by placing it either in themes/yourtheme/tem - 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. ``` diff --git a/src/extensions/PreviewGridFieldDetailFormExtension.php b/src/extensions/PreviewGridFieldDetailFormExtension.php index d13e5de..267a57d 100644 --- a/src/extensions/PreviewGridFieldDetailFormExtension.php +++ b/src/extensions/PreviewGridFieldDetailFormExtension.php @@ -11,21 +11,42 @@ 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')) { + if ( + $this->owner->record instanceof CMSPreviewable && + !$fields->fieldByName('SilverStripeNavigator') + ) { $this->injectNavigatorAndPreview($form, $fields); } } private function injectNavigatorAndPreview(&$form, &$fields) { - Requirements::javascript('arillo/silverstripe-dataobject-preview:client/javascript/GridField.Preview.js'); + 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'); + $template = Controller::curr()->getTemplatesWithSuffix( + '_SilverStripeNavigator' + ); $navigator = new SilverStripeNavigator($this->owner->record); - $field = new LiteralField('SilverStripeNavigator', $navigator->renderWith($template)); + $field = new LiteralField( + 'SilverStripeNavigator', + $navigator->renderWith($template) + ); $field->setAllowHTML(true); $fields->push($field); $form->addExtraClass('cms-previewable');