src/EventSubscriber/Content/ContentInTextModulesSubscriber.php line 47

Open in your IDE?
  1. <?php
  2. namespace App\EventSubscriber\Content;
  3. use App\Entity\Content\Text;
  4. use Symfony\Component\EventDispatcher\EventSubscriberInterface;
  5. use App\Event\Content\ContentDisplayEvent;
  6. use Twig\Environment;
  7. use App\Twig\ModuleExtension;
  8. /**
  9.  * @author Przemysław Chrupek <przemyslaw.chrupek@avt.pl>
  10.  */
  11. class ContentInTextModulesSubscriber implements EventSubscriberInterface
  12. {
  13.     /**
  14.      * @var Content
  15.      */
  16.     private $content;
  17.     /**
  18.      * @var Environment
  19.      */
  20.     private $environment;
  21.     /**
  22.      * @var int
  23.      */
  24.     private $moduleCounter;
  25.     public function __construct(Environment $environmentModuleExtension $moduleExtension)
  26.     {
  27.         $this->environment $environment;
  28.         $this->moduleExtension $moduleExtension;
  29.         $this->moduleCounter 0;
  30.     }
  31.     public static function getSubscribedEvents()
  32.     {
  33.         return [
  34.             ContentDisplayEvent::NAME => [
  35.                 ['preContentDisplay'100],
  36.             ]
  37.         ];
  38.     }
  39.     public function preContentDisplay(ContentDisplayEvent $event)
  40.     {
  41.         $this->content $event->getContent();
  42.         $text $this->content->getText();
  43.         if (!$text instanceof Text) {
  44.             return;
  45.         }
  46.         $pattern '/<pre[^{]*?>{\$(in-article-module)}.*?<\/pre>/msi';
  47.         $text->setValue(preg_replace_callback($pattern, [$this'modules'], $text->getValue()));
  48.     }
  49.     protected function modules($matches)
  50.     {
  51.         $html '';
  52.         $this->moduleCounter++;
  53.         $position 'in-article-module-' $this->moduleCounter;
  54.         ob_start();
  55.         $this->moduleExtension->renderModulePosition($this->environment$position);
  56.         $html .= ob_get_clean();
  57.         return $html;
  58.     }
  59. }