Skip to content

Commit 3d57022

Browse files
author
René Hrdina
committed
feat: properly support translated info collection fields
1 parent 54d9726 commit 3d57022

File tree

3 files changed

+33
-3
lines changed

3 files changed

+33
-3
lines changed

bundle/Ibexa/ContentForms/InformationCollectionMapper.php

+1-1
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ final class InformationCollectionMapper
2121
*/
2222
public function mapToFormData(Content $content, Location $location, ContentType $contentType): InformationCollectionStruct
2323
{
24-
$fields = $content->getFieldsByLanguage($content->contentInfo->mainLanguageCode);
24+
$fields = $content->getFieldsByLanguage();
2525

2626
$informationCollectionFields = [];
2727

lib/Handler.php

+31-2
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44

55
namespace Netgen\InformationCollection;
66

7+
use Ibexa\Contracts\Core\SiteAccess\ConfigResolverInterface;
78
use Ibexa\Contracts\Core\Repository\ContentTypeService;
89
use Ibexa\Contracts\Core\Repository\Values\Content\Content;
910
use Ibexa\Contracts\Core\Repository\Values\Content\Location;
@@ -24,11 +25,19 @@ final class Handler
2425

2526
private EventDispatcherInterface $eventDispatcher;
2627

27-
public function __construct(FormFactoryInterface $formFactory, ContentTypeService $contentTypeService, EventDispatcherInterface $eventDispatcher)
28+
private ConfigResolverInterface $configResolver;
29+
30+
public function __construct(
31+
FormFactoryInterface $formFactory,
32+
ContentTypeService $contentTypeService,
33+
EventDispatcherInterface $eventDispatcher,
34+
ConfigResolverInterface $configResolver
35+
)
2836
{
2937
$this->formFactory = $formFactory;
3038
$this->contentTypeService = $contentTypeService;
3139
$this->eventDispatcher = $eventDispatcher;
40+
$this->configResolver = $configResolver;
3241
}
3342

3443
public function getForm(Content $content, Location $location): FormInterface
@@ -40,11 +49,31 @@ public function getForm(Content $content, Location $location): FormInterface
4049
$data = $informationCollectionMapper->mapToFormData($content, $location, $contentType);
4150

4251
return $this->formFactory->create(InformationCollectionType::class, $data, [
43-
'languageCode' => $content->contentInfo->mainLanguageCode,
52+
'languageCode' => $this->determineLanguageToLoad($content),
4453
'mainLanguageCode' => $content->contentInfo->mainLanguageCode,
4554
]);
4655
}
4756

57+
/**
58+
* @param Content $content
59+
* @return string
60+
*/
61+
private function determineLanguageToLoad(Content $content): string
62+
{
63+
$versionInfo = $content->getVersionInfo();
64+
65+
$siteAccessLanguagesCodes = (array)$this->configResolver->getParameter('languages');
66+
foreach($siteAccessLanguagesCodes as $languageCode)
67+
{
68+
if (in_array($languageCode, $versionInfo->languageCodes, true))
69+
{
70+
return $languageCode;
71+
}
72+
}
73+
74+
return $content->contentInfo->mainLanguageCode;
75+
}
76+
4877
public function handle(InformationCollectionStruct $struct, array $options): void
4978
{
5079
$event = new InformationCollected($struct, $options);

lib/Resources/config/services.yml

+1
Original file line numberDiff line numberDiff line change
@@ -95,6 +95,7 @@ services:
9595
- "@form.factory"
9696
- "@ibexa.api.service.content_type"
9797
- "@event_dispatcher"
98+
- "@ibexa.config.resolver"
9899

99100
Netgen\InformationCollection\Core\Service\CaptchaService:
100101
autowire: true

0 commit comments

Comments
 (0)