Skip to content

Commit

Permalink
add locale and site automatically in export mode (#156)
Browse files Browse the repository at this point in the history
  • Loading branch information
solverat authored May 23, 2024
1 parent 14a646d commit 66569a4
Show file tree
Hide file tree
Showing 3 changed files with 103 additions and 0 deletions.
3 changes: 3 additions & 0 deletions UPGRADE.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
# Upgrade Notes

## 4.1.11
- [IMPROVEMENT] Add locale and site automatically in export mode (xliff, word export), based on given document

## 4.1.10
- [BUGFIX] Fix exception handler priority to prevent authentication exception hijacking

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,96 @@
<?php

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;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\RequestStack;

class PimcoreExportDataAwareModifier implements RouteItemModifierInterface
{
private const EXPORT_AWARE_ROUTES = [
'pimcore_admin_translation_wordexport',
'pimcore_admin_translation_xliffexport'
];

public function __construct(
protected RequestStack $requestStack,
protected RequestHelper $requestHelper
) {
}

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

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

public function modifyByParameters(RouteItemInterface $routeItem, array $parameters, array $context): void
{
if (!$this->requestStack->getMainRequest() instanceof Request) {
return;
}

$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
{
$hasSiteContext = $routeItem->getRouteContextBag()->has('site') && $routeItem->getRouteContextBag()->get('site') !== null;
$hasLocaleParameter = $routeItem->getRouteParametersBag()->has('_locale');

$exportData = $request->request->has('data') ? json_decode($request->request->get('data'), true) : [];

if (count($exportData) === 0) {
return;
}

if (!array_key_exists('id', $exportData[0]) || !array_key_exists('type', $exportData[0])) {
return;
}

$elementId = $exportData[0]['id'];
$elementType = $exportData[0]['type'];

if ($elementType !== 'document') {
return;
}

$element = Document::getById($elementId);
if (!$element instanceof Document) {
return;
}

if (!$hasSiteContext) {
$site = Frontend::getSiteForDocument($element);
if ($site instanceof Site) {
$routeItem->getRouteContextBag()->set('site', $site);
}
}

if (!$hasLocaleParameter) {
$routeItem->getRouteParametersBag()->set('_locale', $element->getProperty('language'));
}
}
}
4 changes: 4 additions & 0 deletions src/I18nBundle/Resources/config/services/route.yml
Original file line number Diff line number Diff line change
Expand Up @@ -27,3 +27,7 @@ services:
I18nBundle\Modifier\RouteItem\Type\RequestAwareModifier:
tags:
- { name: i18n.modifier.route_item }

I18nBundle\Modifier\RouteItem\Type\PimcoreExportDataAwareModifier:
tags:
- { name: i18n.modifier.route_item }

0 comments on commit 66569a4

Please sign in to comment.