Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Extract base form types for every entity used in AdminBundle #16257

Merged
merged 15 commits into from
May 23, 2024
Merged
Prev Previous commit
Next Next commit
Extract admin-only CountryType
  • Loading branch information
TheMilek committed May 22, 2024
commit 22972b36dcddf723a761253dbfae097efc6a72c8
69 changes: 67 additions & 2 deletions src/Sylius/Bundle/AdminBundle/Form/Type/CountryType.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,17 +14,82 @@
namespace Sylius\Bundle\AdminBundle\Form\Type;

use Sylius\Bundle\AddressingBundle\Form\Type\CountryType as BaseCountryType;
use Sylius\Bundle\AddressingBundle\Form\Type\ProvinceType;
use Sylius\Component\Addressing\Model\CountryInterface;
use Sylius\Component\Resource\Repository\RepositoryInterface;
use Symfony\Component\Form\AbstractType;
use Symfony\Component\Form\Extension\Core\Type\CheckboxType;
use Symfony\Component\Form\FormBuilderInterface;
use Symfony\Component\Form\FormEvent;
use Symfony\Component\Form\FormEvents;
use Symfony\Component\Intl\Countries;
use Symfony\UX\LiveComponent\Form\Type\LiveCollectionType;

final class CountryType extends AbstractType
{
public function getBlockPrefix(): string
/** @param RepositoryInterface<CountryInterface> $countryRepository */
public function __construct(private readonly RepositoryInterface $countryRepository)
{
return 'sylius_admin_country';
}

public function buildForm(FormBuilderInterface $builder, array $options): void
{
$builder->addEventListener(FormEvents::PRE_SET_DATA, function (FormEvent $event): void {
$options = [
'label' => 'sylius.form.country.name',
'choice_loader' => null,
];

$country = $event->getData();
if ($country instanceof CountryInterface && null !== $country->getCode()) {
$options['disabled'] = true;
$options['choices'] = [$this->getCountryName($country->getCode()) => $country->getCode()];
} else {
$options['choices'] = array_flip($this->getAvailableCountries());
}

$form = $event->getForm();
$form->add('code', \Symfony\Component\Form\Extension\Core\Type\CountryType::class, $options);
TheMilek marked this conversation as resolved.
Show resolved Hide resolved
});

$builder
->add('provinces', LiveCollectionType::class, [
'entry_type' => ProvinceType::class,
'allow_add' => true,
'allow_delete' => true,
'by_reference' => false,
'button_add_options' => [
'label' => 'sylius.form.country.add_province',
],
])
->add('enabled', CheckboxType::class, [
'label' => 'sylius.form.country.enabled',
])
;
}

public function getParent(): string
{
return BaseCountryType::class;
}

private function getCountryName(string $code): string
{
return Countries::getName($code);
}

/** @return string[] */
private function getAvailableCountries(): array
{
$availableCountries = Countries::getNames();

/** @var CountryInterface[] $definedCountries */
$definedCountries = $this->countryRepository->findAll();

foreach ($definedCountries as $country) {
unset($availableCountries[$country->getCode()]);
}

return $availableCountries;
}
}
95 changes: 0 additions & 95 deletions src/Sylius/Bundle/AdminBundle/Form/Type/CountryTypeExtension.php

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@ sylius_admin_country:
except: ['show', 'delete']
redirect: update
grid: sylius_admin_country
form:
type: Sylius\Bundle\AdminBundle\Form\Type\CountryType
permission: true
vars:
all:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -43,9 +43,9 @@
<tag name="form.type" />
</service>

<service id="sylius.form.extension.country" class="Sylius\Bundle\AdminBundle\Form\Extension\CountryTypeExtension">
<service id="sylius_admin.form.type.country" class="Sylius\Bundle\AdminBundle\Form\Type\CountryType">
<argument type="service" id="sylius.repository.country" />
<tag name="form.type_extension" extended-type="Sylius\Bundle\AddressingBundle\Form\Type\CountryType" priority="100" />
<tag name="form.type" />
</service>

<service id="sylius_admin.form.extension.shipping_method" class="Sylius\Bundle\AdminBundle\Form\Extension\ShippingMethodTypeExtension">
Expand Down