src/EventSubscriber/FenceOfferElementSubscriber.php line 29

Open in your IDE?
  1. <?php
  2. namespace App\EventSubscriber;
  3. use ApiPlatform\Core\EventListener\EventPriorities;
  4. use App\Entity\FenceOfferElement;
  5. use App\Entity\SectionLayout;
  6. use Doctrine\ORM\EntityManagerInterface;
  7. use Symfony\Component\EventDispatcher\EventSubscriberInterface;
  8. use Symfony\Component\HttpFoundation\Request;
  9. use Symfony\Component\HttpKernel\Event\ViewEvent;
  10. use Symfony\Component\HttpKernel\KernelEvents;
  11. class FenceOfferElementSubscriber implements EventSubscriberInterface
  12. {
  13.     private $entityManager;
  14.     public function __construct(EntityManagerInterface $entityManager)
  15.     {
  16.         $this->entityManager $entityManager;
  17.     }
  18.     public static function getSubscribedEvents()
  19.     {
  20.         return [
  21.             KernelEvents::VIEW => ['addSectionLayouts'EventPriorities::PRE_SERIALIZE],
  22.         ];
  23.     }
  24.     public function addSectionLayouts(ViewEvent $event)
  25.     {
  26.         $fenceOfferElement  $event->getControllerResult();
  27.         $method $event->getRequest()->getMethod();
  28.         if ($fenceOfferElement instanceof FenceOfferElement && $method === Request::METHOD_GET) {
  29.             $sectionLayouts $this->entityManager->getRepository(SectionLayout::class)->findByType($fenceOfferElement->getFenceElement()->getType() ?? ''$fenceOfferElement->getFenceElement()->getElementType() ?? '');
  30.             $formConfigJson $fenceOfferElement->getFenceElement()->getFormConfigJson();
  31.             $formConfigJson['section_layouts'] = $this->prepareSectionLayouts($sectionLayouts);
  32.             $fenceOfferElement->getFenceElement()->setFormConfigJson($formConfigJson);
  33.         }
  34.     }
  35.     private function prepareSectionLayouts(array $sectionLayouts): array
  36.     {
  37.         $elements = [];
  38.         foreach ($sectionLayouts as $sectionLayout) {
  39.             $elements[] = [
  40.                 'id' => $sectionLayout->getId(),
  41.                 'name' => $sectionLayout->getName(),
  42.                 'data' => $sectionLayout->getLayout(),
  43.                 'type' => $sectionLayout->getType(),
  44.             ];
  45.         }
  46.         return $elements;
  47.     }
  48. }