diff --git a/app/code/Magento/Catalog/Model/ResourceModel/Category/Collection.php b/app/code/Magento/Catalog/Model/ResourceModel/Category/Collection.php index 6de2cf7788779..46bb74513b59c 100644 --- a/app/code/Magento/Catalog/Model/ResourceModel/Category/Collection.php +++ b/app/code/Magento/Catalog/Model/ResourceModel/Category/Collection.php @@ -6,12 +6,15 @@ namespace Magento\Catalog\Model\ResourceModel\Category; use Magento\CatalogUrlRewrite\Model\CategoryUrlRewriteGenerator; +use Magento\Framework\App\Config\ScopeConfigInterface; +use Magento\Store\Model\ScopeInterface; /** * Category resource collection * * @api * @author Magento Core Team + * @SuppressWarnings(PHPMD.CouplingBetweenObjects) * @since 100.0.2 */ class Collection extends \Magento\Catalog\Model\ResourceModel\Collection\AbstractCollection @@ -58,6 +61,61 @@ class Collection extends \Magento\Catalog\Model\ResourceModel\Collection\Abstrac */ protected $_loadWithProductCount = false; + /** + * Core store config + * + * @var \Magento\Framework\App\Config\ScopeConfigInterface + */ + private $scopeConfig; + + /** + * Constructor + * @param \Magento\Framework\Data\Collection\EntityFactory $entityFactory + * @param \Psr\Log\LoggerInterface $logger + * @param \Magento\Framework\Data\Collection\Db\FetchStrategyInterface $fetchStrategy + * @param \Magento\Framework\Event\ManagerInterface $eventManager + * @param \Magento\Eav\Model\Config $eavConfig + * @param \Magento\Framework\App\ResourceConnection $resource + * @param \Magento\Eav\Model\EntityFactory $eavEntityFactory + * @param \Magento\Eav\Model\ResourceModel\Helper $resourceHelper + * @param \Magento\Framework\Validator\UniversalFactory $universalFactory + * @param \Magento\Store\Model\StoreManagerInterface $storeManager + * @param \Magento\Framework\DB\Adapter\AdapterInterface $connection + * @param \Magento\Framework\App\Config\ScopeConfigInterface $scopeConfig + * + * @SuppressWarnings(PHPMD.ExcessiveParameterList) + */ + public function __construct( + \Magento\Framework\Data\Collection\EntityFactory $entityFactory, + \Psr\Log\LoggerInterface $logger, + \Magento\Framework\Data\Collection\Db\FetchStrategyInterface $fetchStrategy, + \Magento\Framework\Event\ManagerInterface $eventManager, + \Magento\Eav\Model\Config $eavConfig, + \Magento\Framework\App\ResourceConnection $resource, + \Magento\Eav\Model\EntityFactory $eavEntityFactory, + \Magento\Eav\Model\ResourceModel\Helper $resourceHelper, + \Magento\Framework\Validator\UniversalFactory $universalFactory, + \Magento\Store\Model\StoreManagerInterface $storeManager, + \Magento\Framework\DB\Adapter\AdapterInterface $connection = null, + \Magento\Framework\App\Config\ScopeConfigInterface $scopeConfig = null + ) { + parent::__construct( + $entityFactory, + $logger, + $fetchStrategy, + $eventManager, + $eavConfig, + $resource, + $eavEntityFactory, + $resourceHelper, + $universalFactory, + $storeManager, + $connection + ); + $this->scopeConfig = $scopeConfig ?: + \Magento\Framework\App\ObjectManager::getInstance()->get(ScopeConfigInterface::class); + } + /** * Init collection and determine table names * @@ -404,6 +462,23 @@ public function addRootLevelFilter() return $this; } + /** + * Add navigation max depth filter + * + * @return $this + */ + public function addNavigationMaxDepthFilter() + { + $navigationMaxDepth = (int)$this->scopeConfig->getValue( + 'catalog/navigation/max_depth', + ScopeInterface::SCOPE_STORE + ); + if ($navigationMaxDepth > 0) { + $this->addLevelFilter($navigationMaxDepth); + } + return $this; + } + /** * Add order field * diff --git a/app/code/Magento/Catalog/Plugin/Block/Topmenu.php b/app/code/Magento/Catalog/Plugin/Block/Topmenu.php index debf7fd002b2d..44f9193ab4012 100644 --- a/app/code/Magento/Catalog/Plugin/Block/Topmenu.php +++ b/app/code/Magento/Catalog/Plugin/Block/Topmenu.php @@ -184,6 +184,7 @@ protected function getCategoryTree($storeId, $rootId) $collection->addFieldToFilter('path', ['like' => '1/' . $rootId . '/%']); //load only from store root $collection->addAttributeToFilter('include_in_menu', 1); $collection->addIsActiveFilter(); + $collection->addNavigationMaxDepthFilter(); $collection->addUrlRewriteToResult(); $collection->addOrder('level', Collection::SORT_ORDER_ASC); $collection->addOrder('position', Collection::SORT_ORDER_ASC);