mirror of
https://gitlab.com/Chill-Projet/chill-bundles.git
synced 2025-09-02 21:13:57 +00:00
[alt names] add alt names on person creation form
This commit is contained in:
@@ -27,11 +27,11 @@ use Symfony\Component\OptionsResolver\OptionsResolver;
|
||||
use Symfony\Component\Form\Extension\Core\DataTransformer\DateTimeToStringTransformer;
|
||||
use Symfony\Component\Form\Extension\Core\Type\HiddenType;
|
||||
use Symfony\Component\Form\Extension\Core\Type\DateType;
|
||||
|
||||
use Chill\MainBundle\Form\Type\CenterType;
|
||||
use Chill\PersonBundle\Form\Type\GenderType;
|
||||
use Chill\MainBundle\Form\Type\DataTransformer\CenterTransformer;
|
||||
use Chill\PersonBundle\Form\CreationPersonType;
|
||||
use Chill\PersonBundle\Config\ConfigPersonAltNamesHelper;
|
||||
use Chill\PersonBundle\Form\Type\PersonAltNameType;
|
||||
|
||||
class CreationPersonType extends AbstractType
|
||||
{
|
||||
@@ -47,9 +47,19 @@ class CreationPersonType extends AbstractType
|
||||
* @var CenterTransformer
|
||||
*/
|
||||
private $centerTransformer;
|
||||
|
||||
/**
|
||||
*
|
||||
* @var ConfigPersonAltNamesHelper
|
||||
*/
|
||||
protected $configPersonAltNamesHelper;
|
||||
|
||||
public function __construct(CenterTransformer $centerTransformer) {
|
||||
public function __construct(
|
||||
CenterTransformer $centerTransformer,
|
||||
ConfigPersonAltNamesHelper $configPersonAltNamesHelper
|
||||
) {
|
||||
$this->centerTransformer = $centerTransformer;
|
||||
$this->configPersonAltNamesHelper = $configPersonAltNamesHelper;
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -78,6 +88,14 @@ class CreationPersonType extends AbstractType
|
||||
))
|
||||
->add('center', HiddenType::class)
|
||||
;
|
||||
|
||||
if ($this->configPersonAltNamesHelper->hasAltNames()) {
|
||||
$builder->add('altNames', PersonAltNameType::class, [
|
||||
'by_reference' => false,
|
||||
'force_hidden' => true
|
||||
]);
|
||||
}
|
||||
|
||||
$builder->get('birthdate')
|
||||
->addModelTransformer($dateToStringTransformer);
|
||||
$builder->get('creation_date')
|
||||
@@ -105,6 +123,12 @@ class CreationPersonType extends AbstractType
|
||||
))
|
||||
->add('center', CenterType::class)
|
||||
;
|
||||
|
||||
if ($this->configPersonAltNamesHelper->hasAltNames()) {
|
||||
$builder->add('altNames', PersonAltNameType::class, [
|
||||
'by_reference' => false
|
||||
]);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
@@ -3,6 +3,7 @@
|
||||
namespace Chill\PersonBundle\Form\DataMapper;
|
||||
|
||||
use Symfony\Component\Form\DataMapperInterface;
|
||||
use Doctrine\Common\Collections\ArrayCollection;
|
||||
use Doctrine\Common\Collections\Collection;
|
||||
use Symfony\Component\Form\Exception\UnexpectedTypeException;
|
||||
use Chill\PersonBundle\Entity\PersonAltName;
|
||||
@@ -44,7 +45,15 @@ class PersonAltNameDataMapper implements DataMapperInterface
|
||||
public function mapFormsToData($forms, &$viewData)
|
||||
{
|
||||
$mapIndexToKey = [];
|
||||
foreach ($viewData->getIterator() as $key => $altName) {
|
||||
|
||||
if (is_array($viewData)) {
|
||||
$dataIterator = $viewData;
|
||||
} else {
|
||||
$dataIterator = $viewData instanceof ArrayCollection ?
|
||||
$viewData->toArray() : $viewData->getIterator();
|
||||
}
|
||||
|
||||
foreach ($dataIterator as $key => $altName) {
|
||||
/** @var PersonAltName $altName */
|
||||
$mapIndexToKey[$altName->getKey()] = $key;
|
||||
}
|
||||
@@ -64,7 +73,12 @@ class PersonAltNameDataMapper implements DataMapperInterface
|
||||
->setKey($key)
|
||||
->setLabel($form->getData())
|
||||
;
|
||||
$viewData->add($altName);
|
||||
|
||||
if (is_array($viewData)) {
|
||||
$viewData[] = $altName;
|
||||
} else {
|
||||
$viewData->add($altName);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@@ -6,6 +6,7 @@ use Symfony\Component\Form\AbstractType;
|
||||
use Symfony\Component\OptionsResolver\OptionsResolver;
|
||||
use Symfony\Component\Form\FormBuilderInterface;
|
||||
use Symfony\Component\Form\Extension\Core\Type\TextType;
|
||||
use Symfony\Component\Form\Extension\Core\Type\HiddenType;
|
||||
use Chill\PersonBundle\Config\ConfigPersonAltNamesHelper;
|
||||
use Chill\MainBundle\Templating\TranslatableStringHelper;
|
||||
|
||||
@@ -38,7 +39,9 @@ class PersonAltNameType extends AbstractType
|
||||
public function buildForm(FormBuilderInterface $builder, array $options)
|
||||
{
|
||||
foreach ($this->getKeyChoices() as $label => $key) {
|
||||
$builder->add($key, TextType::class, [
|
||||
$builder->add(
|
||||
$key,
|
||||
$options['force_hidden'] ? HiddenType::class : TextType::class, [
|
||||
'label' => $label,
|
||||
'required' => false
|
||||
]);
|
||||
@@ -64,6 +67,9 @@ class PersonAltNameType extends AbstractType
|
||||
{
|
||||
$resolver
|
||||
->setDefault('class', \Chill\PersonBundle\Entity\PersonAltName::class)
|
||||
->setDefined('force_hidden')
|
||||
->setAllowedTypes('force_hidden', 'bool')
|
||||
->setDefault('force_hidden', false)
|
||||
;
|
||||
}
|
||||
|
||||
|
Reference in New Issue
Block a user