Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 21 additions & 4 deletions src/Builder/FormContractor.php
Original file line number Diff line number Diff line change
Expand Up @@ -94,6 +94,9 @@ public function getFormBuilder($name, array $options = [])

public function getDefaultOptions($type, FieldDescriptionInterface $fieldDescription)
{
// NEXT_MAJOR: Remove this line and update the function signature.
$formOptions = \func_get_args()[2] ?? [];
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@core23 The phpdoc is inherited from the interface getDefaultOptions.

I prefer to add the phpdoc in the interface in the SonataAdmin PR https://github.com/sonata-project/SonataAdminBundle/pull/6438/files


$options = [];
$options['sonata_field_description'] = $fieldDescription;

Expand Down Expand Up @@ -163,10 +166,7 @@ public function getDefaultOptions($type, FieldDescriptionInterface $fieldDescrip

$options['type'] = AdminType::class;
$options['modifiable'] = true;
$options['type_options'] = [
'sonata_field_description' => $fieldDescription,
'data_class' => $fieldDescription->getAssociationAdmin()->getClass(),
];
$options['type_options'] = $this->getDefaultAdminTypeOptions($fieldDescription, $formOptions);
}

return $options;
Expand All @@ -184,4 +184,21 @@ private function checkFormClass($type, $classes)
return is_a($type, $subclass, true);
});
}

private function getDefaultAdminTypeOptions(FieldDescriptionInterface $fieldDescription, array $formOptions): array
{
$typeOptions = [
'sonata_field_description' => $fieldDescription,
'data_class' => $fieldDescription->getAssociationAdmin()->getClass(),
'empty_data' => static function () use ($fieldDescription) {
return $fieldDescription->getAssociationAdmin()->getNewInstance();
},
];

if (isset($formOptions['by_reference'])) {
$typeOptions['by_reference'] = $formOptions['by_reference'];
}

return $typeOptions;
}
}
5 changes: 4 additions & 1 deletion tests/Builder/FormContractorTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -109,12 +109,15 @@ public function testDefaultOptionsForSonataFormTypes(): void
// collection type
$fieldDescription->method('getMappingType')->willReturn(ClassMetadata::MANY);
foreach ($collectionTypes as $index => $formType) {
$options = $this->formContractor->getDefaultOptions($formType, $fieldDescription);
$options = $this->formContractor->getDefaultOptions($formType, $fieldDescription, [
'by_reference' => false,
]);
$this->assertSame($fieldDescription, $options['sonata_field_description']);
$this->assertSame(AdminType::class, $options['type']);
$this->assertTrue($options['modifiable']);
$this->assertSame($fieldDescription, $options['type_options']['sonata_field_description']);
$this->assertSame($modelClass, $options['type_options']['data_class']);
$this->assertFalse($options['type_options']['by_reference']);
}
}

Expand Down