From ccab1aa9df2c3bba1997ce16dc878c8cd828bc6c Mon Sep 17 00:00:00 2001 From: Stefan Hagspiel Date: Thu, 30 May 2024 20:25:37 +0200 Subject: [PATCH 1/3] try to determinate locale and site when inline renderer is active --- UPGRADE.md | 2 + config/services/route.yaml | 4 + .../Type/PimcoreExportDataAwareModifier.php | 7 +- .../PimcoreInlineRendererAwareModifier.php | 101 ++++++++++++++++++ 4 files changed, 109 insertions(+), 5 deletions(-) create mode 100644 src/Modifier/RouteItem/Type/PimcoreInlineRendererAwareModifier.php diff --git a/UPGRADE.md b/UPGRADE.md index afc6c46..f55c4ca 100644 --- a/UPGRADE.md +++ b/UPGRADE.md @@ -1,5 +1,7 @@ # Upgrade Notes +## 5.0.6 +- [IMPROVEMENT] Try to determinate locale and site when inline renderer is active (mostly via `checkMissingRequiredEditable()`) ## 5.0.5 - [BUGFIX] Fix exception handler decoration ## 5.0.4 diff --git a/config/services/route.yaml b/config/services/route.yaml index 393dcb7..0aa4b5a 100644 --- a/config/services/route.yaml +++ b/config/services/route.yaml @@ -29,5 +29,9 @@ services: - { name: i18n.modifier.route_item } I18nBundle\Modifier\RouteItem\Type\PimcoreExportDataAwareModifier: + tags: + - { name: i18n.modifier.route_item } + + I18nBundle\Modifier\RouteItem\Type\PimcoreInlineRendererAwareModifier: tags: - { name: i18n.modifier.route_item } \ No newline at end of file diff --git a/src/Modifier/RouteItem/Type/PimcoreExportDataAwareModifier.php b/src/Modifier/RouteItem/Type/PimcoreExportDataAwareModifier.php index 13a6f96..f5d22e9 100644 --- a/src/Modifier/RouteItem/Type/PimcoreExportDataAwareModifier.php +++ b/src/Modifier/RouteItem/Type/PimcoreExportDataAwareModifier.php @@ -3,7 +3,6 @@ namespace I18nBundle\Modifier\RouteItem\Type; use I18nBundle\Model\RouteItem\RouteItemInterface; -use Pimcore\Http\RequestHelper; use Pimcore\Model\Document; use Pimcore\Model\Site; use Pimcore\Tool\Frontend; @@ -17,10 +16,8 @@ class PimcoreExportDataAwareModifier implements RouteItemModifierInterface 'pimcore_bundle_xliff_translation_xliffexport' ]; - public function __construct( - protected RequestStack $requestStack, - protected RequestHelper $requestHelper - ) { + public function __construct(protected RequestStack $requestStack) + { } public function supportParameters(string $type, RouteItemInterface $routeItem, array $parameters, array $context): bool diff --git a/src/Modifier/RouteItem/Type/PimcoreInlineRendererAwareModifier.php b/src/Modifier/RouteItem/Type/PimcoreInlineRendererAwareModifier.php new file mode 100644 index 0000000..ef85f76 --- /dev/null +++ b/src/Modifier/RouteItem/Type/PimcoreInlineRendererAwareModifier.php @@ -0,0 +1,101 @@ +isValidRequest(); + } + + public function supportRequest(string $type, RouteItemInterface $routeItem, Request $request, array $context): bool + { + return $this->isValidRequest(); + } + + public function modifyByParameters(RouteItemInterface $routeItem, array $parameters, array $context): void + { + $this->modify($routeItem, $this->requestStack->getMainRequest()); + } + + public function modifyByRequest(RouteItemInterface $routeItem, Request $request, array $context): void + { + $this->modify($routeItem, $request); + } + + protected function modify(RouteItemInterface $routeItem, Request $request): void + { + $document = $this->determinateDocumentByRoute($request); + + $hasSiteContext = $routeItem->getRouteContextBag()->has('site') && $routeItem->getRouteContextBag()->get('site') !== null; + $hasLocaleParameter = $routeItem->getRouteParametersBag()->has('_locale'); + + if (!$document instanceof Document) { + return; + } + + if (!$hasSiteContext) { + $site = Frontend::getSiteForDocument($document); + if ($site instanceof Site) { + $routeItem->getRouteContextBag()->set('site', $site); + } + } + + if (!$hasLocaleParameter && !empty($document->getProperty('language'))) { + $routeItem->getRouteParametersBag()->set('_locale', $document->getProperty('language')); + } + } + + private function determinateDocumentByRoute(Request $request): ?Document + { + if ( + $request->attributes->get('_route') === 'pimcore_admin_document_document_add' && + $request->request->get('elementType') === 'document' && + !empty($request->request->get('parentId')) + ) { + return Document::getById($request->request->get('parentId')); + } + + if ( + $request->attributes->get('_route') === 'pimcore_admin_document_page_save' && + !empty($request->request->get('id')) + ) { + return Document::getById($request->request->get('id')); + } + + return null; + } + + private function isValidRequest(): bool + { + $parentRequest = $this->requestStack->getParentRequest(); + + if (!$parentRequest instanceof Request) { + return false; + } + + if (!$parentRequest->attributes->has(EditmodeResolver::ATTRIBUTE_EDITMODE) || $parentRequest->attributes->get(EditmodeResolver::ATTRIBUTE_EDITMODE) === false) { + return false; + } + + if (!$parentRequest->attributes->has(Block::ATTRIBUTE_IGNORE_EDITMODE_INDICES) || $parentRequest->attributes->get(Block::ATTRIBUTE_IGNORE_EDITMODE_INDICES) === false) { + return false; + } + + return true; + } +} From ada230245aff5bcc6ee37ae22d0a16cb244f685b Mon Sep 17 00:00:00 2001 From: Stefan Hagspiel Date: Thu, 30 May 2024 20:54:18 +0200 Subject: [PATCH 2/3] simplify render check --- .../PimcoreInlineRendererAwareModifier.php | 27 +++++++------------ 1 file changed, 10 insertions(+), 17 deletions(-) diff --git a/src/Modifier/RouteItem/Type/PimcoreInlineRendererAwareModifier.php b/src/Modifier/RouteItem/Type/PimcoreInlineRendererAwareModifier.php index ef85f76..6a011eb 100644 --- a/src/Modifier/RouteItem/Type/PimcoreInlineRendererAwareModifier.php +++ b/src/Modifier/RouteItem/Type/PimcoreInlineRendererAwareModifier.php @@ -3,9 +3,7 @@ namespace I18nBundle\Modifier\RouteItem\Type; use I18nBundle\Model\RouteItem\RouteItemInterface; -use Pimcore\Http\Request\Resolver\EditmodeResolver; use Pimcore\Model\Document; -use Pimcore\Model\Document\Editable\Block; use Pimcore\Model\Site; use Pimcore\Tool\Frontend; use Symfony\Component\HttpFoundation\Request; @@ -13,6 +11,11 @@ class PimcoreInlineRendererAwareModifier implements RouteItemModifierInterface { + private const EDIT_AWARE_ROUTES = [ + 'pimcore_admin_document_document_add', + 'pimcore_admin_document_page_save' + ]; + public function __construct(protected RequestStack $requestStack) { } @@ -82,20 +85,10 @@ private function determinateDocumentByRoute(Request $request): ?Document private function isValidRequest(): bool { - $parentRequest = $this->requestStack->getParentRequest(); - - if (!$parentRequest instanceof Request) { - return false; - } - - if (!$parentRequest->attributes->has(EditmodeResolver::ATTRIBUTE_EDITMODE) || $parentRequest->attributes->get(EditmodeResolver::ATTRIBUTE_EDITMODE) === false) { - return false; - } - - if (!$parentRequest->attributes->has(Block::ATTRIBUTE_IGNORE_EDITMODE_INDICES) || $parentRequest->attributes->get(Block::ATTRIBUTE_IGNORE_EDITMODE_INDICES) === false) { - return false; - } - - return true; + return in_array( + $this->requestStack->getMainRequest()->attributes->get('_route'), + self::EDIT_AWARE_ROUTES, + true + ); } } From d5be61393a417d37a3cd30cd1cb4a1f240124a13 Mon Sep 17 00:00:00 2001 From: Stefan Hagspiel Date: Thu, 30 May 2024 20:59:51 +0200 Subject: [PATCH 3/3] clean up --- .../Type/PimcoreExportDataAwareModifier.php | 21 ++++++++++--------- .../PimcoreInlineRendererAwareModifier.php | 4 ++++ 2 files changed, 15 insertions(+), 10 deletions(-) diff --git a/src/Modifier/RouteItem/Type/PimcoreExportDataAwareModifier.php b/src/Modifier/RouteItem/Type/PimcoreExportDataAwareModifier.php index f5d22e9..3ae9937 100644 --- a/src/Modifier/RouteItem/Type/PimcoreExportDataAwareModifier.php +++ b/src/Modifier/RouteItem/Type/PimcoreExportDataAwareModifier.php @@ -22,20 +22,12 @@ public function __construct(protected RequestStack $requestStack) public function supportParameters(string $type, RouteItemInterface $routeItem, array $parameters, array $context): bool { - if (!$this->requestStack->getMainRequest() instanceof Request) { - return false; - } - - return in_array($this->requestStack->getMainRequest()->attributes->get('_route'), self::EXPORT_AWARE_ROUTES, true); + return $this->isValidRequest(); } public function supportRequest(string $type, RouteItemInterface $routeItem, Request $request, array $context): bool { - if (!$this->requestStack->getMainRequest() instanceof Request) { - return false; - } - - return in_array($this->requestStack->getMainRequest()->attributes->get('_route'), self::EXPORT_AWARE_ROUTES, true); + return $this->isValidRequest(); } public function modifyByParameters(RouteItemInterface $routeItem, array $parameters, array $context): void @@ -90,4 +82,13 @@ protected function modify(RouteItemInterface $routeItem, Request $request): void $routeItem->getRouteParametersBag()->set('_locale', $element->getProperty('language')); } } + + private function isValidRequest(): bool + { + if (!$this->requestStack->getMainRequest() instanceof Request) { + return false; + } + + return in_array($this->requestStack->getMainRequest()->attributes->get('_route'), self::EXPORT_AWARE_ROUTES, true); + } } diff --git a/src/Modifier/RouteItem/Type/PimcoreInlineRendererAwareModifier.php b/src/Modifier/RouteItem/Type/PimcoreInlineRendererAwareModifier.php index 6a011eb..c470ffc 100644 --- a/src/Modifier/RouteItem/Type/PimcoreInlineRendererAwareModifier.php +++ b/src/Modifier/RouteItem/Type/PimcoreInlineRendererAwareModifier.php @@ -85,6 +85,10 @@ private function determinateDocumentByRoute(Request $request): ?Document private function isValidRequest(): bool { + if (!$this->requestStack->getMainRequest() instanceof Request) { + return false; + } + return in_array( $this->requestStack->getMainRequest()->attributes->get('_route'), self::EDIT_AWARE_ROUTES,