mirror of
https://gitlab.com/Chill-Projet/chill-bundles.git
synced 2025-08-27 10:03:49 +00:00
load postal codes dynamically
This commit is contained in:
85
Form/ChoiceLoader/PostalCodeChoiceLoader.php
Normal file
85
Form/ChoiceLoader/PostalCodeChoiceLoader.php
Normal file
@@ -0,0 +1,85 @@
|
||||
<?php
|
||||
/*
|
||||
* Copyright (C) 2018 Champs-Libres <info@champs-libres.coop>
|
||||
*
|
||||
* This program is free software: you can redistribute it and/or modify
|
||||
* it under the terms of the GNU Affero General Public License as published by
|
||||
* the Free Software Foundation, either version 3 of the License, or
|
||||
* (at your option) any later version.
|
||||
*
|
||||
* This program is distributed in the hope that it will be useful,
|
||||
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
* GNU Affero General Public License for more details.
|
||||
*
|
||||
* You should have received a copy of the GNU Affero General Public License
|
||||
* along with this program. If not, see <http://www.gnu.org/licenses/>.
|
||||
*/
|
||||
namespace Chill\MainBundle\Form\ChoiceLoader;
|
||||
|
||||
use Symfony\Component\Form\ChoiceList\Loader\ChoiceLoaderInterface;
|
||||
use Symfony\Component\Form\ChoiceList\ChoiceListInterface;
|
||||
use Chill\MainBundle\Repository\PostalCodeRepository;
|
||||
use Symfony\Component\Form\ChoiceList\LazyChoiceList;
|
||||
use Chill\MainBundle\Entity\PostalCode;
|
||||
|
||||
/**
|
||||
*
|
||||
*
|
||||
* @author Julien Fastré <julien.fastre@champs-libres.coop>
|
||||
*/
|
||||
class PostalCodeChoiceLoader implements ChoiceLoaderInterface
|
||||
{
|
||||
/**
|
||||
*
|
||||
* @var PostalCodeRepository
|
||||
*/
|
||||
protected $postalCodeRepository;
|
||||
|
||||
protected $lazyLoadedPostalCodes = [];
|
||||
|
||||
public function __construct(PostalCodeRepository $postalCodeRepository)
|
||||
{
|
||||
$this->postalCodeRepository = $postalCodeRepository;
|
||||
}
|
||||
|
||||
public function loadChoiceList($value = null): ChoiceListInterface
|
||||
{
|
||||
$list = new \Symfony\Component\Form\ChoiceList\ArrayChoiceList(
|
||||
$this->lazyLoadedPostalCodes,
|
||||
function(PostalCode $pc) use ($value) {
|
||||
return \call_user_func($value, $pc);
|
||||
});
|
||||
|
||||
return $list;
|
||||
}
|
||||
|
||||
public function loadChoicesForValues(array $values, $value = null)
|
||||
{
|
||||
$choices = [];
|
||||
|
||||
foreach($values as $value) {
|
||||
$choices[] = $this->postalCodeRepository->find($value);
|
||||
}
|
||||
|
||||
return $choices;
|
||||
}
|
||||
|
||||
public function loadValuesForChoices(array $choices, $value = null)
|
||||
{
|
||||
$values = [];
|
||||
|
||||
foreach ($choices as $choice) {
|
||||
if (NULL === $choice) {
|
||||
$values[] = null;
|
||||
continue;
|
||||
}
|
||||
|
||||
$id = \call_user_func($value, $choice);
|
||||
$values[] = $id;
|
||||
$this->lazyLoadedPostalCodes[$id] = $choice;
|
||||
}
|
||||
|
||||
return $values;
|
||||
}
|
||||
}
|
@@ -21,10 +21,14 @@ namespace Chill\MainBundle\Form\Type;
|
||||
|
||||
use Symfony\Component\Form\AbstractType;
|
||||
use Symfony\Component\OptionsResolver\OptionsResolver;
|
||||
use Symfony\Component\Form\FormBuilderInterface;
|
||||
use Symfony\Bridge\Doctrine\Form\Type\EntityType;
|
||||
use Chill\MainBundle\Templating\TranslatableStringHelper;
|
||||
use Chill\MainBundle\Entity\PostalCode;
|
||||
use Symfony\Component\Form\FormInterface;
|
||||
use Symfony\Component\Form\FormView;
|
||||
use Symfony\Component\Routing\Generator\UrlGeneratorInterface;
|
||||
use Chill\MainBundle\Form\ChoiceLoader\PostalCodeChoiceLoader;
|
||||
use Symfony\Component\Translation\TranslatorInterface;
|
||||
|
||||
/**
|
||||
* A form to pick between PostalCode
|
||||
@@ -39,10 +43,35 @@ class PostalCodeType extends AbstractType
|
||||
* @var TranslatableStringHelper
|
||||
*/
|
||||
protected $translatableStringHelper;
|
||||
|
||||
/**
|
||||
*
|
||||
* @var UrlGeneratorInterface
|
||||
*/
|
||||
protected $urlGenerator;
|
||||
|
||||
/**
|
||||
*
|
||||
* @var PostalCodeChoiceLoader
|
||||
*/
|
||||
protected $choiceLoader;
|
||||
|
||||
/**
|
||||
*
|
||||
* @var TranslatorInterface
|
||||
*/
|
||||
protected $translator;
|
||||
|
||||
public function __construct(TranslatableStringHelper $helper)
|
||||
{
|
||||
public function __construct(
|
||||
TranslatableStringHelper $helper,
|
||||
UrlGeneratorInterface $urlGenerator,
|
||||
PostalCodeChoiceLoader $choiceLoader,
|
||||
TranslatorInterface $translator
|
||||
) {
|
||||
$this->translatableStringHelper = $helper;
|
||||
$this->urlGenerator = $urlGenerator;
|
||||
$this->choiceLoader = $choiceLoader;
|
||||
$this->translator = $translator;
|
||||
}
|
||||
|
||||
|
||||
@@ -55,11 +84,26 @@ class PostalCodeType extends AbstractType
|
||||
{
|
||||
// create a local copy for usage in Closure
|
||||
$helper = $this->translatableStringHelper;
|
||||
$resolver->setDefault('class', PostalCode::class)
|
||||
$resolver
|
||||
->setDefault('class', PostalCode::class)
|
||||
->setDefault('choice_label', function(PostalCode $code) use ($helper) {
|
||||
return $code->getCode().' '.$code->getName().' ['.
|
||||
$helper->localize($code->getCountry()->getName()).']';
|
||||
}
|
||||
);
|
||||
})
|
||||
->setDefault('choice_loader', $this->choiceLoader)
|
||||
->setDefault('placeholder', 'Select a postal code')
|
||||
;
|
||||
}
|
||||
|
||||
public function buildView(FormView $view, FormInterface $form, array $options)
|
||||
{
|
||||
$view->vars['attr']['data-postal-code'] = 'data-postal-code';
|
||||
$view->vars['attr']['data-search-url'] = $this->urlGenerator
|
||||
->generate('chill_main_postal_code_search');
|
||||
$view->vars['attr']['data-placeholder'] = $this->translator->trans($options['placeholder']);
|
||||
$view->vars['attr']['data-no-results-label'] = $this->translator->trans('select2.no_results');
|
||||
$view->vars['attr']['data-error-load-label'] = $this->translator->trans('select2.error_loading');
|
||||
$view->vars['attr']['data-searching-label'] = $this->translator->trans('select2.searching');
|
||||
}
|
||||
|
||||
}
|
||||
|
Reference in New Issue
Block a user