Skip to content

Commit

Permalink
Revert "EZP-31086: Redirect based on router, not url alias (#323)" (#343
Browse files Browse the repository at this point in the history
)

This reverts commit e5756b0.
  • Loading branch information
mnocon authored Sep 16, 2020
1 parent e5756b0 commit 5f6a8f4
Show file tree
Hide file tree
Showing 3 changed files with 116 additions and 3 deletions.
4 changes: 4 additions & 0 deletions bundle/Resources/config/services.yml
Original file line number Diff line number Diff line change
Expand Up @@ -252,6 +252,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
7 changes: 4 additions & 3 deletions lib/Form/Processor/ContentFormProcessor.php
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ class ContentFormProcessor implements EventSubscriberInterface
* @param \eZ\Publish\API\Repository\ContentService $contentService
* @param \eZ\Publish\API\Repository\LocationService $locationService
* @param \Symfony\Component\Routing\RouterInterface $router
* @param \eZ\Publish\API\Repository\URLAliasService $urlAliasService
*/
public function __construct(
ContentService $contentService,
Expand Down Expand Up @@ -124,7 +125,7 @@ public function processPublish(FormActionEvent $event)
$event->setPayload('is_new', $draft->contentInfo->isDraft());

$redirectUrl = $form['redirectUrlAfterPublish']->getData() ?: $this->router->generate(
'ez_urlalias', [
'_ezpublishLocation', [
'locationId' => $content->contentInfo->mainLocationId,
]
);
Expand All @@ -145,7 +146,7 @@ public function processCancel(FormActionEvent $event)

if ($data->isNew()) {
$response = new RedirectResponse($this->router->generate(
'ez_urlalias',
'_ezpublishLocation',
['locationId' => $data->getLocationStructs()[0]->parentLocationId]
));
$event->setResponse($response);
Expand All @@ -170,7 +171,7 @@ public function processCancel(FormActionEvent $event)
}

$url = $this->router->generate(
'ez_urlalias',
'_ezpublishLocation',
['locationId' => $redirectionLocationId],
UrlGeneratorInterface::ABSOLUTE_URL
);
Expand Down
108 changes: 108 additions & 0 deletions lib/Form/Processor/SystemUrlRedirectProcessor.php
Original file line number Diff line number Diff line change
@@ -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 5f6a8f4

Please # to comment.