Skip to content

Commit 07233f2

Browse files
committed
Respect Category Top Navigation Max Depth setting
The setting `Stores > Configuration > Catalog > Catalog > Category Top Navigation > Maximal Depth` was actually never being used to limit the topmenu. Now the setting will be used as a condition for the category collection when it is fetched.
1 parent f93b914 commit 07233f2

File tree

2 files changed

+74
-0
lines changed

2 files changed

+74
-0
lines changed

app/code/Magento/Catalog/Model/ResourceModel/Category/Collection.php

Lines changed: 73 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,12 +6,14 @@
66
namespace Magento\Catalog\Model\ResourceModel\Category;
77

88
use Magento\CatalogUrlRewrite\Model\CategoryUrlRewriteGenerator;
9+
use Magento\Store\Model\ScopeInterface;
910

1011
/**
1112
* Category resource collection
1213
*
1314
* @api
1415
* @author Magento Core Team <core@magentocommerce.com>
16+
* @SuppressWarnings(PHPMD.CouplingBetweenObjects)
1517
* @since 100.0.2
1618
*/
1719
class Collection extends \Magento\Catalog\Model\ResourceModel\Collection\AbstractCollection
@@ -58,6 +60,60 @@ class Collection extends \Magento\Catalog\Model\ResourceModel\Collection\Abstrac
5860
*/
5961
protected $_loadWithProductCount = false;
6062

63+
/**
64+
* Core store config
65+
*
66+
* @var \Magento\Framework\App\Config\ScopeConfigInterface
67+
*/
68+
private $scopeConfig;
69+
70+
/**
71+
* Constructor
72+
* @param \Magento\Framework\Data\Collection\EntityFactory $entityFactory
73+
* @param \Psr\Log\LoggerInterface $logger
74+
* @param \Magento\Framework\Data\Collection\Db\FetchStrategyInterface $fetchStrategy
75+
* @param \Magento\Framework\Event\ManagerInterface $eventManager
76+
* @param \Magento\Eav\Model\Config $eavConfig
77+
* @param \Magento\Framework\App\ResourceConnection $resource
78+
* @param \Magento\Eav\Model\EntityFactory $eavEntityFactory
79+
* @param \Magento\Eav\Model\ResourceModel\Helper $resourceHelper
80+
* @param \Magento\Framework\Validator\UniversalFactory $universalFactory
81+
* @param \Magento\Store\Model\StoreManagerInterface $storeManager
82+
* @param \Magento\Framework\App\Config\ScopeConfigInterface $scopeConfig
83+
* @param \Magento\Framework\DB\Adapter\AdapterInterface|null $connection
84+
*
85+
* @SuppressWarnings(PHPMD.ExcessiveParameterList)
86+
*/
87+
public function __construct(
88+
\Magento\Framework\Data\Collection\EntityFactory $entityFactory,
89+
\Psr\Log\LoggerInterface $logger,
90+
\Magento\Framework\Data\Collection\Db\FetchStrategyInterface $fetchStrategy,
91+
\Magento\Framework\Event\ManagerInterface $eventManager,
92+
\Magento\Eav\Model\Config $eavConfig,
93+
\Magento\Framework\App\ResourceConnection $resource,
94+
\Magento\Eav\Model\EntityFactory $eavEntityFactory,
95+
\Magento\Eav\Model\ResourceModel\Helper $resourceHelper,
96+
\Magento\Framework\Validator\UniversalFactory $universalFactory,
97+
\Magento\Store\Model\StoreManagerInterface $storeManager,
98+
\Magento\Framework\App\Config\ScopeConfigInterface $scopeConfig,
99+
\Magento\Framework\DB\Adapter\AdapterInterface $connection = null
100+
) {
101+
parent::__construct(
102+
$entityFactory,
103+
$logger,
104+
$fetchStrategy,
105+
$eventManager,
106+
$eavConfig,
107+
$resource,
108+
$eavEntityFactory,
109+
$resourceHelper,
110+
$universalFactory,
111+
$storeManager,
112+
$connection
113+
);
114+
$this->scopeConfig = $scopeConfig;
115+
}
116+
61117
/**
62118
* Init collection and determine table names
63119
*
@@ -404,6 +460,23 @@ public function addRootLevelFilter()
404460
return $this;
405461
}
406462

463+
/**
464+
* Add navigation max depth filter
465+
*
466+
* @return $this
467+
*/
468+
public function addNavigationMaxDepthFilter()
469+
{
470+
$navigationMaxDepth = (int)$this->scopeConfig->getValue(
471+
'catalog/navigation/max_depth',
472+
ScopeInterface::SCOPE_STORE
473+
);
474+
if ($navigationMaxDepth > 0) {
475+
$this->addLevelFilter($navigationMaxDepth);
476+
}
477+
return $this;
478+
}
479+
407480
/**
408481
* Add order field
409482
*

app/code/Magento/Catalog/Plugin/Block/Topmenu.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -183,6 +183,7 @@ protected function getCategoryTree($storeId, $rootId)
183183
$collection->addFieldToFilter('path', ['like' => '1/' . $rootId . '/%']); //load only from store root
184184
$collection->addAttributeToFilter('include_in_menu', 1);
185185
$collection->addIsActiveFilter();
186+
$collection->addNavigationMaxDepthFilter();
186187
$collection->addUrlRewriteToResult();
187188
$collection->addOrder('level', Collection::SORT_ORDER_ASC);
188189
$collection->addOrder('position', Collection::SORT_ORDER_ASC);

0 commit comments

Comments
 (0)