Skip to content

Commit 6216001

Browse files
ViniTouŁukasz Serwatka
authored and
Łukasz Serwatka
committed
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
1 parent b6cc9c8 commit 6216001

File tree

3 files changed

+127
-43
lines changed

3 files changed

+127
-43
lines changed

bundle/Resources/config/services.yml

+4-1
Original file line numberDiff line numberDiff line change
@@ -208,7 +208,6 @@ services:
208208
- '@ezpublish.api.service.content'
209209
- '@ezpublish.api.service.location'
210210
- '@router'
211-
- '@ezpublish.api.service.url_alias'
212211
tags:
213212
- { name: kernel.event_subscriber }
214213

@@ -251,6 +250,10 @@ services:
251250
tags:
252251
- { name: kernel.event_subscriber }
253252

253+
EzSystems\RepositoryForms\Form\Processor\SystemUrlRedirectProcessor:
254+
autowire: true
255+
autoconfigure: true
256+
254257
# Controllers
255258
ezrepoforms.controller.content_edit:
256259
class: "%ezrepoforms.controller.content_edit.class%"

lib/Form/Processor/ContentFormProcessor.php

+15-42
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,6 @@
99

1010
use eZ\Publish\API\Repository\ContentService;
1111
use eZ\Publish\API\Repository\LocationService;
12-
use eZ\Publish\API\Repository\URLAliasService;
1312
use eZ\Publish\API\Repository\Values\Content\Content;
1413
use eZ\Publish\API\Repository\Values\Content\ContentStruct;
1514
use eZ\Publish\API\Repository\Values\Content\Location;
@@ -21,6 +20,7 @@
2120
use EzSystems\RepositoryForms\Event\RepositoryFormEvents;
2221
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
2322
use Symfony\Component\HttpFoundation\RedirectResponse;
23+
use Symfony\Component\Routing\Generator\UrlGeneratorInterface;
2424
use Symfony\Component\Routing\RouterInterface;
2525

2626
/**
@@ -37,9 +37,6 @@ class ContentFormProcessor implements EventSubscriberInterface
3737
/** @var \Symfony\Component\Routing\RouterInterface */
3838
private $router;
3939

40-
/** @var \eZ\Publish\API\Repository\URLAliasService */
41-
private $urlAliasService;
42-
4340
/**
4441
* @param \eZ\Publish\API\Repository\ContentService $contentService
4542
* @param \eZ\Publish\API\Repository\LocationService $locationService
@@ -49,13 +46,11 @@ class ContentFormProcessor implements EventSubscriberInterface
4946
public function __construct(
5047
ContentService $contentService,
5148
LocationService $locationService,
52-
RouterInterface $router,
53-
URLAliasService $urlAliasService
49+
RouterInterface $router
5450
) {
5551
$this->contentService = $contentService;
5652
$this->locationService = $locationService;
5753
$this->router = $router;
58-
$this->urlAliasService = $urlAliasService;
5954
}
6055

6156
/**
@@ -110,7 +105,6 @@ public function processSaveDraft(FormActionEvent $event)
110105
* @throws \eZ\Publish\API\Repository\Exceptions\ContentFieldValidationException
111106
* @throws \eZ\Publish\API\Repository\Exceptions\ContentValidationException
112107
* @throws \eZ\Publish\API\Repository\Exceptions\InvalidArgumentException
113-
* @throws \eZ\Publish\API\Repository\Exceptions\NotFoundException
114108
* @throws \eZ\Publish\API\Repository\Exceptions\UnauthorizedException
115109
* @throws \eZ\Publish\Core\Base\Exceptions\InvalidArgumentException
116110
*/
@@ -123,35 +117,19 @@ public function processPublish(FormActionEvent $event)
123117
$draft = $this->saveDraft($data, $form->getConfig()->getOption('languageCode'));
124118
$content = $this->contentService->publishVersion($draft->versionInfo);
125119

126-
$location = $this->locationService->loadLocation($content->contentInfo->mainLocationId);
120+
$redirectUrl = $form['redirectUrlAfterPublish']->getData() ?: $this->router->generate(
121+
'_ezpublishLocation', [
122+
'locationId' => $content->contentInfo->mainLocationId,
123+
]
124+
);
127125

128-
$redirectUrl = $form['redirectUrlAfterPublish']->getData() ?: $this->getSystemUrl($location, [$content->versionInfo->initialLanguageCode]);
129126
$event->setResponse(new RedirectResponse($redirectUrl));
130127
}
131128

132-
/**
133-
* @param \eZ\Publish\API\Repository\Values\Content\Location $location
134-
* @param array $prioritizedLanguageList
135-
*
136-
* @return string
137-
*
138-
* @throws \eZ\Publish\API\Repository\Exceptions\NotFoundException
139-
*/
140-
private function getSystemUrl(Location $location, array $prioritizedLanguageList): string
141-
{
142-
return $this->urlAliasService->reverseLookup(
143-
$location,
144-
null,
145-
true,
146-
$prioritizedLanguageList
147-
)->path;
148-
}
149-
150129
/**
151130
* @param \EzSystems\RepositoryForms\Event\FormActionEvent $event
152131
*
153132
* @throws \eZ\Publish\API\Repository\Exceptions\BadStateException
154-
* @throws \eZ\Publish\API\Repository\Exceptions\NotFoundException
155133
* @throws \eZ\Publish\API\Repository\Exceptions\UnauthorizedException
156134
*/
157135
public function processCancel(FormActionEvent $event)
@@ -160,14 +138,10 @@ public function processCancel(FormActionEvent $event)
160138
$data = $event->getData();
161139

