-
Notifications
You must be signed in to change notification settings - Fork 19
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
add locale and site automatically in export mode (#156)
- Loading branch information
Showing
3 changed files
with
103 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
96 changes: 96 additions & 0 deletions
96
src/I18nBundle/Modifier/RouteItem/Type/PimcoreExportDataAwareModifier.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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')); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters