From 62160016078e2362e220a4eb6c858d683d49e267 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Dawid=20Parafi=C5=84ski?= Date: Thu, 11 Oct 2018 11:43:33 +0200 Subject: [PATCH] EZP-29617: No route found after adding new translation (#256) * EZP-29617: No route found after adding new translation * EZP-29617: No route found after adding new translation --- bundle/Resources/config/services.yml | 5 +- lib/Form/Processor/ContentFormProcessor.php | 57 +++------ .../Processor/SystemUrlRedirectProcessor.php | 108 ++++++++++++++++++ 3 files changed, 127 insertions(+), 43 deletions(-) create mode 100644 lib/Form/Processor/SystemUrlRedirectProcessor.php diff --git a/bundle/Resources/config/services.yml b/bundle/Resources/config/services.yml index d2ca805e0..37feb29c7 100644 --- a/bundle/Resources/config/services.yml +++ b/bundle/Resources/config/services.yml @@ -208,7 +208,6 @@ services: - '@ezpublish.api.service.content' - '@ezpublish.api.service.location' - '@router' - - '@ezpublish.api.service.url_alias' tags: - { name: kernel.event_subscriber } @@ -251,6 +250,10 @@ services: tags: - { name: kernel.event_subscriber } + EzSystems\RepositoryForms\Form\Processor\SystemUrlRedirectProcessor: + autowire: true + autoconfigure: true + # Controllers ezrepoforms.controller.content_edit: class: "%ezrepoforms.controller.content_edit.class%" diff --git a/lib/Form/Processor/ContentFormProcessor.php b/lib/Form/Processor/ContentFormProcessor.php index 595786039..d738cfe53 100644 --- a/lib/Form/Processor/ContentFormProcessor.php +++ b/lib/Form/Processor/ContentFormProcessor.php @@ -9,7 +9,6 @@ use eZ\Publish\API\Repository\ContentService; use eZ\Publish\API\Repository\LocationService; -use eZ\Publish\API\Repository\URLAliasService; use eZ\Publish\API\Repository\Values\Content\Content; use eZ\Publish\API\Repository\Values\Content\ContentStruct; use eZ\Publish\API\Repository\Values\Content\Location; @@ -21,6 +20,7 @@ use EzSystems\RepositoryForms\Event\RepositoryFormEvents; use Symfony\Component\EventDispatcher\EventSubscriberInterface; use Symfony\Component\HttpFoundation\RedirectResponse; +use Symfony\Component\Routing\Generator\UrlGeneratorInterface; use Symfony\Component\Routing\RouterInterface; /** @@ -37,9 +37,6 @@ class ContentFormProcessor implements EventSubscriberInterface /** @var \Symfony\Component\Routing\RouterInterface */ private $router; - /** @var \eZ\Publish\API\Repository\URLAliasService */ - private $urlAliasService; - /** * @param \eZ\Publish\API\Repository\ContentService $contentService * @param \eZ\Publish\API\Repository\LocationService $locationService @@ -49,13 +46,11 @@ class ContentFormProcessor implements EventSubscriberInterface public function __construct( ContentService $contentService, LocationService $locationService, - RouterInterface $router, - URLAliasService $urlAliasService + RouterInterface $router ) { $this->contentService = $contentService; $this->locationService = $locationService; $this->router = $router; - $this->urlAliasService = $urlAliasService; } /** @@ -110,7 +105,6 @@ public function processSaveDraft(FormActionEvent $event) * @throws \eZ\Publish\API\Repository\Exceptions\ContentFieldValidationException * @throws \eZ\Publish\API\Repository\Exceptions\ContentValidationException * @throws \eZ\Publish\API\Repository\Exceptions\InvalidArgumentException - * @throws \eZ\Publish\API\Repository\Exceptions\NotFoundException * @throws \eZ\Publish\API\Repository\Exceptions\UnauthorizedException * @throws \eZ\Publish\Core\Base\Exceptions\InvalidArgumentException */ @@ -123,35 +117,19 @@ public function processPublish(FormActionEvent $event) $draft = $this->saveDraft($data, $form->getConfig()->getOption('languageCode')); $content = $this->contentService->publishVersion($draft->versionInfo); - $location = $this->locationService->loadLocation($content->contentInfo->mainLocationId); + $redirectUrl = $form['redirectUrlAfterPublish']->getData() ?: $this->router->generate( + '_ezpublishLocation', [ + 'locationId' => $content->contentInfo->mainLocationId, + ] + ); - $redirectUrl = $form['redirectUrlAfterPublish']->getData() ?: $this->getSystemUrl($location, [$content->versionInfo->initialLanguageCode]); $event->setResponse(new RedirectResponse($redirectUrl)); } - /** - * @param \eZ\Publish\API\Repository\Values\Content\Location $location - * @param array $prioritizedLanguageList - * - * @return string - * - * @throws \eZ\Publish\API\Repository\Exceptions\NotFoundException - */ - private function getSystemUrl(Location $location, array $prioritizedLanguageList): string - { - return $this->urlAliasService->reverseLookup( - $location, - null, - true, - $prioritizedLanguageList - )->path; - } - /** * @param \EzSystems\RepositoryForms\Event\FormActionEvent $event * * @throws \eZ\Publish\API\Repository\Exceptions\BadStateException - * @throws \eZ\Publish\API\Repository\Exceptions\NotFoundException * @throws \eZ\Publish\API\Repository\Exceptions\UnauthorizedException */ public function processCancel(FormActionEvent $event) @@ -160,14 +138,10 @@ public function processCancel(FormActionEvent $event) $data = $event->getData(); if ($data->isNew()) { - $parentLocation = $this->locationService->loadLocation( - $data->getLocationStructs()[0]->parentLocationId - ); - $url = $this->getSystemUrl( - $parentLocation, - [$data->mainLanguageCode, $parentLocation->contentInfo->mainLanguageCode] - ); - $response = new RedirectResponse($url); + $response = new RedirectResponse($this->router->generate( + '_ezpublishLocation', + ['locationId' => $data->getLocationStructs()[0]->parentLocationId] + )); $event->setResponse($response); return; @@ -187,11 +161,10 @@ public function processCancel(FormActionEvent $event) $this->contentService->deleteVersion($versionInfo); } - $locationToRedirect = $this->locationService->loadLocation($redirectionLocationId); - - $url = $this->getSystemUrl( - $locationToRedirect, - [$locationToRedirect->contentInfo->mainLanguageCode] + $url = $this->router->generate( + '_ezpublishLocation', + ['locationId' => $redirectionLocationId], + UrlGeneratorInterface::ABSOLUTE_URL ); $event->setResponse(new RedirectResponse($url)); diff --git a/lib/Form/Processor/SystemUrlRedirectProcessor.php b/lib/Form/Processor/SystemUrlRedirectProcessor.php new file mode 100644 index 000000000..9446beb7c --- /dev/null +++ b/lib/Form/Processor/SystemUrlRedirectProcessor.php @@ -0,0 +1,108 @@ +router = $router; + $this->urlAliasService = $urlAliasService; + $this->locationService = $locationService; + } + + /** + * @return array + */ + public static function getSubscribedEvents(): array + { + return [ + RepositoryFormEvents::CONTENT_PUBLISH => ['processRedirectAfterPublish', 2], + RepositoryFormEvents::CONTENT_CANCEL => ['processRedirectAfterCancel', 2], + ]; + } + + /** + * @param \EzSystems\RepositoryForms\Event\FormActionEvent $event + * + * @throws \eZ\Publish\API\Repository\Exceptions\NotFoundException + * @throws \eZ\Publish\API\Repository\Exceptions\UnauthorizedException + */ + public function processRedirectAfterPublish(FormActionEvent $event): void + { + if ($event->getForm()['redirectUrlAfterPublish']->getData()) { + return; + } + + $this->resolveSystemUrlRedirect($event); + } + + /** + * @param \EzSystems\RepositoryForms\Event\FormActionEvent $event + * + * @throws \eZ\Publish\API\Repository\Exceptions\NotFoundException + * @throws \eZ\Publish\API\Repository\Exceptions\UnauthorizedException + */ + public function processRedirectAfterCancel(FormActionEvent $event): void + { + $this->resolveSystemUrlRedirect($event); + } + + /** + * @param \EzSystems\RepositoryForms\Event\FormActionEvent $event + * + * @throws \eZ\Publish\API\Repository\Exceptions\NotFoundException + * @throws \eZ\Publish\API\Repository\Exceptions\UnauthorizedException + */ + private function resolveSystemUrlRedirect(FormActionEvent $event): void + { + /** @var \Symfony\Component\HttpFoundation\RedirectResponse $response */ + $response = $event->getResponse(); + + if (!$response instanceof RedirectResponse) { + return; + } + + $params = $this->router->match($response->getTargetUrl()); + + if (!in_array('locationId', $params)) { + return; + } + + $location = $this->locationService->loadLocation($params['locationId']); + + $event->setResponse(new RedirectResponse($this->urlAliasService->reverseLookup($location)->path)); + } +}