-
Notifications
You must be signed in to change notification settings - Fork 10
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Revert "Remove support for netgen/ez-forms-bundle"
This reverts commit 5cb32d6.
- Loading branch information
Showing
4 changed files
with
227 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
} |