Skip to content

Commit

Permalink
EZP-28850: Add optional location parameter to draft edit for more app…
Browse files Browse the repository at this point in the history
…ropriate redirection (ezsystems#214)
  • Loading branch information
webhdx authored and Łukasz Serwatka committed Mar 8, 2018
1 parent 4d56b12 commit b79b0b8
Show file tree
Hide file tree
Showing 4 changed files with 34 additions and 16 deletions.
38 changes: 24 additions & 14 deletions bundle/Controller/ContentEditController.php
Expand Up @@ -24,6 +24,7 @@
use EzSystems\RepositoryForms\Form\Type\Content\ContentDraftCreateType;
use EzSystems\RepositoryForms\Form\Type\Content\ContentEditType;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;

class ContentEditController extends Controller
{
Expand Down Expand Up @@ -69,9 +70,9 @@ public function __construct(
* @param int $contentTypeIdentifier ContentType id to create
* @param string $language Language code to create the content in (eng-GB, ger-DE, ...))
* @param int $parentLocationId Location the content should be a child of
* @param \Symfony\Component\HttpFoundation\Request $request
* @param Request $request
*
* @return \EzSystems\RepositoryForms\Content\View\ContentCreateView|\Symfony\Component\HttpFoundation\Response
* @return \EzSystems\RepositoryForms\Content\View\ContentCreateView|Response
*
* @throws \eZ\Publish\Core\Base\Exceptions\InvalidArgumentType
* @throws \eZ\Publish\API\Repository\Exceptions\UnauthorizedException
Expand Down Expand Up @@ -115,9 +116,9 @@ public function createWithoutDraftAction($contentTypeIdentifier, $language, $par
* @param int $fromVersionNo
* @param string $fromLanguage
* @param string $toLanguage
* @param \Symfony\Component\HttpFoundation\Request $request
* @param Request $request
*
* @return \EzSystems\RepositoryForms\Content\View\ContentCreateDraftView|\Symfony\Component\HttpFoundation\Response
* @return \EzSystems\RepositoryForms\Content\View\ContentCreateDraftView|Response
*
* @throws \eZ\Publish\API\Repository\Exceptions\UnauthorizedException
* @throws \eZ\Publish\API\Repository\Exceptions\NotFoundException
Expand Down Expand Up @@ -172,23 +173,26 @@ public function createContentDraftAction(
*
* @param int $contentId ContentType id to create
* @param int $versionNo Version number the version should be created from. Defaults to the currently published one.
* @param \Symfony\Component\HttpFoundation\Request $request
* @param Request $request
* @param string $language Language code to create the version in (eng-GB, ger-DE, ...))
* @param int|null $locationId
*
* @return \EzSystems\RepositoryForms\Content\View\ContentEditView|\Symfony\Component\HttpFoundation\Response
*
* @throws \eZ\Publish\Core\Base\Exceptions\InvalidArgumentType
* @throws \eZ\Publish\API\Repository\Exceptions\UnauthorizedException
* @throws \eZ\Publish\API\Repository\Exceptions\NotFoundException
* @throws \eZ\Publish\Core\Base\Exceptions\BadStateException If the version isn't editable, or if there is no editable version.
* @return ContentEditView|Response
* @throws BadStateException If the version isn't editable, or if there is no editable version.
*/
public function editContentDraftAction($contentId, $versionNo = null, Request $request, $language = null)
{
public function editContentDraftAction(
$contentId,
$versionNo = null,
Request $request,
$language = null,
$locationId = null
) {
$draft = $this->contentService->loadContent($contentId, [$language], $versionNo);
if ($draft->getVersionInfo()->status !== VersionInfo::STATUS_DRAFT) {
throw new BadStateException('Version status', 'status is not draft');
}

$referrerLocation = $this->locationService->loadLocation($locationId ?? $draft->contentInfo->mainLocationId);
$language = $language ?: $draft->getVersionInfo()->getContentInfo()->mainLanguageCode;
$language = $this->languageService->loadLanguage($language);
$contentType = $this->contentTypeService->loadContentType($draft->contentInfo->contentTypeId);
Expand All @@ -212,7 +216,12 @@ public function editContentDraftAction($contentId, $versionNo = null, Request $r
$form->handleRequest($request);

if ($form->isValid() && null !== $form->getClickedButton()) {
$this->contentActionDispatcher->dispatchFormAction($form, $contentUpdate, $form->getClickedButton()->getName());
$this->contentActionDispatcher->dispatchFormAction(
$form,
$contentUpdate,
$form->getClickedButton()->getName(),
['referrerLocation' => $referrerLocation]
);
if ($response = $this->contentActionDispatcher->getResponse()) {
return $response;
}
Expand All @@ -223,6 +232,7 @@ public function editContentDraftAction($contentId, $versionNo = null, Request $r
'language' => $language,
'content' => $draft,
'contentType' => $contentType,
'location' => $referrerLocation,
]);
}

Expand Down
5 changes: 3 additions & 2 deletions bundle/Resources/config/routing.yml
Expand Up @@ -6,10 +6,11 @@ ez_content_create_no_draft:
expose: true

ez_content_draft_edit:
path: /content/edit/draft/{contentId}/{versionNo}/{language}
path: /content/edit/draft/{contentId}/{versionNo}/{language}/{locationId}
defaults:
_controller: ez_content_edit:editContentDraftAction
language: null
language: ~
locationId: ~
options:
expose: true

Expand Down
3 changes: 3 additions & 0 deletions lib/Form/ActionDispatcher/ContentDispatcher.php
Expand Up @@ -7,13 +7,16 @@
*/
namespace EzSystems\RepositoryForms\Form\ActionDispatcher;

use eZ\Publish\API\Repository\Values\Content\Location;
use EzSystems\RepositoryForms\Event\RepositoryFormEvents;
use Symfony\Component\OptionsResolver\OptionsResolver;

class ContentDispatcher extends AbstractActionDispatcher
{
protected function configureOptions(OptionsResolver $resolver)
{
$resolver->setDefined(['referrerLocation']);
$resolver->setAllowedTypes('referrerLocation', [Location::class, null]);
}

protected function getActionEventBaseName()
Expand Down
4 changes: 4 additions & 0 deletions lib/Form/Processor/ContentFormProcessor.php
Expand Up @@ -64,11 +64,13 @@ public function processSaveDraft(FormActionEvent $event)
$formConfig = $form->getConfig();
$languageCode = $formConfig->getOption('languageCode');
$draft = $this->saveDraft($data, $languageCode);
$referrerLocation = $event->getOption('referrerLocation');

$defaultUrl = $this->router->generate('ez_content_draft_edit', [
'contentId' => $draft->id,
'versionNo' => $draft->getVersionInfo()->versionNo,
'language' => $languageCode,
'locationId' => null !== $referrerLocation ? $referrerLocation->id : null,
]);
$event->setResponse(new RedirectResponse($formConfig->getAction() ?: $defaultUrl));
}
Expand Down Expand Up @@ -136,11 +138,13 @@ public function processCreateDraft(FormActionEvent $event)
$contentInfo = $this->contentService->loadContentInfo($createContentDraft->contentId);
$versionInfo = $this->contentService->loadVersionInfo($contentInfo, $createContentDraft->fromVersionNo);
$contentDraft = $this->contentService->createContentDraft($contentInfo, $versionInfo);
$referrerLocation = $event->getOption('referrerLocation');

$contentEditUrl = $this->router->generate('ez_content_draft_edit', [
'contentId' => $contentDraft->id,
'versionNo' => $contentDraft->getVersionInfo()->versionNo,
'language' => $contentDraft->contentInfo->mainLanguageCode,
'locationId' => null !== $referrerLocation ? $referrerLocation->id : null,
]);
$event->setResponse(new RedirectResponse($contentEditUrl));
}
Expand Down

0 comments on commit b79b0b8

Please sign in to comment.