|
@@ -3,19 +3,29 @@
|
|
|
namespace App\Form;
|
|
|
|
|
|
use App\Entity\Article;
|
|
|
+use App\Entity\Tag;
|
|
|
use App\Entity\User;
|
|
|
+use App\Repository\TagRepository;
|
|
|
use App\Service\BaseUrl;
|
|
|
+use Doctrine\ORM\EntityManagerInterface;
|
|
|
use Symfony\Bridge\Doctrine\Form\Type\EntityType;
|
|
|
use Symfony\Component\Form\AbstractType;
|
|
|
use Symfony\Component\Form\Extension\Core\Type\ChoiceType;
|
|
|
use Symfony\Component\Form\Extension\Core\Type\TextareaType;
|
|
|
use Symfony\Component\Form\Extension\Core\Type\TextType;
|
|
|
use Symfony\Component\Form\FormBuilderInterface;
|
|
|
+use Symfony\Component\Form\FormEvent;
|
|
|
+use Symfony\Component\Form\FormEvents;
|
|
|
use Symfony\Component\OptionsResolver\OptionsResolver;
|
|
|
|
|
|
class ArticleType extends AbstractType
|
|
|
{
|
|
|
- public function __construct(protected BaseUrl $baseUrl)
|
|
|
+ public function __construct(
|
|
|
+ protected readonly BaseUrl $baseUrl,
|
|
|
+ protected readonly EntityManagerInterface $em,
|
|
|
+ protected readonly TagRepository $tagRepository
|
|
|
+
|
|
|
+ )
|
|
|
{
|
|
|
|
|
|
}
|
|
@@ -35,6 +45,13 @@ class ArticleType extends AbstractType
|
|
|
'data-url' => $this->baseUrl->getBasePath()
|
|
|
],
|
|
|
])
|
|
|
+ ->add('tags', TextType::class, [
|
|
|
+ 'mapped' => false,
|
|
|
+ 'required' => false,
|
|
|
+ 'attr' => [
|
|
|
+ 'placeholder' => 'Entrez des tag séparés par des virgules'
|
|
|
+ ]
|
|
|
+ ])
|
|
|
->add('content', TextareaType::class, [
|
|
|
'required' => false,
|
|
|
'attr' => [
|
|
@@ -56,6 +73,44 @@ class ArticleType extends AbstractType
|
|
|
'choice_value' => null
|
|
|
])
|
|
|
;
|
|
|
+
|
|
|
+ // Listener pour pré-remplir le champ texte lors de l'édition
|
|
|
+ $builder->addEventListener(FormEvents::POST_SET_DATA, function (FormEvent $event): void {
|
|
|
+ /** @var Article $article */
|
|
|
+ $article = $event->getData();
|
|
|
+ $form = $event->getForm();
|
|
|
+ if ($article && $article->getTags()) {
|
|
|
+ $tags = $article->getTags()->map(fn($tag) => $tag->getName())->toArray();
|
|
|
+ if ($form->has('tags')) {
|
|
|
+ $form->get('tags')->setData(implode(', ', $tags));
|
|
|
+ }
|
|
|
+ }
|
|
|
+ });
|
|
|
+
|
|
|
+ // Écouteur d'événement pour permettre la création de nouveaux tags
|
|
|
+ $builder->addEventListener(FormEvents::SUBMIT, function (FormEvent $event): void {
|
|
|
+ $article = $event->getData();
|
|
|
+ $form = $event->getForm();
|
|
|
+
|
|
|
+ $article->clearTags();
|
|
|
+
|
|
|
+ $tagsInput = $form->get('tags')->getData();
|
|
|
+ if ($tagsInput) {
|
|
|
+ $tags = array_map('trim', explode(',', $tagsInput));
|
|
|
+
|
|
|
+ foreach ($tags as $tagName) {
|
|
|
+ $tag = $this->tagRepository->findOneBy(['name' => $tagName]);
|
|
|
+
|
|
|
+ if (!$tag) {
|
|
|
+ $tag = new Tag();
|
|
|
+ $tag->setName($tagName);
|
|
|
+ $this->em->persist($tag);
|
|
|
+ }
|
|
|
+
|
|
|
+ $article->addTag($tag);
|
|
|
+ }
|
|
|
+ }
|
|
|
+ });
|
|
|
}
|
|
|
|
|
|
public function configureOptions(OptionsResolver $resolver): void
|