Skip to content

Commit f6af757

Browse files
MAGETWO-85519: MAGETWO-61422 Respect Category Top Navigation Max Depth setting #12640
2 parents 9c9f2f3 + 22ea8d8 commit f6af757

File tree

2 files changed

+76
-0
lines changed

2 files changed

+76
-0
lines changed

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

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

88
use Magento\CatalogUrlRewrite\Model\CategoryUrlRewriteGenerator;
9+
use Magento\Framework\App\Config\ScopeConfigInterface;
10+
use Magento\Store\Model\ScopeInterface;
911

1012
/**
1113
* Category resource collection
1214
*
1315
* @api
1416
* @author Magento Core Team <[email protected]>
17+
* @SuppressWarnings(PHPMD.CouplingBetweenObjects)
1518
* @since 100.0.2
1619
*/
1720
class Collection extends \Magento\Catalog\Model\ResourceModel\Collection\AbstractCollection
@@ -58,6 +61,61 @@ class Collection extends \Magento\Catalog\Model\ResourceModel\Collection\Abstrac
5861
*/
5962
protected $_loadWithProductCount = false;
6063

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

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

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)