Skip to content

Commit

Permalink
Revert "Remove support for netgen/ez-forms-bundle"
Browse files Browse the repository at this point in the history
This reverts commit 5cb32d6.
  • Loading branch information
emodric committed Feb 28, 2022
1 parent c981c8c commit 639498c
Show file tree
Hide file tree
Showing 4 changed files with 227 additions and 0 deletions.
80 changes: 80 additions & 0 deletions bundle/Form/FieldTypeHandler/EnhancedSelection.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,80 @@
<?php

declare(strict_types=1);

namespace Netgen\Bundle\EnhancedSelectionBundle\Form\FieldTypeHandler;

use Ibexa\Contracts\Core\FieldType\Value;
use Ibexa\Contracts\Core\Repository\Values\Content\Content;
use Ibexa\Contracts\Core\Repository\Values\ContentType\FieldDefinition;
use Netgen\Bundle\EnhancedSelectionBundle\Core\FieldType\EnhancedSelection\Value as EnhancedSelectionValue;
use Netgen\Bundle\EzFormsBundle\Form\FieldTypeHandler;
use Symfony\Component\Form\Extension\Core\Type\ChoiceType;
use Symfony\Component\Form\FormBuilderInterface;
use function is_array;

final class EnhancedSelection extends FieldTypeHandler
{
public function convertFieldValueToForm(Value $value, ?FieldDefinition $fieldDefinition = null)
{
$isMultiple = true;
if ($fieldDefinition !== null) {
$fieldSettings = $fieldDefinition->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;
}
}
5 changes: 5 additions & 0 deletions bundle/Resources/config/field_types.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down
2 changes: 2 additions & 0 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -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": {
Expand Down
140 changes: 140 additions & 0 deletions tests/Form/FieldTypeHandler/EnhancedSelectionTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,140 @@
<?php

declare(strict_types=1);

namespace Netgen\Bundle\EnhancedSelectionBundle\Tests\Form\FieldTypeHandler;

use Ibexa\Core\Repository\Values\ContentType\FieldDefinition;
use Netgen\Bundle\EnhancedSelectionBundle\Core\FieldType\EnhancedSelection\Value as EnhancedSelectionValue;
use Netgen\Bundle\EnhancedSelectionBundle\Form\FieldTypeHandler\EnhancedSelection;
use Netgen\Bundle\EzFormsBundle\Form\FieldTypeHandler;
use PHPUnit\Framework\TestCase;
use Symfony\Component\Form\FormBuilder;

final class EnhancedSelectionTest extends TestCase
{
/**
* @var EnhancedSelection
*/
private $handler;

protected function setUp(): void
{
$this->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);
}
}

0 comments on commit 639498c

Please sign in to comment.