diff --git a/UPGRADE.md b/UPGRADE.md index f55c4ca..c3cae89 100644 --- a/UPGRADE.md +++ b/UPGRADE.md @@ -1,5 +1,7 @@ # Upgrade Notes +## 5.1.0 +- [FEATURE] pimcore backend: object preview button ## 5.0.6 - [IMPROVEMENT] Try to determinate locale and site when inline renderer is active (mostly via `checkMissingRequiredEditable()`) ## 5.0.5 diff --git a/config/services/event.yaml b/config/services/event.yaml index 8791deb..e28fbc8 100644 --- a/config/services/event.yaml +++ b/config/services/event.yaml @@ -30,6 +30,11 @@ services: tags: - { name: kernel.event_subscriber } + # event: add backend assets + I18nBundle\EventListener\Admin\AssetListener: + tags: + - { name: kernel.event_subscriber } + # event: log context switch I18nBundle\EventListener\ContextSwitchDetectorListener: tags: diff --git a/public/js/backend/i18n-object-preview.js b/public/js/backend/i18n-object-preview.js new file mode 100644 index 0000000..77c5c3f --- /dev/null +++ b/public/js/backend/i18n-object-preview.js @@ -0,0 +1,84 @@ +pimcore.registerNS('pimcore.plugin.i18n.objectPreview'); +pimcore.plugin.i18n.objectPreview = Class.create({ + + objectInstance: null, + + initialize: function () { + document.addEventListener(pimcore.events.postOpenObject, (e) => { + this.postOpenObject(e.detail.object, e.detail.type); + }); + }, + + postOpenObject: function(objectInstance) { + if (!objectInstance.data.hasPreview) { + return; + } + + this.objectInstance = objectInstance; + + let sitesStore = pimcore.globalmanager.get('sites'); + if (sitesStore.isLoading()) { + sitesStore.addListener('load', () => this.modifyObjectPreviewBtn()); + return; + } + + this.modifyObjectPreviewBtn(); + }, + + modifyObjectPreviewBtn: function() { + let locales = pimcore.settings.websiteLanguages; + let sitesStore = pimcore.globalmanager.get('sites'); + + let index = this.objectInstance.toolbar.items.length; + let origPreviewButton = this.objectInstance.toolbar.items.find(e => typeof e === 'object' && e.iconCls === 'pimcore_material_icon_preview pimcore_material_icon') + + if (origPreviewButton) { + index = this.objectInstance.toolbar.items.indexOf(origPreviewButton); + this.objectInstance.toolbar.remove(origPreviewButton); + } + + let previewButton = this.objectInstance.toolbar.insert(index, { + tooltip: t('open'), + scale: 'medium', + iconCls: 'pimcore_material_icon_preview pimcore_material_icon', + menu: [] + }); + + sitesStore.each((siteItem) => { + if (locales.length === 1) { + let locale = locales[0]; + previewButton.menu.insert({ + text: siteItem.data.domain + ' [' + locale + ']', + handler: () => this.openObjectPreview({ + i18n_locale: locale, + i18n_site: siteItem.data.id + }) + }) + } else { + previewButton.menu.insert({ + text: siteItem.data.domain, + menu: locales.map(locale => new Object({ + text: locale, + handler: () => this.openObjectPreview({ + i18n_locale: locale, + i18n_site: siteItem.data.id + }) + })) + }); + } + }); + }, + + openObjectPreview: function(params) { + let url = Routing.generate('pimcore_admin_dataobject_dataobject_preview', { + id: this.objectInstance.data.general.id, + time: (new Date()).getTime(), + ...params + }); + this.objectInstance.saveToSession(() => { + window.open(url); + }) + } + +}); +new pimcore.plugin.i18n.objectPreview(); diff --git a/src/EventListener/Admin/AssetListener.php b/src/EventListener/Admin/AssetListener.php new file mode 100644 index 0000000..bab5e7d --- /dev/null +++ b/src/EventListener/Admin/AssetListener.php @@ -0,0 +1,24 @@ + ['addJsFiles', -50], + ]; + } + + public function addJsFiles(PathsEvent $event): void + { + $event->addPaths([ + '/bundles/i18n/js/backend/i18n-object-preview.js', + ]); + } +} diff --git a/src/EventListener/I18nPreviewListener.php b/src/EventListener/I18nPreviewListener.php index b26972e..4ff4d2c 100644 --- a/src/EventListener/I18nPreviewListener.php +++ b/src/EventListener/I18nPreviewListener.php @@ -94,7 +94,7 @@ protected function resolveDocument(Request $request): void return; } - $path = $siteIdentifier !== null ? $this->siteResolver->getSitePath($request) : $request; + $path = $siteIdentifier ? $this->siteResolver->getSitePath($request) : $request; $nearestDocument = $this->documentService->getNearestDocumentByPath($path, false, $this->nearestDocumentTypes); if (!$nearestDocument instanceof Document) {