src/Twig/Extension/CategoryExtension.php line 65

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\Model\Product\Category;
  16. use Pimcore\Model\DataObject\Folder;
  17. use App\Website\LinkGenerator\CategoryLinkGenerator;
  18. use Twig\Extension\AbstractExtension;
  19. use Twig\TwigFunction;
  20. class CategoryExtension extends AbstractExtension
  21. {
  22.     /**
  23.      * @var CategoryLinkGenerator
  24.      */
  25.     protected $categoryLinkGenerator;
  26.     /**
  27.      * CategoryFilterExtension constructor.
  28.      *
  29.      * @param CategoryLinkGenerator $categoryLinkGenerator
  30.      */
  31.     public function __construct(CategoryLinkGenerator $categoryLinkGenerator)
  32.     {
  33.         $this->categoryLinkGenerator = $categoryLinkGenerator;
  34.     }
  35.     /**
  36.      * @return TwigFunction[]
  37.      */
  38.     public function getFunctions()
  39.     {
  40.         return [
  41.             new TwigFunction('app_get_main_categories', [$this, 'prepareData']),
  42.         ];
  43.     }
  44.     /**
  45.      * @param Document $document
  46.      * @return \stdClass
  47.      */
  48.     public function prepareData($document = null)
  49.     {
  50.         $root = $document->getProperty('products_root_category');
  51.         $categories = [];
  52.         $categories = array_filter($root->getChildren(), function ($item) {
  53.             return $item instanceof Category && $item->isPublished();
  54.         });
  55.         usort($categories, function($a, $b) {return $a->getId() >= $b->getId();});
  56.         return $categories;
  57.     }
  58. }