Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

try to determinate locale and site when inline renderer is active #161

Merged
merged 3 commits into from
May 30, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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.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
Expand Down
4 changes: 4 additions & 0 deletions config/services/route.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -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 }
28 changes: 13 additions & 15 deletions src/Modifier/RouteItem/Type/PimcoreExportDataAwareModifier.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -17,28 +16,18 @@ 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
{
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
Expand Down Expand Up @@ -93,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);
}
}
98 changes: 98 additions & 0 deletions src/Modifier/RouteItem/Type/PimcoreInlineRendererAwareModifier.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,98 @@
<?php

namespace I18nBundle\Modifier\RouteItem\Type;

use I18nBundle\Model\RouteItem\RouteItemInterface;
use Pimcore\Model\Document;
use Pimcore\Model\Site;
use Pimcore\Tool\Frontend;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\RequestStack;

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)
{
}

public function supportParameters(string $type, RouteItemInterface $routeItem, array $parameters, array $context): bool
{
return $this->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
{
if (!$this->requestStack->getMainRequest() instanceof Request) {
return false;
}

return in_array(
$this->requestStack->getMainRequest()->attributes->get('_route'),
self::EDIT_AWARE_ROUTES,
true
);
}
}
Loading