162140
if ($data->isNew()) {
163-
$parentLocation = $this->locationService->loadLocation(
164-
$data->getLocationStructs()[0]->parentLocationId
165-
);
166-
$url = $this->getSystemUrl(
167-
$parentLocation,
168-
[$data->mainLanguageCode, $parentLocation->contentInfo->mainLanguageCode]
169-
);
170-
$response = new RedirectResponse($url);
141+
$response = new RedirectResponse($this->router->generate(
142+
'_ezpublishLocation',
143+
['locationId' => $data->getLocationStructs()[0]->parentLocationId]
144+
));
171145
$event->setResponse($response);
172146

173147
return;
@@ -187,11 +161,10 @@ public function processCancel(FormActionEvent $event)
187161
$this->contentService->deleteVersion($versionInfo);
188162
}
189163

190-
$locationToRedirect = $this->locationService->loadLocation($redirectionLocationId);
191-
192-
$url = $this->getSystemUrl(
193-
$locationToRedirect,
194-
[$locationToRedirect->contentInfo->mainLanguageCode]
164+
$url = $this->router->generate(
165+
'_ezpublishLocation',
166+
['locationId' => $redirectionLocationId],
167+
UrlGeneratorInterface::ABSOLUTE_URL
195168
);
196169

197170
$event->setResponse(new RedirectResponse($url));
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,108 @@
1+
<?php
2+
3+
/**
4+
* @copyright Copyright (C) eZ Systems AS. All rights reserved.
5+
* @license For full copyright and license information view LICENSE file distributed with this source code.
6+
*/
7+
declare(strict_types=1);
8+
9+
namespace EzSystems\RepositoryForms\Form\Processor;
10+
11+
use eZ\Publish\API\Repository\LocationService;
12+
use eZ\Publish\API\Repository\URLAliasService;
13+
use EzSystems\RepositoryForms\Event\FormActionEvent;
14+
use EzSystems\RepositoryForms\Event\RepositoryFormEvents;
15+
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
16+
use Symfony\Component\HttpFoundation\RedirectResponse;
17+
use Symfony\Component\Routing\RouterInterface;
18+
19+
class SystemUrlRedirectProcessor implements EventSubscriberInterface
20+
{
21+
/** @var \Symfony\Component\Routing\RouterInterface */
22+
private $router;
23+
24+
/** @var \eZ\Publish\API\Repository\URLAliasService */
25+
private $urlAliasService;
26+
27+
/** @var \eZ\Publish\API\Repository\LocationService */
28+
private $locationService;
29+
30+
/**
31+
* @param \Symfony\Component\Routing\RouterInterface $router
32+
* @param \eZ\Publish\API\Repository\URLAliasService $urlAliasService
33+
* @param \eZ\Publish\API\Repository\LocationService $locationService
34+
* @param array $siteaccessGroups
35+
*/
36+
public function __construct(
37+
RouterInterface $router,
38+
URLAliasService $urlAliasService,
39+
LocationService $locationService
40+
) {
41+
$this->router = $router;
42+
$this->urlAliasService = $urlAliasService;
43+
$this->locationService = $locationService;
44+
}
45+
46+
/**
47+
* @return array
48+
*/
49+
public static function getSubscribedEvents(): array
50+
{
51+
return [
52+
RepositoryFormEvents::CONTENT_PUBLISH => ['processRedirectAfterPublish', 2],
53+
RepositoryFormEvents::CONTENT_CANCEL => ['processRedirectAfterCancel', 2],
54+
];
55+
}
56+
57+
/**
58+
* @param \EzSystems\RepositoryForms\Event\FormActionEvent $event
59+
*
60+
* @throws \eZ\Publish\API\Repository\Exceptions\NotFoundException
61+
* @throws \eZ\Publish\API\Repository\Exceptions\UnauthorizedException
62+
*/
63+
public function processRedirectAfterPublish(FormActionEvent $event): void
64+
{
65+
if ($event->getForm()['redirectUrlAfterPublish']->getData()) {
66+
return;
67+
}
68+
69+
$this->resolveSystemUrlRedirect($event);
70+
}
71+
72+
/**
73+
* @param \EzSystems\RepositoryForms\Event\FormActionEvent $event
74+
*
75+
* @throws \eZ\Publish\API\Repository\Exceptions\NotFoundException
76+
* @throws \eZ\Publish\API\Repository\Exceptions\UnauthorizedException
77+
*/
78+
public function processRedirectAfterCancel(FormActionEvent $event): void
79+
{
80+
$this->resolveSystemUrlRedirect($event);
81+
}
82+
83+
/**
84+
* @param \EzSystems\RepositoryForms\Event\FormActionEvent $event
85+
*
86+
* @throws \eZ\Publish\API\Repository\Exceptions\NotFoundException
87+
* @throws \eZ\Publish\API\Repository\Exceptions\UnauthorizedException
88+
*/
89+
private function resolveSystemUrlRedirect(FormActionEvent $event): void
90+
{
91+
/** @var \Symfony\Component\HttpFoundation\RedirectResponse $response */
92+
$response = $event->getResponse();
93+
94+
if (!$response instanceof RedirectResponse) {
95+
return;
96+
}
97+
98+
$params = $this->router->match($response->getTargetUrl());
99+
100+
if (!in_array('locationId', $params)) {
101+
return;
102+
}
103+
104+
$location = $this->locationService->loadLocation($params['locationId']);
105+
106+
$event->setResponse(new RedirectResponse($this->urlAliasService->reverseLookup($location)->path));
107+
}
108+
}

0 commit comments

Comments
 (0)