diff --git a/bundle/Form/FieldTypeHandler/RelationList.php b/bundle/Form/FieldTypeHandler/RelationList.php new file mode 100644 index 0000000..00db88f --- /dev/null +++ b/bundle/Form/FieldTypeHandler/RelationList.php @@ -0,0 +1,113 @@ +repository = $repository; + $this->translationHelper = $translationHelper; + } + + protected function buildFieldForm( + FormBuilderInterface $formBuilder, + FieldDefinition $fieldDefinition, + $languageCode, + Content $content = null + ) + { + $options = $this->getDefaultFieldOptions($fieldDefinition, $languageCode, $content); + + $fieldSettings = $fieldDefinition->getFieldSettings(); + + $selectionMethod = $fieldSettings['selectionMethod']; + + $defaultLocation = $fieldSettings['selectionDefaultLocation']; + $contentTypes = $fieldSettings['selectionContentTypes']; + + /* TODO: implement different selection methods */ + switch ($fieldSettings['selectionMethod']) { + case self::MULTIPLE_SELECTION: + $locationService = $this->repository->getLocationService(); + $location = $locationService->loadLocation($defaultLocation ? $defaultLocation : 2); + $locationList = $locationService->loadLocationChildren($location); + + $choices = []; + foreach ($locationList->locations as $child) { + /** @var Location $child */ + $choices[$this->translationHelper->getTranslatedContentNameByContentInfo($child->contentInfo)] = $child->contentInfo->id; + } + + $formBuilder->add($fieldDefinition->identifier, ChoiceType::class, [ + 'choices' => $choices, + 'expanded' => false, + 'multiple' => true, + 'choices_as_values' => true, + ], $options); + break; + default: + $locationService = $this->repository->getLocationService(); + $location = $locationService->loadLocation($defaultLocation ? $defaultLocation : 2); + $locationList = $locationService->loadLocationChildren($location); + + $choices = []; + foreach ($locationList->locations as $child) { + /** @var Location $child */ + $choices[$this->translationHelper->getTranslatedContentNameByContentInfo($child->contentInfo)] = $child->contentInfo->id; + } + + $formBuilder->add($fieldDefinition->identifier, ChoiceType::class, [ + 'choices' => $choices, + 'expanded' => false, + 'multiple' => false, + 'choices_as_values' => true, + ], $options); + break; + } + } + + public function convertFieldValueToForm(Value $value, FieldDefinition $fieldDefinition = null) + { + if (empty($value->destinationContentIds)) { + return null; + } + + return $value->destinationContentIds; + } + + public function convertFieldValueFromForm($data) + { + return new RelationListValue($data); + } +} diff --git a/bundle/Resources/config/form_fieldtype_handlers.yml b/bundle/Resources/config/form_fieldtype_handlers.yml index 1e3b88a..428edb4 100644 --- a/bundle/Resources/config/form_fieldtype_handlers.yml +++ b/bundle/Resources/config/form_fieldtype_handlers.yml @@ -114,3 +114,11 @@ services: - "@ezpublish.translation_helper" tags: - { name: netgen.ezforms.form.fieldtype_handler, alias: ezobjectrelation } + + netgen.ezforms.form.fieldtype_handler.ezobjectrelationlist: + class: Netgen\Bundle\EzFormsBundle\Form\FieldTypeHandler\RelationList + arguments: + - "@ezpublish.api.repository" + - "@ezpublish.translation_helper" + tags: + - { name: netgen.ezforms.form.fieldtype_handler, alias: ezobjectrelationlist }