Skip to content

Commit

Permalink
i18n object preview button
Browse files Browse the repository at this point in the history
  • Loading branch information
benwalch committed Jun 20, 2024
1 parent 0bff479 commit 41de7bd
Show file tree
Hide file tree
Showing 5 changed files with 116 additions and 1 deletion.
2 changes: 2 additions & 0 deletions UPGRADE.md
Original file line number Diff line number Diff line change
@@ -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
Expand Down
5 changes: 5 additions & 0 deletions config/services/event.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down
84 changes: 84 additions & 0 deletions public/js/backend/i18n-object-preview.js
Original file line number Diff line number Diff line change
@@ -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();
24 changes: 24 additions & 0 deletions src/EventListener/Admin/AssetListener.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
<?php

namespace I18nBundle\EventListener\Admin;

use Pimcore\Event\BundleManager\PathsEvent;
use Pimcore\Event\BundleManagerEvents;
use Symfony\Component\EventDispatcher\EventSubscriberInterface;

class AssetListener implements EventSubscriberInterface
{
public static function getSubscribedEvents(): array
{
return [
BundleManagerEvents::JS_PATHS => ['addJsFiles', -50],
];
}

public function addJsFiles(PathsEvent $event): void
{
$event->addPaths([
'/bundles/i18n/js/backend/i18n-object-preview.js',
]);
}
}
2 changes: 1 addition & 1 deletion src/EventListener/I18nPreviewListener.php
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down

0 comments on commit 41de7bd

Please sign in to comment.