thirdparty, manage case with only one type

in that case, type field is hidden, uniq type is automatically written in table
This commit is contained in:
Mathieu Jaumotte 2021-09-15 16:06:15 +02:00
parent 6e31e01e51
commit 683eac91d8
3 changed files with 50 additions and 25 deletions

View File

@ -62,6 +62,35 @@ class ThirdPartyType extends AbstractType
foreach ($this->typesManager->getProviders() as $key => $provider) {
$types['chill_3party.key_label.'.$key] = $key;
}
if (count($types) === 1) {
$builder
->add('type', HiddenType::class, [
'data' => array_values($types)
])
->get('type')
->addModelTransformer(new CallbackTransformer(
function (?array $typeArray): ?string {
if (null === $typeArray) {
return null;
}
return implode(',', $typeArray);
},
function (?string $typeStr): ?array {
if (null === $typeStr) {
return null;
}
return explode(',', $typeStr);
}
))
;
} else {
$builder->add('type', ChoiceType::class, [
'choices' => $types,
'expanded' => true,
'multiple' => true,
'label' => 'thirdparty.Type'
]);
}
$builder
->add('name', TextType::class, [
@ -81,12 +110,6 @@ class ThirdPartyType extends AbstractType
'multiple' => true,
'attr' => ['class' => 'select2']
])
->add('type', ChoiceType::class, [
'choices' => $types,
'expanded' => true,
'multiple' => true,
'label' => 'thirdparty.Type'
])
->add('telephone', TextType::class, [
'label' => 'Phonenumber',
'required' => false
@ -113,24 +136,26 @@ class ThirdPartyType extends AbstractType
'attr' => ['class' => 'select2']
])
;
$builder->add('address', HiddenType::class);
$builder->get('address')
->addModelTransformer(new CallbackTransformer(
function (?Address $address): string {
if (null === $address) {
return '';
$builder
->add('address', HiddenType::class)
->get('address')
->addModelTransformer(new CallbackTransformer(
function (?Address $address): string {
if (null === $address) {
return '';
}
return $address->getId();
},
function (?string $addressId): ?Address {
if (null === $addressId) {
return null;
}
return $this->om
->getRepository(Address::class)
->findOneBy(['id' => (int) $addressId]);
}
return $address->getId();
},
function (string $addressId): ?Address {
if ('' === $addressId) {
return null;
}
return $this->om
->getRepository(Address::class)
->findOneBy(['id' => (int) $addressId]);
}
))
))
;
// Contact Person ThirdParty (child)

View File

@ -26,8 +26,8 @@
{{ form_row(form.profession) }}
{% endif %}
{{ form_row(form.categories) }}
{{ form_row(form.type) }}
{{ form_row(form.categories) }}
{{ form_row(form.telephone) }}
{{ form_row(form.email) }}

View File

@ -43,8 +43,8 @@
{{ form_row(form.profession) }}
{% endif %}
{{ form_row(form.categories) }}
{{ form_row(form.type) }}
{{ form_row(form.categories) }}
{{ form_row(form.telephone) }}
{{ form_row(form.email) }}