vendor/ibexa/dashboard/src/bundle/EventSubscriber/PageBuilder/ContentBlocksSubscriber.php line 51

Open in your IDE?
  1. <?php
  2. /**
  3.  * @copyright Copyright (C) Ibexa AS. All rights reserved.
  4.  * @license For full copyright and license information view LICENSE file distributed with this source code.
  5.  */
  6. declare(strict_types=1);
  7. namespace Ibexa\Bundle\Dashboard\EventSubscriber\PageBuilder;
  8. use Ibexa\AdminUi\Form\Type\Content\Draft\ContentEditType;
  9. use Ibexa\FieldTypePage\FieldType\Page\Block\Renderer\BlockRenderEvents;
  10. use Ibexa\FieldTypePage\FieldType\Page\Block\Renderer\Event\PreRenderEvent;
  11. use Symfony\Component\EventDispatcher\EventSubscriberInterface;
  12. use Symfony\Component\Form\FormFactoryInterface;
  13. use Symfony\Component\Routing\Generator\UrlGeneratorInterface;
  14. use Symfony\Component\String\UnicodeString;
  15. /**
  16.  * @internal
  17.  */
  18. final class ContentBlocksSubscriber implements EventSubscriberInterface
  19. {
  20.     private const MY_CONTENT_BLOCK_IDENTIFIER 'my_content';
  21.     private const COMMON_CONTENT_BLOCK_IDENTIFIER 'common_content';
  22.     private FormFactoryInterface $formFactory;
  23.     private UrlGeneratorInterface $urlGenerator;
  24.     public function __construct(
  25.         FormFactoryInterface $formFactory,
  26.         UrlGeneratorInterface $urlGenerator
  27.     ) {
  28.         $this->formFactory $formFactory;
  29.         $this->urlGenerator $urlGenerator;
  30.     }
  31.     public static function getSubscribedEvents(): array
  32.     {
  33.         return [
  34.             BlockRenderEvents::getBlockPreRenderEventName(self::MY_CONTENT_BLOCK_IDENTIFIER) => [
  35.                 ['onBlockPreRender', -100],
  36.             ],
  37.             BlockRenderEvents::getBlockPreRenderEventName(self::COMMON_CONTENT_BLOCK_IDENTIFIER) => [
  38.                 ['onBlockPreRender', -100],
  39.             ],
  40.         ];
  41.     }
  42.     public function onBlockPreRender(PreRenderEvent $event): void
  43.     {
  44.         /** @var \Ibexa\FieldTypePage\FieldType\Page\Block\Renderer\Twig\TwigRenderRequest $twigRenderRequest */
  45.         $twigRenderRequest $event->getRenderRequest();
  46.         $blockValue $event->getBlockValue();
  47.         $form $this->formFactory->createNamed(
  48.             $this->sanitizeFormName(sprintf('content_edit_%s_%s'$blockValue->getType(), $blockValue->getId())),
  49.             ContentEditType::class,
  50.             null,
  51.             [
  52.                 'action' => $this->urlGenerator->generate('ibexa.content.edit'),
  53.             ],
  54.         );
  55.         $twigRenderRequest->addParameter('form_edit'$form->createView());
  56.     }
  57.     private function sanitizeFormName(string $formName): string
  58.     {
  59.         return (string)(new UnicodeString($formName))->replaceMatches('/[^A-Za-z0-9_:-]/''_');
  60.     }
  61. }