From 005b67cd006b6227f615a84816541115dc1faa91 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ren=C3=A9=20Hrdina?= Date: Mon, 6 Mar 2023 09:36:03 +0000 Subject: [PATCH] feat: properly support translated info collection fields --- .../InformationCollectionMapper.php | 2 +- lib/Handler.php | 33 +++++++++++++++++-- lib/Resources/config/services.yml | 1 + 3 files changed, 33 insertions(+), 3 deletions(-) diff --git a/bundle/EzPlatform/RepositoryForms/InformationCollectionMapper.php b/bundle/EzPlatform/RepositoryForms/InformationCollectionMapper.php index 9cb45293..e3800cfb 100644 --- a/bundle/EzPlatform/RepositoryForms/InformationCollectionMapper.php +++ b/bundle/EzPlatform/RepositoryForms/InformationCollectionMapper.php @@ -26,7 +26,7 @@ final class InformationCollectionMapper */ public function mapToFormData(Content $content, Location $location, ContentType $contentType) { - $fields = $content->getFieldsByLanguage($content->contentInfo->mainLanguageCode); + $fields = $content->getFieldsByLanguage(); $informationCollectionFields = []; diff --git a/lib/Handler.php b/lib/Handler.php index b5840d84..63d757c8 100644 --- a/lib/Handler.php +++ b/lib/Handler.php @@ -5,6 +5,7 @@ use eZ\Publish\API\Repository\ContentTypeService; use eZ\Publish\API\Repository\Values\Content\Content; use eZ\Publish\API\Repository\Values\Content\Location; +use eZ\Publish\Core\MVC\ConfigResolverInterface; use Netgen\InformationCollection\API\Events; use Netgen\InformationCollection\API\Value\Event\InformationCollected; use Netgen\InformationCollection\API\Value\InformationCollectionStruct; @@ -31,11 +32,19 @@ final class Handler */ private $eventDispatcher; - public function __construct(FormFactoryInterface $formFactory, ContentTypeService $contentTypeService, EventDispatcherInterface $eventDispatcher) + private ConfigResolverInterface $configResolver; + + public function __construct( + FormFactoryInterface $formFactory, + ContentTypeService $contentTypeService, + EventDispatcherInterface $eventDispatcher, + ConfigResolverInterface $configResolver + ) { $this->formFactory = $formFactory; $this->contentTypeService = $contentTypeService; $this->eventDispatcher = $eventDispatcher; + $this->configResolver = $configResolver; } public function getForm(Content $content, Location $location): FormInterface @@ -47,11 +56,31 @@ public function getForm(Content $content, Location $location): FormInterface $data = $informationCollectionMapper->mapToFormData($content, $location, $contentType); return $this->formFactory->create(InformationCollectionType::class, $data, [ - 'languageCode' => $content->contentInfo->mainLanguageCode, + 'languageCode' => $this->determineLanguageToLoad($content), 'mainLanguageCode' => $content->contentInfo->mainLanguageCode, ]); } + /** + * @param Content $content + * @return string + */ + private function determineLanguageToLoad(Content $content) + { + $versionInfo = $content->getVersionInfo(); + + $siteAccessLanguagesCodes = (array)$this->configResolver->getParameter('languages'); + foreach($siteAccessLanguagesCodes as $languageCode) + { + if (in_array($languageCode, $versionInfo->languageCodes)) + { + return $languageCode; + } + } + + return $content->contentInfo->mainLanguageCode; + } + public function handle(InformationCollectionStruct $struct, array $options): void { $event = new InformationCollected($struct, $options); diff --git a/lib/Resources/config/services.yml b/lib/Resources/config/services.yml index 84e49dec..7c7d7173 100644 --- a/lib/Resources/config/services.yml +++ b/lib/Resources/config/services.yml @@ -84,6 +84,7 @@ services: - "@form.factory" - "@ezpublish.api.service.content_type" - "@event_dispatcher" + - "@ezpublish.config.resolver" Netgen\InformationCollection\Core\Service\CaptchaService: autowire: true