<?php
/**
* Pimcore
*
* This source file is available under two different licenses:
* - GNU General Public License version 3 (GPLv3)
* - Pimcore Enterprise License (PEL)
* Full copyright and license information is available in
* LICENSE.md which is distributed with this source code.
*
* @copyright Copyright (c) Pimcore GmbH (http://www.pimcore.org)
* @license http://www.pimcore.org/license GPLv3 and PEL
*/
namespace App\Twig\Extension;
use App\Model\Product\Category;
use Pimcore\Model\DataObject\Folder;
use App\Website\LinkGenerator\CategoryLinkGenerator;
use Twig\Extension\AbstractExtension;
use Twig\TwigFunction;
class CategoryExtension extends AbstractExtension
{
/**
* @var CategoryLinkGenerator
*/
protected $categoryLinkGenerator;
/**
* CategoryFilterExtension constructor.
*
* @param CategoryLinkGenerator $categoryLinkGenerator
*/
public function __construct(CategoryLinkGenerator $categoryLinkGenerator)
{
$this->categoryLinkGenerator = $categoryLinkGenerator;
}
/**
* @return TwigFunction[]
*/
public function getFunctions()
{
return [
new TwigFunction('app_get_main_categories', [$this, 'prepareData']),
];
}
/**
* @param Document $document
* @return \stdClass
*/
public function prepareData($document = null)
{
$root = $document->getProperty('products_root_category');
$categories = [];
$categories = array_filter($root->getChildren(), function ($item) {
return $item instanceof Category && $item->isPublished();
});
usort($categories, function($a, $b) {return $a->getId() >= $b->getId();});
return $categories;
}
}