Skip to content

Commit

Permalink
Entities with construct can be used on Collection and AdminType
Browse files Browse the repository at this point in the history
There was a problem with entities that defined a constructor
with arguments. It was impossible to use them on Collection and
AdminType because we didn't override the default behavior when
the data is empty.
  • Loading branch information
jordisala1991 authored and soullivaneuh committed Apr 10, 2018
1 parent 2f09845 commit e4174d7
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 0 deletions.
6 changes: 6 additions & 0 deletions src/Builder/FormContractor.php
Original file line number Diff line number Diff line change
Expand Up @@ -157,6 +157,9 @@ public function getDefaultOptions($type, FieldDescriptionInterface $fieldDescrip
$options['delete'] = false;

$options['data_class'] = $fieldDescription->getAssociationAdmin()->getClass();
$options['empty_data'] = function () use ($fieldDescription) {
return $fieldDescription->getAssociationAdmin()->getNewInstance();
};
$fieldDescription->setOption('edit', $fieldDescription->getOption('edit', 'admin'));
// NEXT_MAJOR: Check only against FQCNs when dropping support for Symfony 2.8
} elseif ('sonata_type_collection' === $type || $this->checkFormClass($type, [CollectionType::class])) {
Expand All @@ -174,6 +177,9 @@ public function getDefaultOptions($type, FieldDescriptionInterface $fieldDescrip
$options['type_options'] = [
'sonata_field_description' => $fieldDescription,
'data_class' => $fieldDescription->getAssociationAdmin()->getClass(),
'empty_data' => function () use ($fieldDescription) {
return $fieldDescription->getAssociationAdmin()->getNewInstance();
},
];
}

Expand Down
4 changes: 4 additions & 0 deletions tests/Builder/FormContractorTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,7 @@ public function testDefaultOptionsForSonataFormTypes()
{
$admin = $this->createMock(AdminInterface::class);
$modelManager = $this->createMock(ModelManagerInterface::class);
$model = $this->createMock(\stdClass::class);
$modelClass = 'FooEntity';

$admin->method('getModelManager')->willReturn($modelManager);
Expand All @@ -73,6 +74,7 @@ public function testDefaultOptionsForSonataFormTypes()
$fieldDescription->method('getAdmin')->willReturn($admin);
$fieldDescription->method('getTargetEntity')->willReturn($modelClass);
$fieldDescription->method('getAssociationAdmin')->willReturn($admin);
$admin->method('getNewInstance')->willReturn($model);

// NEXT_MAJOR: Use only FQCNs when dropping support for Symfony 2.8
$modelTypes = [
Expand Down Expand Up @@ -110,6 +112,7 @@ public function testDefaultOptionsForSonataFormTypes()
$this->assertSame($modelClass, $options['data_class']);
$this->assertFalse($options['btn_add']);
$this->assertFalse($options['delete']);
$this->assertSame($model, $options['empty_data']());
}

// collection type
Expand All @@ -121,6 +124,7 @@ public function testDefaultOptionsForSonataFormTypes()
$this->assertTrue($options['modifiable']);
$this->assertSame($fieldDescription, $options['type_options']['sonata_field_description']);
$this->assertSame($modelClass, $options['type_options']['data_class']);
$this->assertSame($model, $options['type_options']['empty_data']());
}
}

Expand Down

0 comments on commit e4174d7

Please sign in to comment.