[address] allow to add custom data on addresses

This commit is contained in:
2021-03-08 14:11:39 +01:00
parent 14d32aa763
commit 5bf9bbc22e
9 changed files with 172 additions and 17 deletions

View File

@@ -39,6 +39,8 @@ use Symfony\Component\Form\Extension\Core\Type\ChoiceType;
* - `null_if_empty` (boolean): replace the address type by null if the street
* or the postCode is empty. This is useful when the address is not required and
* embedded in another form.
* - `customize_data` (callable): allow to add custom field to the form, which
* will be store in the `customs` property
*/
class AddressType extends AbstractType
{
@@ -80,9 +82,19 @@ class AddressType extends AbstractType
]);
}
if ($options['customize_data'] !== NULL && is_callable($options['customize_data'])) {
$customsBuilder = $builder->create('customs', NULL, [
'compound' => true,
'label' => false
]);
\call_user_func($options['customize_data'], $customsBuilder);
$builder->add($customsBuilder);
}
if ($options['null_if_empty'] === TRUE) {
$builder->setDataMapper(new AddressDataMapper());
}
}
public function configureOptions(OptionsResolver $resolver)
@@ -98,6 +110,8 @@ class AddressType extends AbstractType
->setDefined('null_if_empty')
->setDefault('null_if_empty', false)
->setAllowedTypes('null_if_empty', 'bool')
->setDefined('customize_data')
->setAllowedTypes('customize_data', 'callable')
;
}
}