From f3f1cf08b78f60c3e87cd18e9f6613afff6e6d83 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Tomislav=20Buljevi=C4=87?= Date: Mon, 20 Nov 2017 11:51:03 +0100 Subject: [PATCH 1/4] Create RelationList.php Relation List Field Type Handler implementation for NetgenEzFormsBundle. --- bundle/Form/FieldTypeHandler/RelationList.php | 87 +++++++++++++++++++ 1 file changed, 87 insertions(+) create mode 100644 bundle/Form/FieldTypeHandler/RelationList.php diff --git a/bundle/Form/FieldTypeHandler/RelationList.php b/bundle/Form/FieldTypeHandler/RelationList.php new file mode 100644 index 0000000..4c0636e --- /dev/null +++ b/bundle/Form/FieldTypeHandler/RelationList.php @@ -0,0 +1,87 @@ +repository = $repository; + } + + 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']) { + 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); + } +} From ccae3cd7d56409e3264c07954831b6229af5f6f0 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Tomislav=20Buljevi=C4=87?= Date: Mon, 20 Nov 2017 11:56:04 +0100 Subject: [PATCH 2/4] Update form_fieldtype_handlers.yml Added RelationList handler to the list of form fieldtype handlers. --- bundle/Resources/config/form_fieldtype_handlers.yml | 10 +++++++++- 1 file changed, 9 insertions(+), 1 deletion(-) diff --git a/bundle/Resources/config/form_fieldtype_handlers.yml b/bundle/Resources/config/form_fieldtype_handlers.yml index 0da2782..b46b178 100644 --- a/bundle/Resources/config/form_fieldtype_handlers.yml +++ b/bundle/Resources/config/form_fieldtype_handlers.yml @@ -17,6 +17,7 @@ parameters: netgen.ezforms.form.fieldtype_handler.ezbinaryfile.class: Netgen\Bundle\EzFormsBundle\Form\FieldTypeHandler\BinaryFile netgen.ezforms.form.fieldtype_handler.ezgmaplocation.class: Netgen\Bundle\EzFormsBundle\Form\FieldTypeHandler\MapLocation netgen.ezforms.form.fieldtype_handler.ezobjectrelation.class: Netgen\Bundle\EzFormsBundle\Form\FieldTypeHandler\Relation + netgen.ezforms.form.fieldtype_handler.ezobjectrelationlist.class: Netgen\Bundle\EzFormsBundle\Form\FieldTypeHandler\RelationList services: netgen.ezforms.form.fieldtype_handler.ezimage: @@ -115,4 +116,11 @@ services: - "@ezpublish.api.repository" - "@ezpublish.translation_helper" tags: - - {name: netgen.ezforms.form.fieldtype_handler, alias: ezobjectrelation} \ No newline at end of file + - {name: netgen.ezforms.form.fieldtype_handler, alias: ezobjectrelation} + + netgen.ezforms.form.fieldtype_handler.ezobjectrelationlist: + class: "%netgen.ezforms.form.fieldtype_handler.ezobjectrelationlist.class%" + arguments: + - "@ezpublish.api.repository" + tags: + - {name: netgen.ezforms.form.fieldtype_handler, alias: ezobjectrelationlist} From 54904ade3c5be06b92da6330eaf5dd71aea5aeff Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Tomislav=20Buljevi=C4=87?= Date: Mon, 20 Nov 2017 12:07:18 +0100 Subject: [PATCH 3/4] Update RelationList.php Added translationHelper --- bundle/Form/FieldTypeHandler/RelationList.php | 28 ++++++++++++++++++- 1 file changed, 27 insertions(+), 1 deletion(-) diff --git a/bundle/Form/FieldTypeHandler/RelationList.php b/bundle/Form/FieldTypeHandler/RelationList.php index 4c0636e..00db88f 100644 --- a/bundle/Form/FieldTypeHandler/RelationList.php +++ b/bundle/Form/FieldTypeHandler/RelationList.php @@ -7,6 +7,7 @@ use eZ\Publish\API\Repository\Values\ContentType\FieldDefinition; use eZ\Publish\SPI\FieldType\Value; use Netgen\Bundle\EzFormsBundle\Form\FieldTypeHandler; +use eZ\Publish\Core\Helper\TranslationHelper; use Symfony\Component\Form\Extension\Core\Type\ChoiceType; use Symfony\Component\Form\FormBuilderInterface; @@ -24,12 +25,19 @@ class RelationList extends FieldTypeHandler * @var Repository */ private $repository; + + /** + * @var TranslationHelper + */ + private $translationHelper; public function __construct( - Repository $repository + Repository $repository, + TranslationHelper $translationHelper ) { $this->repository = $repository; + $this->translationHelper = $translationHelper; } protected function buildFieldForm( @@ -50,6 +58,24 @@ protected function buildFieldForm( /* 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); From f04b920ef2a3bfb4362cd8c1a2391539c749a375 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Tomislav=20Buljevi=C4=87?= Date: Mon, 20 Nov 2017 12:08:16 +0100 Subject: [PATCH 4/4] Update form_fieldtype_handlers.yml Added translation helper to the ezobjectrelationlist handler. --- bundle/Resources/config/form_fieldtype_handlers.yml | 1 + 1 file changed, 1 insertion(+) diff --git a/bundle/Resources/config/form_fieldtype_handlers.yml b/bundle/Resources/config/form_fieldtype_handlers.yml index b46b178..b8c213b 100644 --- a/bundle/Resources/config/form_fieldtype_handlers.yml +++ b/bundle/Resources/config/form_fieldtype_handlers.yml @@ -122,5 +122,6 @@ services: class: "%netgen.ezforms.form.fieldtype_handler.ezobjectrelationlist.class%" arguments: - "@ezpublish.api.repository" + - "@ezpublish.translation_helper" tags: - {name: netgen.ezforms.form.fieldtype_handler, alias: ezobjectrelationlist}