From 639498cb99376c78418eb5fcff7267b3b6c449a0 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Edi=20Modri=C4=87?= Date: Mon, 28 Feb 2022 12:06:31 +0100 Subject: [PATCH] Revert "Remove support for netgen/ez-forms-bundle" This reverts commit 5cb32d6d624bc75f320567eb711c97113a1bbae2. --- .../FieldTypeHandler/EnhancedSelection.php | 80 ++++++++++ bundle/Resources/config/field_types.yaml | 5 + composer.json | 2 + .../EnhancedSelectionTest.php | 140 ++++++++++++++++++ 4 files changed, 227 insertions(+) create mode 100644 bundle/Form/FieldTypeHandler/EnhancedSelection.php create mode 100644 tests/Form/FieldTypeHandler/EnhancedSelectionTest.php diff --git a/bundle/Form/FieldTypeHandler/EnhancedSelection.php b/bundle/Form/FieldTypeHandler/EnhancedSelection.php new file mode 100644 index 0000000..c2eeaea --- /dev/null +++ b/bundle/Form/FieldTypeHandler/EnhancedSelection.php @@ -0,0 +1,80 @@ +getFieldSettings(); + $isMultiple = $fieldSettings['isMultiple']; + } + + if (!$isMultiple) { + if (empty($value->identifiers)) { + return ''; + } + + return $value->identifiers[0]; + } + + return $value->identifiers; + } + + public function convertFieldValueFromForm($data): EnhancedSelectionValue + { + if ($data === null) { + return new EnhancedSelectionValue(); + } + + return new EnhancedSelectionValue(is_array($data) ? $data : [$data]); + } + + protected function buildFieldForm( + FormBuilderInterface $formBuilder, + FieldDefinition $fieldDefinition, + string $languageCode, + ?Content $content = null + ): void { + $options = $this->getDefaultFieldOptions($fieldDefinition, $languageCode, $content); + + $fieldSettings = $fieldDefinition->getFieldSettings(); + $optionsValues = $fieldSettings['options']; + + $options['multiple'] = $fieldSettings['isMultiple']; + $options['expanded'] = $fieldSettings['isExpanded']; + $options['choices'] = $this->getValues($optionsValues); + + $formBuilder->add( + $fieldDefinition->identifier, + ChoiceType::class, + $options + ); + } + + private function getValues(array $options): array + { + $values = []; + + foreach ($options as $option) { + if (!empty($option['identifier']) && !empty($option['name'])) { + $values[$option['name']] = $option['identifier']; + } + } + + return $values; + } +} diff --git a/bundle/Resources/config/field_types.yaml b/bundle/Resources/config/field_types.yaml index 1326170..3b43372 100644 --- a/bundle/Resources/config/field_types.yaml +++ b/bundle/Resources/config/field_types.yaml @@ -27,6 +27,11 @@ services: tags: - { name: ibexa.field_type.indexable, alias: sckenhancedselection } + netgen.enhanced_selection.field_type.form.fieldtype_handler: + class: Netgen\Bundle\EnhancedSelectionBundle\Form\FieldTypeHandler\EnhancedSelection + tags: + - { name: netgen.ezforms.form.fieldtype_handler, alias: sckenhancedselection } + netgen.enhanced_selection.field_type.form_mapper: class: Netgen\Bundle\EnhancedSelectionBundle\Core\FieldType\EnhancedSelection\FormMapper tags: diff --git a/composer.json b/composer.json index 1740acc..72733d8 100644 --- a/composer.json +++ b/composer.json @@ -22,11 +22,13 @@ }, "require-dev": { "phpunit/phpunit": "^9.0", + "netgen/ez-forms-bundle": "^3.0", "friendsofphp/php-cs-fixer": "^2.16" }, "minimum-stability": "dev", "prefer-stable": true, "suggest": { + "netgen/ez-forms-bundle": "Allows use of enhanced selection field type with Symfony forms", "ibexa/solr": "Allows use of EnhancedSelection search criterion with Solr search engine" }, "autoload": { diff --git a/tests/Form/FieldTypeHandler/EnhancedSelectionTest.php b/tests/Form/FieldTypeHandler/EnhancedSelectionTest.php new file mode 100644 index 0000000..7eb7642 --- /dev/null +++ b/tests/Form/FieldTypeHandler/EnhancedSelectionTest.php @@ -0,0 +1,140 @@ +handler = new EnhancedSelection(); + } + + public function testInstanceOfFieldTypeHandler(): void + { + self::assertInstanceOf(FieldTypeHandler::class, $this->handler); + } + + public function testConvertFieldValueToForm(): void + { + $identifiers = ['identifier1', 'identifier2']; + $selection = new EnhancedSelectionValue($identifiers); + + $converted = $this->handler->convertFieldValueToForm($selection); + + self::assertSame($identifiers, $converted); + } + + public function testConvertFieldValueToFormWithIdentifiersArrayEmpty(): void + { + $identifiers = []; + $selection = new EnhancedSelectionValue($identifiers); + $fieldDefinition = new FieldDefinition( + [ + 'fieldSettings' => [ + 'isMultiple' => false, + ], + ] + ); + + $converted = $this->handler->convertFieldValueToForm($selection, $fieldDefinition); + + self::assertSame('', $converted); + } + + public function testConvertFieldValueToFormWithFieldDefinitionMultiple(): void + { + $identifiers = ['identifier1', 'identifier2']; + $selection = new EnhancedSelectionValue($identifiers); + $fieldDefinition = new FieldDefinition( + [ + 'fieldSettings' => [ + 'isMultiple' => true, + ], + ] + ); + + $converted = $this->handler->convertFieldValueToForm($selection, $fieldDefinition); + + self::assertSame($identifiers, $converted); + } + + public function testConvertFieldValueToFormWithFieldDefinitionSingle(): void + { + $identifiers = ['identifier1', 'identifier2']; + $selection = new EnhancedSelectionValue($identifiers); + $fieldDefinition = new FieldDefinition( + [ + 'fieldSettings' => [ + 'isMultiple' => false, + ], + ] + ); + + $converted = $this->handler->convertFieldValueToForm($selection, $fieldDefinition); + + self::assertSame($identifiers[0], $converted); + } + + public function testConvertFieldValueFromForm(): void + { + $identifiers = ['identifier1', 'identifier2']; + $selection = new EnhancedSelectionValue($identifiers); + + $converted = $this->handler->convertFieldValueFromForm($identifiers); + + self::assertSame($selection->identifiers, $converted->identifiers); + } + + public function testBuildFieldCreateForm(): void + { + $formBuilder = $this->getMockBuilder(FormBuilder::class) + ->disableOriginalConstructor() + ->onlyMethods(['add']) + ->getMock(); + + $formBuilder->expects(self::once()) + ->method('add'); + + $fieldDefinition = new FieldDefinition( + [ + 'id' => 'id', + 'identifier' => 'identifier', + 'isRequired' => true, + 'descriptions' => ['fre-FR' => 'fre-FR'], + 'names' => ['fre-FR' => 'fre-FR'], + 'fieldSettings' => [ + 'options' => [ + [ + 'identifier' => 'identifier0', + 'name' => 'Identifier0', + ], + [ + 'identifier' => 'identifier1', + 'name' => 'Identifier1', + ], + ], + 'isMultiple' => true, + 'isExpanded' => true, + ], + ] + ); + + $languageCode = 'eng-GB'; + + $this->handler->buildFieldCreateForm($formBuilder, $fieldDefinition, $languageCode); + } +}