Skip to content

MAGETWO-61422 Respect Category Top Navigation Max Depth setting #12640

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -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 <[email protected]>
* @SuppressWarnings(PHPMD.CouplingBetweenObjects)
* @since 100.0.2
*/
class Collection extends \Magento\Catalog\Model\ResourceModel\Collection\AbstractCollection
Expand Down Expand Up @@ -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
*
Expand Down Expand Up @@ -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);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

In my test, this looks like it doesn't work as intended. If I set a maximum depth of 1, no category is shown. If I set the value to 2, the top level category is displayed. If we add 1 to $navigationMaxDepth like $this->addLevelFilter($navigationMaxDepth + 1), it works like I would expect it to. I guess that's because the root category level also has to be considered.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

No, that seems not like a good solution to me. I think we should adhere to the levels on http://docs.magento.com/m2/ee/user_guide/catalog/navigation-top.html

So in the admin configuration you have to use 2 for the first level.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You are absolutely right. Thank you for pointing this out. The PR works as intended.

}
return $this;
}

/**
* Add order field
*
Expand Down
1 change: 1 addition & 0 deletions app/code/Magento/Catalog/Plugin/Block/Topmenu.php
Original file line number Diff line number Diff line change
Expand Up @@ -183,6 +183,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);
Expand Down