From 7c08ebbc8956f1d59dd6b41edbc7309566d79ffb Mon Sep 17 00:00:00 2001 From: Maciej Kobus Date: Tue, 29 Nov 2022 22:28:53 +0100 Subject: [PATCH 1/5] IBX-3265: Added optional `versionNo` parameter to `ezplatform.content.translate` route --- src/bundle/Resources/config/routing.yaml | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/src/bundle/Resources/config/routing.yaml b/src/bundle/Resources/config/routing.yaml index 682891d70d..e1da825f45 100644 --- a/src/bundle/Resources/config/routing.yaml +++ b/src/bundle/Resources/config/routing.yaml @@ -564,11 +564,12 @@ ibexa.content.translate_with_location.proxy: fromLanguageCode: ~ ezplatform.content.translate: - path: /content/{contentId}/translate/{toLanguageCode}/{fromLanguageCode} + path: /content/{contentId}/translate/{toLanguageCode}/{fromLanguageCode}/{versionNo} methods: ['GET', 'POST'] defaults: _controller: 'EzSystems\EzPlatformAdminUiBundle\Controller\ContentEditController::translateAction' fromLanguageCode: ~ + versionNo: ~ ibexa.content.translate_with_location: path: /content/{contentId}/location/{locationId}/translate/{toLanguageCode}/{fromLanguageCode} From 1c739078ae4948271640c4f9eee884545e5c90b3 Mon Sep 17 00:00:00 2001 From: Maciej Kobus Date: Tue, 29 Nov 2022 22:29:59 +0100 Subject: [PATCH 2/5] IBX-3265: Made autosave use `ezplatform.content.translate` route when translating content --- src/bundle/Controller/ContentEditController.php | 2 +- src/lib/EventListener/ContentProxyCreateDraftListener.php | 5 +++-- 2 files changed, 4 insertions(+), 3 deletions(-) diff --git a/src/bundle/Controller/ContentEditController.php b/src/bundle/Controller/ContentEditController.php index cd573975b6..e75d3de5a0 100644 --- a/src/bundle/Controller/ContentEditController.php +++ b/src/bundle/Controller/ContentEditController.php @@ -57,7 +57,7 @@ public function proxyTranslateAction( return $event->getResponse(); } - // Fallback to "translate" + // Fallback to default translation interface (no draft) return $this->redirectToRoute('ezplatform.content.translate', [ 'contentId' => $contentId, 'fromLanguageCode' => $fromLanguageCode, diff --git a/src/lib/EventListener/ContentProxyCreateDraftListener.php b/src/lib/EventListener/ContentProxyCreateDraftListener.php index 2bafb4023c..1d26a3fb03 100644 --- a/src/lib/EventListener/ContentProxyCreateDraftListener.php +++ b/src/lib/EventListener/ContentProxyCreateDraftListener.php @@ -132,11 +132,12 @@ public function translate(ContentProxyTranslateEvent $event): void ); $response = new RedirectResponse( - $this->router->generate('ezplatform.content.draft.edit', [ + $this->router->generate('ezplatform.content.translate', [ 'contentId' => $contentDraft->id, 'versionNo' => $contentDraft->getVersionInfo()->versionNo, - 'language' => $toLanguageCode, 'locationId' => $event->getLocationId(), + 'fromLanguageCode' => $fromLanguageCode, + 'toLanguageCode' => $toLanguageCode, ]) ); From daaa8713b035123583c6038a187abacc3350b6c5 Mon Sep 17 00:00:00 2001 From: Maciej Kobus Date: Tue, 29 Nov 2022 22:32:04 +0100 Subject: [PATCH 3/5] IBX-3265: Made translation use existing draft when requested --- src/lib/Form/Data/ContentTranslationData.php | 7 +++++-- src/lib/Form/Data/FormMapper/ContentTranslationMapper.php | 6 +++++- src/lib/Form/Processor/TranslationFormProcessor.php | 8 +++++++- src/lib/View/Builder/ContentTranslateViewBuilder.php | 3 ++- src/lib/View/Filter/ContentTranslateViewFilter.php | 5 ++++- 5 files changed, 23 insertions(+), 6 deletions(-) diff --git a/src/lib/Form/Data/ContentTranslationData.php b/src/lib/Form/Data/ContentTranslationData.php index 570fd78107..e1debe26d1 100644 --- a/src/lib/Form/Data/ContentTranslationData.php +++ b/src/lib/Form/Data/ContentTranslationData.php @@ -8,11 +8,11 @@ namespace EzSystems\EzPlatformAdminUi\Form\Data; -use eZ\Publish\Core\Repository\Values\Content\ContentUpdateStruct; use EzSystems\EzPlatformContentForms\Data\Content\FieldData; +use EzSystems\EzPlatformContentForms\Data\ContentTranslationData as BaseContentTranslationData; use Symfony\Component\Validator\Constraints as Assert; -class ContentTranslationData extends ContentUpdateStruct implements NewnessCheckable +class ContentTranslationData extends BaseContentTranslationData implements NewnessCheckable { /** * @var \EzSystems\EzPlatformContentForms\Data\Content\FieldData[] @@ -23,6 +23,9 @@ class ContentTranslationData extends ContentUpdateStruct implements NewnessCheck /** @var \eZ\Publish\API\Repository\Values\Content\Content */ protected $content; + /** @var \eZ\Publish\API\Repository\Values\Content\VersionInfo */ + protected $versionInfo; + /** @var \eZ\Publish\API\Repository\Values\ContentType\ContentType */ protected $contentType; diff --git a/src/lib/Form/Data/FormMapper/ContentTranslationMapper.php b/src/lib/Form/Data/FormMapper/ContentTranslationMapper.php index 238d8edc64..3fbbb549f1 100644 --- a/src/lib/Form/Data/FormMapper/ContentTranslationMapper.php +++ b/src/lib/Form/Data/FormMapper/ContentTranslationMapper.php @@ -49,7 +49,11 @@ public function mapToFormData(ValueObject $content, array $params = []) /** @var \eZ\Publish\API\Repository\Values\ContentType\ContentType $contentType */ $contentType = $params['contentType']; - $data = new ContentTranslationData(['content' => $content, 'contentType' => $contentType]); + $data = new ContentTranslationData([ + 'content' => $content, + 'versionInfo' => $content->getVersionInfo(), + 'contentType' => $contentType, + ]); $data->initialLanguageCode = $language->languageCode; foreach ($content->getFieldsByLanguage() as $field) { diff --git a/src/lib/Form/Processor/TranslationFormProcessor.php b/src/lib/Form/Processor/TranslationFormProcessor.php index 5ba4552580..6ba58fb0e7 100644 --- a/src/lib/Form/Processor/TranslationFormProcessor.php +++ b/src/lib/Form/Processor/TranslationFormProcessor.php @@ -7,6 +7,7 @@ namespace EzSystems\EzPlatformAdminUi\Form\Processor; use eZ\Publish\API\Repository\ContentService; +use eZ\Publish\API\Repository\Values\Content\VersionInfo; use EzSystems\EzPlatformAdminUi\Form\Data\ContentTranslationData; use EzSystems\EzPlatformContentForms\Data\Content\ContentUpdateData; use EzSystems\EzPlatformContentForms\Data\Content\FieldData; @@ -54,7 +55,12 @@ public function createContentDraft(FormActionEvent $event): void return; } - $contentDraft = $this->contentService->createContentDraft($data->content->contentInfo); + if ($data->content->getVersionInfo()->status === VersionInfo::STATUS_DRAFT) { + $contentDraft = $data->content; + } else { + $contentDraft = $this->contentService->createContentDraft($data->content->contentInfo); + } + $fields = array_filter($data->fieldsData, static function (FieldData $fieldData) use ($contentDraft, $data) { $mainLanguageCode = $contentDraft->getVersionInfo()->getContentInfo()->mainLanguageCode; diff --git a/src/lib/View/Builder/ContentTranslateViewBuilder.php b/src/lib/View/Builder/ContentTranslateViewBuilder.php index ec43318194..19abe46530 100644 --- a/src/lib/View/Builder/ContentTranslateViewBuilder.php +++ b/src/lib/View/Builder/ContentTranslateViewBuilder.php @@ -256,7 +256,8 @@ private function resolveContent(array $parameters, ?Location $location, ?Languag return $this->loadContent( (int) $parameters['contentId'], - null !== $language ? [$language->languageCode] : [] + null !== $language ? [$language->languageCode] : [], + (int) $parameters['versionNo'] ?: null ); } diff --git a/src/lib/View/Filter/ContentTranslateViewFilter.php b/src/lib/View/Filter/ContentTranslateViewFilter.php index 343db22764..55b17b71f4 100644 --- a/src/lib/View/Filter/ContentTranslateViewFilter.php +++ b/src/lib/View/Filter/ContentTranslateViewFilter.php @@ -95,9 +95,11 @@ public function handleContentTranslateForm(FilterViewBuilderParametersEvent $eve $request = $event->getRequest(); $languageCode = $request->attributes->get('toLanguageCode'); $baseLanguageCode = $request->attributes->get('fromLanguageCode'); + $versionNo = $request->attributes->getInt('versionNo'); $content = $this->contentService->loadContent( (int)$request->attributes->get('contentId'), - null !== $baseLanguageCode ? [$baseLanguageCode] : null + null !== $baseLanguageCode ? [$baseLanguageCode] : null, + $versionNo ?: null ); $contentType = $this->contentTypeService->loadContentType( $content->getContentType()->id, @@ -175,6 +177,7 @@ private function resolveContentTranslateForm( 'content' => $content, 'contentUpdateStruct' => $contentUpdate, 'drafts_enabled' => true, + 'intent' => 'translate', ] ); } From 45079670dbfe48e45946b9860f28208c71db459c Mon Sep 17 00:00:00 2001 From: Maciej Kobus Date: Wed, 30 Nov 2022 08:54:06 +0100 Subject: [PATCH 4/5] IBX-3265: Fixed unit tests --- .../EventListener/ContentProxyCreateDraftListenerTest.php | 5 +++-- .../Form/Data/FormMapper/ContentTranslationMapperTest.php | 3 +++ 2 files changed, 6 insertions(+), 2 deletions(-) diff --git a/src/lib/Tests/EventListener/ContentProxyCreateDraftListenerTest.php b/src/lib/Tests/EventListener/ContentProxyCreateDraftListenerTest.php index 0cd7608d55..1ad08d047d 100644 --- a/src/lib/Tests/EventListener/ContentProxyCreateDraftListenerTest.php +++ b/src/lib/Tests/EventListener/ContentProxyCreateDraftListenerTest.php @@ -195,10 +195,11 @@ public function testTranslateContentAutosaveEnabled(): void $router = $this->createMock(RouterInterface::class); $router ->method('generate') - ->with('ezplatform.content.draft.edit', [ + ->with('ezplatform.content.translate', [ 'contentId' => null, 'versionNo' => null, - 'language' => 'pol-PL', + 'toLanguageCode' => 'pol-PL', + 'fromLanguageCode' => 'eng-GB', 'locationId' => null, ]) ->willReturn('redirect_test_url'); diff --git a/src/lib/Tests/Form/Data/FormMapper/ContentTranslationMapperTest.php b/src/lib/Tests/Form/Data/FormMapper/ContentTranslationMapperTest.php index 5678f106f9..e7a5526d7a 100644 --- a/src/lib/Tests/Form/Data/FormMapper/ContentTranslationMapperTest.php +++ b/src/lib/Tests/Form/Data/FormMapper/ContentTranslationMapperTest.php @@ -80,6 +80,7 @@ public function paramsProvider(): array ], new ContentTranslationData([ 'content' => $content_with_1_field, + 'versionInfo' => $content_with_1_field->getVersionInfo(), 'initialLanguageCode' => self::LANGUAGE_CODE, 'fieldsData' => [ $field1->fieldDefIdentifier => new FieldData([ @@ -100,6 +101,7 @@ public function paramsProvider(): array ], new ContentTranslationData([ 'content' => $content_with_1_field, + 'versionInfo' => $content_with_1_field->getVersionInfo(), 'initialLanguageCode' => self::LANGUAGE_CODE, 'fieldsData' => [ $field1->fieldDefIdentifier => new FieldData([ @@ -120,6 +122,7 @@ public function paramsProvider(): array ], new ContentTranslationData([ 'content' => $content_with_3_fields, + 'versionInfo' => $content_with_3_fields->getVersionInfo(), 'initialLanguageCode' => self::LANGUAGE_CODE, 'fieldsData' => [ $field1->fieldDefIdentifier => new FieldData([ From c6cc000726e1eaee4433d9c97526cb2b4ecaf66d Mon Sep 17 00:00:00 2001 From: Maciej Kobus Date: Wed, 30 Nov 2022 11:24:11 +0100 Subject: [PATCH 5/5] IBX-3265: Moved `versionNo` parameter to query param to keep BC --- src/bundle/Resources/config/routing.yaml | 3 +-- src/lib/View/Builder/ContentTranslateViewBuilder.php | 3 +-- src/lib/View/Filter/ContentTranslateViewFilter.php | 11 +++++++++-- 3 files changed, 11 insertions(+), 6 deletions(-) diff --git a/src/bundle/Resources/config/routing.yaml b/src/bundle/Resources/config/routing.yaml index e1da825f45..682891d70d 100644 --- a/src/bundle/Resources/config/routing.yaml +++ b/src/bundle/Resources/config/routing.yaml @@ -564,12 +564,11 @@ ibexa.content.translate_with_location.proxy: fromLanguageCode: ~ ezplatform.content.translate: - path: /content/{contentId}/translate/{toLanguageCode}/{fromLanguageCode}/{versionNo} + path: /content/{contentId}/translate/{toLanguageCode}/{fromLanguageCode} methods: ['GET', 'POST'] defaults: _controller: 'EzSystems\EzPlatformAdminUiBundle\Controller\ContentEditController::translateAction' fromLanguageCode: ~ - versionNo: ~ ibexa.content.translate_with_location: path: /content/{contentId}/location/{locationId}/translate/{toLanguageCode}/{fromLanguageCode} diff --git a/src/lib/View/Builder/ContentTranslateViewBuilder.php b/src/lib/View/Builder/ContentTranslateViewBuilder.php index 19abe46530..ec43318194 100644 --- a/src/lib/View/Builder/ContentTranslateViewBuilder.php +++ b/src/lib/View/Builder/ContentTranslateViewBuilder.php @@ -256,8 +256,7 @@ private function resolveContent(array $parameters, ?Location $location, ?Languag return $this->loadContent( (int) $parameters['contentId'], - null !== $language ? [$language->languageCode] : [], - (int) $parameters['versionNo'] ?: null + null !== $language ? [$language->languageCode] : [] ); } diff --git a/src/lib/View/Filter/ContentTranslateViewFilter.php b/src/lib/View/Filter/ContentTranslateViewFilter.php index 55b17b71f4..0286c7a36d 100644 --- a/src/lib/View/Filter/ContentTranslateViewFilter.php +++ b/src/lib/View/Filter/ContentTranslateViewFilter.php @@ -95,7 +95,7 @@ public function handleContentTranslateForm(FilterViewBuilderParametersEvent $eve $request = $event->getRequest(); $languageCode = $request->attributes->get('toLanguageCode'); $baseLanguageCode = $request->attributes->get('fromLanguageCode'); - $versionNo = $request->attributes->getInt('versionNo'); + $versionNo = $request->query->getInt('versionNo'); $content = $this->contentService->loadContent( (int)$request->attributes->get('contentId'), null !== $baseLanguageCode ? [$baseLanguageCode] : null, @@ -120,7 +120,14 @@ public function handleContentTranslateForm(FilterViewBuilderParametersEvent $eve $content ); - $event->getParameters()->add(['form' => $form->handleRequest($request)]); + $event->getParameters()->add([ + 'form' => $form->handleRequest($request), + 'content' => $content, + 'versionNo' => $versionNo, + 'contentType' => $contentType, + 'toLanguage' => $toLanguage, + 'fromLanguage' => $fromLanguage, + ]); } /**