mirror of
https://gitlab.com/Chill-Projet/chill-bundles.git
synced 2025-08-20 14:43:49 +00:00
Fix loading data for customfield, using proper DI
This commit is contained in:
@@ -30,29 +30,21 @@ use Symfony\Contracts\Translation\TranslatorInterface;
|
||||
use Twig\Environment;
|
||||
|
||||
class LoadCustomFields extends AbstractFixture implements
|
||||
ContainerAwareInterface,
|
||||
OrderedFixtureInterface
|
||||
{
|
||||
/**
|
||||
* @var ContainerInterface
|
||||
*/
|
||||
private $container;
|
||||
private $cfText;
|
||||
|
||||
private $cfChoice;
|
||||
|
||||
/**
|
||||
* @var CustomField
|
||||
*/
|
||||
private $customFieldChoice;
|
||||
|
||||
/**
|
||||
* @var CustomField
|
||||
*/
|
||||
private $customFieldText;
|
||||
|
||||
/**
|
||||
* LoadCustomFields constructor.
|
||||
*/
|
||||
public function __construct(private readonly TranslatableStringHelper $translatableStringHelper)
|
||||
{
|
||||
public function __construct(
|
||||
private readonly EntityManagerInterface $entityManager,
|
||||
private readonly CustomFieldChoice $customFieldChoice,
|
||||
private readonly CustomFieldText $customFieldText,
|
||||
) {
|
||||
}
|
||||
|
||||
//put your code here
|
||||
@@ -68,41 +60,26 @@ class LoadCustomFields extends AbstractFixture implements
|
||||
$manager->flush();
|
||||
}
|
||||
|
||||
public function setContainer(?ContainerInterface $container = null)
|
||||
{
|
||||
if (null === $container) {
|
||||
throw new RuntimeException('The given container should not be null');
|
||||
}
|
||||
|
||||
$this->container = $container;
|
||||
}
|
||||
|
||||
private function createCustomFieldChoice()
|
||||
{
|
||||
return new CustomFieldChoice(
|
||||
$this->container->get(Environment::class),
|
||||
$this->translatableStringHelper
|
||||
);
|
||||
return $this->customFieldChoice;
|
||||
}
|
||||
|
||||
private function createCustomFieldText()
|
||||
{
|
||||
return new CustomFieldText(
|
||||
$this->container->get(Environment::class),
|
||||
$this->translatableStringHelper
|
||||
);
|
||||
return $this->customFieldText;
|
||||
}
|
||||
|
||||
private function loadData(ObjectManager $manager)
|
||||
{
|
||||
$personIds = $this->container->get(EntityManagerInterface::class)
|
||||
$personIds = $this->entityManager
|
||||
->createQuery('SELECT person.id FROM ChillPersonBundle:Person person')
|
||||
->getScalarResult();
|
||||
|
||||
// get possible values for cfGroup
|
||||
$choices = array_map(
|
||||
static fn ($a) => $a['slug'],
|
||||
$this->customFieldChoice->getOptions()['choices']
|
||||
$this->cfChoice->getOptions()['choices']
|
||||
);
|
||||
// create faker
|
||||
$faker = \Faker\Factory::create('fr_FR');
|
||||
@@ -114,9 +91,9 @@ class LoadCustomFields extends AbstractFixture implements
|
||||
$person = $manager->getRepository(Person::class)->find($id);
|
||||
$person->setCFData([
|
||||
'remarques' => $this->createCustomFieldText()
|
||||
->serialize($faker->text(random_int(150, 250)), $this->customFieldText),
|
||||
->serialize($faker->text(random_int(150, 250)), $this->cfText),
|
||||
'document-d-identite' => $this->createCustomFieldChoice()
|
||||
->serialize([$choices[array_rand($choices)]], $this->customFieldChoice),
|
||||
->serialize([$choices[array_rand($choices)]], $this->cfChoice),
|
||||
]);
|
||||
}
|
||||
}
|
||||
@@ -148,7 +125,7 @@ class LoadCustomFields extends AbstractFixture implements
|
||||
$manager->persist($customField0);
|
||||
|
||||
// create text field
|
||||
$this->customFieldText = (new CustomField())
|
||||
$this->cfText = $customFieldText = (new CustomField())
|
||||
->setActive(true)
|
||||
->setName(['fr' => 'Remarques'])
|
||||
->setSlug('remarques')
|
||||
@@ -156,10 +133,10 @@ class LoadCustomFields extends AbstractFixture implements
|
||||
->setType('text')
|
||||
->setOptions(['maxLength' => 5000])
|
||||
->setCustomFieldsGroup($cfGroup);
|
||||
$manager->persist($this->customFieldText);
|
||||
$manager->persist($customFieldText);
|
||||
|
||||
// create choice field
|
||||
$this->customFieldChoice = (new CustomField())
|
||||
$this->cfChoice = $customFieldChoice = (new CustomField())
|
||||
->setActive(true)
|
||||
->setName(['fr' => "Document d'identité"])
|
||||
->setSlug('document-d-identite')
|
||||
@@ -190,6 +167,6 @@ class LoadCustomFields extends AbstractFixture implements
|
||||
],
|
||||
],
|
||||
]);
|
||||
$manager->persist($this->customFieldChoice);
|
||||
$manager->persist($customFieldChoice);
|
||||
}
|
||||
}
|
||||
|
Reference in New Issue
Block a user