Implement snip tags with very elegant tags form
This commit is contained in:
@ -39,9 +39,16 @@ class Snip
|
||||
#[ORM\Column]
|
||||
private bool $archived = false;
|
||||
|
||||
/**
|
||||
* @var Collection<int, Tag>
|
||||
*/
|
||||
#[ORM\ManyToMany(targetEntity: Tag::class, mappedBy: 'snips')]
|
||||
private Collection $tags;
|
||||
|
||||
public function __construct()
|
||||
{
|
||||
$this->snipContents = new ArrayCollection();
|
||||
$this->tags = new ArrayCollection();
|
||||
}
|
||||
|
||||
public function __toString(): string
|
||||
@ -160,4 +167,31 @@ class Snip
|
||||
|
||||
return $this;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return Collection<int, Tag>
|
||||
*/
|
||||
public function getTags(): Collection
|
||||
{
|
||||
return $this->tags;
|
||||
}
|
||||
|
||||
public function addTag(Tag $tag): static
|
||||
{
|
||||
if (!$this->tags->contains($tag)) {
|
||||
$this->tags->add($tag);
|
||||
$tag->addSnip($this);
|
||||
}
|
||||
|
||||
return $this;
|
||||
}
|
||||
|
||||
public function removeTag(Tag $tag): static
|
||||
{
|
||||
if ($this->tags->removeElement($tag)) {
|
||||
$tag->removeSnip($this);
|
||||
}
|
||||
|
||||
return $this;
|
||||
}
|
||||
}
|
||||
|
94
src/Entity/Tag.php
Normal file
94
src/Entity/Tag.php
Normal file
@ -0,0 +1,94 @@
|
||||
<?php
|
||||
|
||||
namespace App\Entity;
|
||||
|
||||
use App\Repository\TagRepository;
|
||||
use Doctrine\Common\Collections\ArrayCollection;
|
||||
use Doctrine\Common\Collections\Collection;
|
||||
use Doctrine\ORM\Mapping as ORM;
|
||||
|
||||
#[ORM\Entity(repositoryClass: TagRepository::class)]
|
||||
#[ORM\UniqueConstraint(name: 'user_tag_unique', columns: ['name', 'user_id'])]
|
||||
class Tag
|
||||
{
|
||||
#[ORM\Id]
|
||||
#[ORM\GeneratedValue]
|
||||
#[ORM\Column]
|
||||
private ?int $id = null;
|
||||
|
||||
#[ORM\Column(length: 255)]
|
||||
private ?string $name = null;
|
||||
|
||||
#[ORM\ManyToOne]
|
||||
#[ORM\JoinColumn(nullable: false)]
|
||||
private ?User $user = null;
|
||||
|
||||
/**
|
||||
* @var Collection<int, Snip>
|
||||
*/
|
||||
#[ORM\ManyToMany(targetEntity: Snip::class, inversedBy: 'tags')]
|
||||
private Collection $snips;
|
||||
|
||||
public function __construct()
|
||||
{
|
||||
$this->snips = new ArrayCollection();
|
||||
}
|
||||
|
||||
public function __toString(): string
|
||||
{
|
||||
return $this->name ?? '';
|
||||
}
|
||||
|
||||
public function getId(): ?int
|
||||
{
|
||||
return $this->id;
|
||||
}
|
||||
|
||||
public function getName(): ?string
|
||||
{
|
||||
return $this->name;
|
||||
}
|
||||
|
||||
public function setName(string $name): static
|
||||
{
|
||||
$this->name = $name;
|
||||
|
||||
return $this;
|
||||
}
|
||||
|
||||
public function getUser(): ?User
|
||||
{
|
||||
return $this->user;
|
||||
}
|
||||
|
||||
public function setUser(?User $user): static
|
||||
{
|
||||
$this->user = $user;
|
||||
|
||||
return $this;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return Collection<int, Snip>
|
||||
*/
|
||||
public function getSnips(): Collection
|
||||
{
|
||||
return $this->snips;
|
||||
}
|
||||
|
||||
public function addSnip(Snip $snip): static
|
||||
{
|
||||
if (!$this->snips->contains($snip)) {
|
||||
$this->snips->add($snip);
|
||||
}
|
||||
|
||||
return $this;
|
||||
}
|
||||
|
||||
public function removeSnip(Snip $snip): static
|
||||
{
|
||||
$this->snips->removeElement($snip);
|
||||
|
||||
return $this;
|
||||
}
|
||||
}
|
@ -28,6 +28,7 @@ class SnipType extends AbstractType
|
||||
'attr' => ['rows' => 20],
|
||||
'mapped' => false,
|
||||
])
|
||||
->add('tags', TagsType::class)
|
||||
->add('public', SwitchType::class)
|
||||
->add('visible', SwitchType::class)
|
||||
;
|
||||
|
79
src/Form/TagsType.php
Normal file
79
src/Form/TagsType.php
Normal file
@ -0,0 +1,79 @@
|
||||
<?php
|
||||
|
||||
namespace App\Form;
|
||||
|
||||
use App\Entity\Tag;
|
||||
use App\Repository\TagRepository;
|
||||
use Doctrine\Common\Collections\Collection;
|
||||
use Symfony\Bundle\SecurityBundle\Security;
|
||||
use Symfony\Component\Form\AbstractType;
|
||||
use Symfony\Component\Form\DataTransformerInterface;
|
||||
use Symfony\Component\Form\Extension\Core\Type\TextType;
|
||||
use Symfony\Component\Form\FormBuilderInterface;
|
||||
use Symfony\Component\OptionsResolver\OptionsResolver;
|
||||
|
||||
class TagsType extends AbstractType implements DataTransformerInterface
|
||||
{
|
||||
public function __construct(
|
||||
private readonly TagRepository $repository,
|
||||
private readonly Security $security,
|
||||
) {}
|
||||
|
||||
public function transform($value): string
|
||||
{
|
||||
if ($value === null) {
|
||||
return '';
|
||||
}
|
||||
|
||||
if ($value instanceof Collection) {
|
||||
$value = $value->toArray();
|
||||
}
|
||||
|
||||
if (is_array($value)) {
|
||||
$tags = array_map(fn(Tag $tag) => $tag->getName(), $value);
|
||||
} else {
|
||||
return '';
|
||||
}
|
||||
return implode(', ', $tags);
|
||||
}
|
||||
|
||||
public function reverseTransform($value): array
|
||||
{
|
||||
$tags = array_filter(array_map('trim', explode(',', $value)));
|
||||
$user = $this->security->getUser();
|
||||
|
||||
$tagEntities = [];
|
||||
foreach ($tags as $tag) {
|
||||
$tagEntity = $this->repository->findOneBy(['name' => $tag, 'user' => $user]);
|
||||
if ($tagEntity === null) {
|
||||
$tagEntity = new Tag()
|
||||
->setName($tag)
|
||||
->setUser($user)
|
||||
;
|
||||
$this->repository->save($tagEntity);
|
||||
}
|
||||
$tagEntities[] = $tagEntity;
|
||||
}
|
||||
|
||||
return $tagEntities;
|
||||
}
|
||||
|
||||
public function buildForm(FormBuilderInterface $builder, array $options): void
|
||||
{
|
||||
$builder->addModelTransformer($this);
|
||||
}
|
||||
|
||||
public function configureOptions(OptionsResolver $resolver): void
|
||||
{
|
||||
$resolver->setDefaults([
|
||||
'data_class' => null, // No specific entity class
|
||||
'label' => 'Tags (comma-separated)',
|
||||
'attr' => ['class' => 'tags-input'], // Optional: Add custom attributes
|
||||
]);
|
||||
}
|
||||
|
||||
public function getParent(): string
|
||||
{
|
||||
return TextType::class;
|
||||
}
|
||||
}
|
49
src/Repository/TagRepository.php
Normal file
49
src/Repository/TagRepository.php
Normal file
@ -0,0 +1,49 @@
|
||||
<?php
|
||||
|
||||
namespace App\Repository;
|
||||
|
||||
use App\Entity\Tag;
|
||||
use Doctrine\Bundle\DoctrineBundle\Repository\ServiceEntityRepository;
|
||||
use Doctrine\Persistence\ManagerRegistry;
|
||||
|
||||
/**
|
||||
* @extends ServiceEntityRepository<Tag>
|
||||
*/
|
||||
class TagRepository extends ServiceEntityRepository
|
||||
{
|
||||
public function __construct(ManagerRegistry $registry)
|
||||
{
|
||||
parent::__construct($registry, Tag::class);
|
||||
}
|
||||
|
||||
public function save(Tag $tag): void
|
||||
{
|
||||
$this->getEntityManager()->persist($tag);
|
||||
$this->getEntityManager()->flush();
|
||||
}
|
||||
|
||||
// /**
|
||||
// * @return Tag[] Returns an array of Tag objects
|
||||
// */
|
||||
// public function findByExampleField($value): array
|
||||
// {
|
||||
// return $this->createQueryBuilder('t')
|
||||
// ->andWhere('t.exampleField = :val')
|
||||
// ->setParameter('val', $value)
|
||||
// ->orderBy('t.id', 'ASC')
|
||||
// ->setMaxResults(10)
|
||||
// ->getQuery()
|
||||
// ->getResult()
|
||||
// ;
|
||||
// }
|
||||
|
||||
// public function findOneBySomeField($value): ?Tag
|
||||
// {
|
||||
// return $this->createQueryBuilder('t')
|
||||
// ->andWhere('t.exampleField = :val')
|
||||
// ->setParameter('val', $value)
|
||||
// ->getQuery()
|
||||
// ->getOneOrNullResult()
|
||||
// ;
|
||||
// }
|
||||
}
|
Reference in New Issue
Block a user