eventDispatcher = $eventDispatcher; } /** * add a widget to this class, which become available for a future call. * * This function is used by DependencyInjection\CompilerPass\WidgetCompilerPass, * which add the widget to this class when it is created by from DI, according * to the given config under `chill_main`. * * @param string $place * @param WidgetInterface $widget */ public function addWidget($place, mixed $ordering, $widget, array $config = []) { $this->widget[$place][$ordering] = [$widget, $config]; } public function getFunctions() { return [ new TwigFunction( 'chill_delegated_block', $this->renderingWidget(...), [ 'is_safe' => ['html'], 'needs_environment' => true, 'deprecated' => true, 'alternative' => 'chill_widget', ] ), new TwigFunction( 'chill_widget', $this->renderingWidget(...), ['is_safe' => ['html'], 'needs_environment' => true] ), ]; } public function getName() { return 'chill_main_widget'; } public function renderingWidget(Environment $env, $block, array $context = []) { // get the content of widgets $content = ''; foreach ($this->getWidgetsArraysOrdered($block) as $a) { /** @var Widget\WidgetInterface $widget */ $widget = $a[0]; $config = $a[1]; $content .= $widget->render($env, $block, $context, $config); } // for old rendering events (deprecated) $event = new DelegatedBlockRenderingEvent($context); $this->eventDispatcher->dispatch($event, 'chill_block.'.$block); return $content.' '.$event->getContent(); } /** * @param string $place * * @return array */ protected function getWidgetsArraysOrdered($place) { if (!\array_key_exists($place, $this->widget)) { $this->widget[$place] = []; } \ksort($this->widget[$place]); return $this->widget[$place]; } }