You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

52 lines
1.6KB

  1. <?php
  2. namespace Lc\SovBundle\EventSubscriber;
  3. use Doctrine\ORM\EntityManagerInterface;
  4. use Lc\SovBundle\Doctrine\EntityInterface;
  5. use Lc\SovBundle\Doctrine\Extension\SluggableInterface;
  6. use Lc\SovBundle\Doctrine\Extension\SortableInterface;
  7. use Lc\SovBundle\Doctrine\Extension\StatusInterface;
  8. use Lc\SovBundle\Doctrine\Extension\TreeInterface;
  9. use Lc\SovBundle\Event\EntityComponentEvent;
  10. use Lc\SovBundle\Event\EntityManager\EntityManagerEvent;
  11. use Lc\SovBundle\Repository\AbstractRepositoryInterface;
  12. use Symfony\Component\EventDispatcher\EventSubscriberInterface;
  13. class SluggablePropertyEventSubscriber implements EventSubscriberInterface
  14. {
  15. protected $em;
  16. protected $adminUrlGenerator;
  17. public function __construct(EntityManagerInterface $entityManager)
  18. {
  19. $this->em = $entityManager;
  20. }
  21. public static function getSubscribedEvents()
  22. {
  23. return [
  24. EntityComponentEvent::DUPLICATE_EVENT => ['setSluggablePropertyDuplicateEvent'],
  25. EntityManagerEvent::POST_UPDATE_EVENT => ['setSluggablePropertyUpdateEvent'],
  26. ];
  27. }
  28. public function setSluggablePropertyDuplicateEvent(EntityComponentEvent $event)
  29. {
  30. $entity = $event->getEntity();
  31. if ($entity instanceof SluggableInterface) {
  32. $entity->clearId(null);
  33. $entity->setSlug(null);
  34. }
  35. }
  36. public function setSluggablePropertyUpdateEvent(EntityManagerEvent $event)
  37. {
  38. $entity = $event->getEntity();
  39. if ($entity instanceof SluggableInterface) {
  40. $entity->setSlug(null);
  41. }
  42. }
  43. }