custom/plugins/MaxiaVariantsTable6/src/Core/Content/VariantsTable/Column/ColumnRenderer.php line 33

Open in your IDE?
  1. <?php
  2. namespace Maxia\MaxiaVariantsTable6\Core\Content\VariantsTable\Column;
  3. use Shopware\Core\Content\Product\SalesChannel\SalesChannelProductEntity;
  4. use Shopware\Core\Content\Seo\SeoUrlPlaceholderHandlerInterface;
  5. use Shopware\Core\Framework\Adapter\Twig\TemplateFinder;
  6. use Shopware\Core\System\SalesChannel\SalesChannelContext;
  7. use Shopware\Storefront\Framework\Routing\RequestTransformer;
  8. use Symfony\Component\DependencyInjection\ContainerInterface;
  9. use Symfony\Component\HttpFoundation\Request;
  10. use Twig\Environment;
  11. class ColumnRenderer
  12. {
  13.     /** @var SalesChannelContext */
  14.     protected $salesChannelContext;
  15.     /** @var ContainerInterface */
  16.     protected $container;
  17.     /** @var string */
  18.     protected $templatePrefix '@Storefront/storefront/plugins/maxia_variants_table/columns/';
  19.     /** @var Environment */
  20.     protected $twig;
  21.     public function __construct(ContainerInterface $container)
  22.     {
  23.         $this->container $container;
  24.     }
  25.     public function render(
  26.         Column $column,
  27.         SalesChannelProductEntity $product,
  28.         SalesChannelProductEntity $parentProduct,
  29.         SalesChannelContext $salesChannelContext,
  30.         array $parameters = [])
  31.     {
  32.         $value $column->getDataMapping()
  33.             ? $column->getDataMapping()->getValue($product$parentProduct$salesChannelContext)
  34.             : null;
  35.         $parameters array_merge($parameters, [
  36.             'column' => $column,
  37.             'value' => $value,
  38.             'product' => $product,
  39.             'parentProduct' => $parentProduct,
  40.             'context' => $salesChannelContext
  41.         ]);
  42.         $event = new ColumnRenderEvent($column$product$parameters$salesChannelContext);
  43.         $this->container->get('event_dispatcher')->dispatch($event);
  44.         $template $column->getTemplate() ?: 'default.html.twig';
  45.         $template $this->templatePrefix $template;
  46.         $content $this->renderTemplate($template$event->getParameters(), $salesChannelContext);
  47.         return trim($content);
  48.     }
  49.     public function renderTemplate(string $template, array $parametersSalesChannelContext $salesChannelContext)
  50.     {
  51.         /** @var Request $request */
  52.         $request $this->container->get('request_stack')->getCurrentRequest();
  53.         $view $this->container->get(TemplateFinder::class)->find($templatefalsenull);
  54.         $content $this->getTwig()->render($view$parameters);
  55.         $host $request->attributes->get(RequestTransformer::STOREFRONT_URL) ?? $request->getHttpHost();
  56.         /** @var SeoUrlPlaceholderHandlerInterface $seoUrlReplacer */
  57.         $seoUrlReplacer $this->container->get(SeoUrlPlaceholderHandlerInterface::class);
  58.         return $seoUrlReplacer->replace($content$host$salesChannelContext);
  59.     }
  60.     public function setTwig(Environment $twig)
  61.     {
  62.         $this->twig $twig;
  63.     }
  64.     public function getTwig()
  65.     {
  66.         return $this->twig;
  67.     }
  68. }