Skip to content

Commit

Permalink
EZP-29617: No route found after adding new translation (#256)
Browse files Browse the repository at this point in the history
* EZP-29617: No route found after adding new translation

* EZP-29617: No route found after adding new translation
  • Loading branch information
ViniTou authored and Łukasz Serwatka committed Oct 11, 2018
1 parent b6cc9c8 commit 6216001
Show file tree
Hide file tree
Showing 3 changed files with 127 additions and 43 deletions.
5 changes: 4 additions & 1 deletion bundle/Resources/config/services.yml
Expand Up @@ -208,7 +208,6 @@ services:
- '@ezpublish.api.service.content'
- '@ezpublish.api.service.location'
- '@router'
- '@ezpublish.api.service.url_alias'
tags:
- { name: kernel.event_subscriber }

Expand Down Expand Up @@ -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%"
Expand Down
57 changes: 15 additions & 42 deletions lib/Form/Processor/ContentFormProcessor.php
Expand Up @@ -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;
Expand All @@ -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;

/**
Expand All @@ -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
Expand All @@ -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;
}

/**
Expand Down Expand Up @@ -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
*/
Expand All @@ -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', [

This comment has been minimized.

Copy link
@wizhippo

wizhippo Aug 2, 2019

Contributor

Is this route not deprecated. Should we use ez_urlalias instead?

'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)
Expand All @@ -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;
Expand All @@ -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));
Expand Down
108 changes: 108 additions & 0 deletions lib/Form/Processor/SystemUrlRedirectProcessor.php
@@ -0,0 +1,108 @@
<?php

/**
* @copyright Copyright (C) eZ Systems AS. All rights reserved.
* @license For full copyright and license information view LICENSE file distributed with this source code.
*/
declare(strict_types=1);

namespace EzSystems\RepositoryForms\Form\Processor;

use eZ\Publish\API\Repository\LocationService;
use eZ\Publish\API\Repository\URLAliasService;
use EzSystems\RepositoryForms\Event\FormActionEvent;
use EzSystems\RepositoryForms\Event\RepositoryFormEvents;
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
use Symfony\Component\HttpFoundation\RedirectResponse;
use Symfony\Component\Routing\RouterInterface;

class SystemUrlRedirectProcessor implements EventSubscriberInterface
{
/** @var \Symfony\Component\Routing\RouterInterface */
private $router;

/** @var \eZ\Publish\API\Repository\URLAliasService */
private $urlAliasService;

/** @var \eZ\Publish\API\Repository\LocationService */
private $locationService;

/**
* @param \Symfony\Component\Routing\RouterInterface $router
* @param \eZ\Publish\API\Repository\URLAliasService $urlAliasService
* @param \eZ\Publish\API\Repository\LocationService $locationService
* @param array $siteaccessGroups
*/
public function __construct(
RouterInterface $router,
URLAliasService $urlAliasService,
LocationService $locationService
) {
$this->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));
}
}

0 comments on commit 6216001

Please sign in to comment.