<?php declare(strict_types=1);
namespace Crehler\MojeBambinoEngine\Storefront\Controller;
use Crehler\MojeBambinoEngine\Util\Lifecycle\ActivateDeactivate;
use Shopware\Core\Content\Category\Service\NavigationLoader;
use Shopware\Core\Content\Category\Tree\Tree;
use Shopware\Storefront\Framework\Cache\Annotation\HttpCache;
use Shopware\Core\Framework\Routing\Annotation\RouteScope;
use Shopware\Core\System\SalesChannel\SalesChannelContext;
use Shopware\Storefront\Controller\StorefrontController;
use Symfony\Component\Routing\Annotation\Route;
/**
* @RouteScope(scopes={"storefront"})
*/
class SubNavigationController extends StorefrontController
{
public const PRODUCT_CATEGORY = '423b226905de450685700be5cef91bc5';
protected NavigationLoader $navigationLoader;
public function __construct(NavigationLoader $navigationLoader)
{
$this->navigationLoader = $navigationLoader;
}
/**
* @HttpCache()
* @Route("/widgets/mojebambino/sub-navigation/{categoryId}/{activeCategoryId}", name="frontend.mojebambino.sub-navigation", options={"seo"="false"}, methods={"GET"}, defaults={"XmlHttpRequest": true})
*/
public function productTree(string $categoryId, string $activeCategoryId, SalesChannelContext $context)
{
$tree = $this->navigationLoader->load($categoryId, $context, $categoryId, 3);
if (isset($tree->getActive()->getCustomFields()[ActivateDeactivate::INTERMEDIATE_CATEGORY])
&& ($tree->getActive()->getCustomFields()[ActivateDeactivate::INTERMEDIATE_CATEGORY])) {
$tree = $this->navigationLoader->load(
$tree->getActive()->getParentId(),
$context,
$tree->getActive()->getParentId(),
3
);
}
$tree = $this->prepareTree($tree, $activeCategoryId);
return $this->renderStorefront(
'@Storefront/storefront/layout/sidebar/category-sub-navigation-ajax.html.twig',
[
'navigationTree' => $tree->getTree()
]
);
}
private function prepareTree(Tree $tree, string $activeCategoryId): Tree
{
$newTree = $tree->getTree();
// unset Produkty Category from base tree
if (isset($newTree[self::PRODUCT_CATEGORY])) {
unset($newTree[self::PRODUCT_CATEGORY]);
}
unset($newTree[$activeCategoryId]);
$tree->setTree($newTree);
return $tree;
}
}