src/Twig/Extension/ProductPageExtension.php line 74

Open in your IDE?
  1. <?php
  2. /**
  3.  * Pimcore
  4.  *
  5.  * This source file is available under two different licenses:
  6.  * - GNU General Public License version 3 (GPLv3)
  7.  * - Pimcore Enterprise License (PEL)
  8.  * Full copyright and license information is available in
  9.  * LICENSE.md which is distributed with this source code.
  10.  *
  11.  *  @copyright  Copyright (c) Pimcore GmbH (http://www.pimcore.org)
  12.  *  @license    http://www.pimcore.org/license     GPLv3 and PEL
  13.  */
  14. namespace App\Twig\Extension;
  15. use App\Website\LinkGenerator\ProductLinkGenerator;
  16. use Pimcore\Bundle\EcommerceFrameworkBundle\Model\ProductInterface;
  17. use Pimcore\Translation\Translator;
  18. use Twig\Extension\AbstractExtension;
  19. use Twig\TwigFilter;
  20. use Twig\TwigFunction;
  21. class ProductPageExtension extends AbstractExtension
  22. {
  23.     /**
  24.      * @var ProductLinkGenerator
  25.      */
  26.     protected $productLinkGenerator;
  27.     /**
  28.      * @var Translator
  29.      */
  30.     protected $translator;
  31.     /**
  32.      * ProductPageExtension constructor.
  33.      *
  34.      * @param ProductLinkGenerator $productLinkGenerator
  35.      * @param Translator $translator
  36.      */
  37.     public function __construct(ProductLinkGenerator $productLinkGeneratorTranslator $translator)
  38.     {
  39.         $this->productLinkGenerator $productLinkGenerator;
  40.         $this->translator $translator;
  41.     }
  42.     /**
  43.      * @return TwigFunction[]
  44.      */
  45.     public function getFunctions()
  46.     {
  47.         return [
  48.             new TwigFunction('app_product_detaillink', [$this'generateLink']),
  49.         ];
  50.     }
  51.     /**
  52.      * @return TwigFilter[]
  53.      */
  54.     public function getFilters()
  55.     {
  56.         return [
  57.             new TwigFilter('colorname', [$this'getColorName'])
  58.         ];
  59.     }
  60.     /**
  61.      * @param ProductInterface $product
  62.      *
  63.      * @return string
  64.      */
  65.     public function generateLink(ProductInterface $product): string
  66.     {
  67.         return $this->productLinkGenerator->generateWithMockup($product, []);
  68.     }
  69.     /**
  70.      * @param array|null $colorNames
  71.      *
  72.      * @return string
  73.      */
  74.     public function getColorName(?array $colorNames = []): string
  75.     {
  76.         $translatedColors = [];
  77.         if ($colorNames) {
  78.             foreach ($colorNames as $color) {
  79.                 $translatedColors[] = $this->translator->trans(mb_strtolower('attribute.' $color));
  80.             }
  81.         }
  82.         return implode('-'$translatedColors);
  83.     }
  84. }