Skip to content

Commit

Permalink
feat: properly support translated info collection fields
Browse files Browse the repository at this point in the history
  • Loading branch information
René Hrdina committed Mar 6, 2023
1 parent 6cd0a40 commit 005b67c
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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 = [];

Expand Down
33 changes: 31 additions & 2 deletions lib/Handler.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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
Expand All @@ -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);
Expand Down
1 change: 1 addition & 0 deletions lib/Resources/config/services.yml
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,7 @@ services:
- "@form.factory"
- "@ezpublish.api.service.content_type"
- "@event_dispatcher"
- "@ezpublish.config.resolver"

Netgen\InformationCollection\Core\Service\CaptchaService:
autowire: true
Expand Down

0 comments on commit 005b67c

Please sign in to comment.