Skip to content

Commit

Permalink
Merge branch '2.4-develop' of https://github.com/mage-os/mirror-magento2
Browse files Browse the repository at this point in the history
 into 2.4-develop
  • Loading branch information
mage-os-ci committed Feb 1, 2024
2 parents fb84973 + 9846306 commit 608b225
Show file tree
Hide file tree
Showing 11 changed files with 1,220 additions and 114 deletions.
58 changes: 5 additions & 53 deletions app/code/Magento/Catalog/Plugin/Block/Topmenu.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,13 +12,12 @@
use Magento\Framework\Data\Tree\Node;

/**
* Plugin for top menu block
* Plugin that enhances the top menu block by building and managing the category tree
* for menu rendering in a storefront.
*/
class Topmenu
{
/**
* Catalog category
*
* @var \Magento\Catalog\Helper\Category
*/
protected $catalogCategory;
Expand All @@ -33,29 +32,21 @@ class Topmenu
*/
private $storeManager;

/**
* @var \Magento\Catalog\Model\Layer\Resolver
*/
private $layerResolver;

/**
* Initialize dependencies.
*
* @param \Magento\Catalog\Helper\Category $catalogCategory
* @param \Magento\Catalog\Model\ResourceModel\Category\StateDependentCollectionFactory $categoryCollectionFactory
* @param \Magento\Store\Model\StoreManagerInterface $storeManager
* @param \Magento\Catalog\Model\Layer\Resolver $layerResolver
*/
public function __construct(
\Magento\Catalog\Helper\Category $catalogCategory,
\Magento\Catalog\Model\ResourceModel\Category\StateDependentCollectionFactory $categoryCollectionFactory,
\Magento\Store\Model\StoreManagerInterface $storeManager,
\Magento\Catalog\Model\Layer\Resolver $layerResolver
\Magento\Store\Model\StoreManagerInterface $storeManager
) {
$this->catalogCategory = $catalogCategory;
$this->collectionFactory = $categoryCollectionFactory;
$this->storeManager = $storeManager;
$this->layerResolver = $layerResolver;
}

/**
Expand All @@ -78,7 +69,6 @@ public function beforeGetHtml(
$storeId = $this->storeManager->getStore()->getId();
/** @var \Magento\Catalog\Model\ResourceModel\Category\Collection $collection */
$collection = $this->getCategoryTree($storeId, $rootId);
$currentCategory = $this->getCurrentCategory();
$mapping = [$rootId => $subject->getMenu()]; // use nodes stack to avoid recursion
foreach ($collection as $category) {
$categoryParentId = $category->getParentId();
Expand All @@ -97,7 +87,6 @@ public function beforeGetHtml(
$categoryNode = new Node(
$this->getCategoryAsArray(
$category,
$currentCategory,
$category->getParentId() == $categoryParentId
),
'id',
Expand Down Expand Up @@ -132,39 +121,20 @@ public function beforeGetIdentities(\Magento\Theme\Block\Html\Topmenu $subject)
}
}

/**
* Get current Category from catalog layer
*
* @return \Magento\Catalog\Model\Category
*/
private function getCurrentCategory()
{
$catalogLayer = $this->layerResolver->get();

if (!$catalogLayer) {
return null;
}

return $catalogLayer->getCurrentCategory();
}

/**
* Convert category to array
*
* @param \Magento\Catalog\Model\Category $category
* @param \Magento\Catalog\Model\Category $currentCategory
* @param Category $category
* @param bool $isParentActive
* @return array
*/
private function getCategoryAsArray($category, $currentCategory, $isParentActive)
private function getCategoryAsArray($category, $isParentActive): array
{
$categoryId = $category->getId();
return [
'name' => $category->getName(),
'id' => 'category-node-' . $categoryId,
'url' => $this->catalogCategory->getCategoryUrl($category),
'has_active' => in_array((string)$categoryId, explode('/', (string)$currentCategory->getPath()), true),
'is_active' => $categoryId == $currentCategory->getId(),
'is_category' => true,
'is_parent_active' => $isParentActive
];
Expand Down Expand Up @@ -196,22 +166,4 @@ protected function getCategoryTree($storeId, $rootId)

return $collection;
}

/**
* Add active
*
* @param \Magento\Theme\Block\Html\Topmenu $subject
* @param string[] $result
* @return string[]
* @SuppressWarnings(PHPMD.UnusedFormalParameter)
*/
public function afterGetCacheKeyInfo(\Magento\Theme\Block\Html\Topmenu $subject, array $result)
{
$activeCategory = $this->getCurrentCategory();
if ($activeCategory) {
$result[] = Category::CACHE_TAG . '_' . $activeCategory->getId();
}

return $result;
}
}
23 changes: 23 additions & 0 deletions app/code/Magento/Catalog/Test/Mftf/Data/SeoConfigData.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
<?xml version="1.0" encoding="UTF-8"?>
<!--
/**
* Copyright © Magento, Inc. All rights reserved.
* See COPYING.txt for license details.
*/
-->

<entities xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:noNamespaceSchemaLocation="urn:magento:mftf:DataGenerator/etc/dataProfileSchema.xsd">
<entity name="EnableCategoriesPathForProductUrls">
<data key="path">catalog/seo/product_use_categories</data>
<data key="scope_id">0</data>
<data key="label">Yes</data>
<data key="value">1</data>
</entity>
<entity name="DisableCategoriesPathForProductUrls">
<data key="path">catalog/seo/product_use_categories</data>
<data key="scope_id">0</data>
<data key="label">No</data>
<data key="value">0</data>
</entity>
</entities>
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,9 @@
<element name="collapseAll" type="button" selector=".tree-actions a:first-child"/>
<element name="expandAll" type="button" selector=".tree-actions a:last-child"/>
<element name="categoryHighlighted" type="text" selector="//div[@id='store.menu']//span[contains(text(),'{{name}}')]/ancestor::li" parameterized="true" timeout="30"/>
<element name="dynamicActiveClassSelector" type="text" selector="//div[@id='store.menu']//span[contains(text(),'{{name}}')]/ancestor::li[contains(@class, '{{containsClass}}') and not(contains(@class, '{{notContainsClass}}'))]" parameterized="true" timeout="30"/>
<element name="categoryNotHighlighted" type="text" selector="[id=\'store.menu\'] ul li.active" timeout="30"/>
<element name="parentCategoryNotHighlighted" type="text" selector="[id=\'store.menu\'] ul li.has-active" timeout="30"/>
<element name="categoryTreeRoot" type="text" selector="li.x-tree-node:first-of-type > div.x-tree-node-el:first-of-type" timeout="30"/>
<element name="categoryInTree" type="text" selector="//a[contains(text(), '{{name}}')]" parameterized="true" timeout="30"/>
<element name="categoryInFrontendTree" type="text" selector="//a/span[contains(text(), '{{name}}')]" parameterized="true" timeout="30"/>
Expand Down
Loading

0 comments on commit 608b225

Please sign in to comment.