Files
2026-09-15 10:35:56 +08:00

240 lines
10 KiB
PHTML
Executable File

<?php
/**
* Magento top menu with an Esun classification mega menu attached to one level-0 node.
*
* @var \Magento\Theme\Block\Html\Topmenu $block
* @var \Magento\Framework\Escaper $escaper
* @var \Esun\MegaMenu\ViewModel\MegaMenuData|null $viewModel
*/
$viewModel = $block->getData('view_model');
if (!$viewModel || !$viewModel->isEnabled()) {
$columnsLimit = $block->getColumnsLimit() ?: 0;
$_menuHtml = $block->getHtml('level-top', 'submenu', $columnsLimit);
?>
<nav class="navigation" data-action="navigation">
<ul data-mage-init='{"menu":{"responsive":true, "expanded":true, "position":{"my":"left top","at":"left bottom"}}}'>
<?= /* @noEscape */ $_menuHtml ?>
<?= $block->getChildHtml() ?>
</ul>
</nav>
<?php
return;
}
$columnsLimit = $block->getColumnsLimit() ?: 0;
$block->getHtml('level-top', 'submenu', $columnsLimit);
$rootCategoryIds = $viewModel->getRootCategoryIds();
$legacyRootCategoryId = $viewModel->getRootCategoryId();
$targetTitle = strtoupper(trim($viewModel->getTitle()));
$baseScopeName = 'esunMegaMenu_' . preg_replace('/[^A-Za-z0-9_]/', '_', (string)$block->getNameInLayout());
$renderedRootCategoryIds = [];
$componentConfigs = [];
$getNodeCategoryId = function ($node): int {
if (preg_match('/^category-node-(\d+)$/', (string)$node->getId(), $matches)) {
return (int)$matches[1];
}
return 0;
};
$renderDefaultNode = function ($node) use (
$escaper,
$getNodeCategoryId,
$viewModel
): string {
$categoryId = $getNodeCategoryId($node);
$nodeUrl = $categoryId
? $viewModel->getTopLevelCategoryUrl($categoryId, (string)$node->getUrl())
: (string)$node->getUrl();
$classes = array_filter([
'level0',
(string)$node->getPositionClass(),
(string)$node->getClass(),
'level-top',
$node->getIsActive() ? 'active' : ''
]);
$html = '<li class="' . $escaper->escapeHtmlAttr(implode(' ', $classes)) . '">';
$html .= '<a href="' . $escaper->escapeUrl($nodeUrl) . '" class="level-top">';
$html .= '<span>' . $escaper->escapeHtml($node->getName()) . '</span></a>';
$html .= '</li>';
return $html;
};
$getMegaNodeRootCategoryId = function ($node) use (
$viewModel,
$getNodeCategoryId,
$legacyRootCategoryId,
$targetTitle
): int {
$categoryId = $getNodeCategoryId($node);
if ($categoryId && $viewModel->isMegaMenuRootCategory($categoryId)) {
return $categoryId;
}
if ($legacyRootCategoryId && strtoupper(trim((string)$node->getName())) === $targetTitle) {
return $legacyRootCategoryId;
}
return 0;
};
$renderMegaNode = function (int $rootCategoryId, string $label, string $class = '') use (
$block,
$escaper,
$viewModel,
$baseScopeName,
&$componentConfigs
): string {
if ($viewModel->isIconCategoryMenu($rootCategoryId)) {
return $block->getLayout()
->createBlock(\Magento\Framework\View\Element\Template::class)
->setTemplate('Esun_MegaMenu::html/icon-category-menu.phtml')
->setData('root_category_id', $rootCategoryId)
->setData('menu_label', $label)
->setData('menu_class', $class)
->setData('menu_url', $viewModel->getRootCategoryUrl($rootCategoryId))
->setData('menu_description', $viewModel->getRootCategoryDescription($rootCategoryId))
->setData('menu_items', $viewModel->getIconCategoryMenuData($rootCategoryId))
->toHtml();
}
$scopeName = $baseScopeName . '_' . $rootCategoryId;
$featuredProduct = $viewModel->getFeaturedProductData($rootCategoryId);
$componentConfigs[$scopeName] = [
'component' => 'Esun_MegaMenu/js/mega-menu',
'config' => [
'rawMenuData' => $viewModel->getMegaMenuData($rootCategoryId)
]
];
ob_start();
?>
<li class="<?= $escaper->escapeHtmlAttr(trim('level0 level-top parent category-item esun-megamenu-trigger ' . $class)) ?>" data-category="<?= $rootCategoryId ?>"
data-bind="scope: '<?= $escaper->escapeHtmlAttr($scopeName) ?>'">
<a href="javascript:void(0)" class="level-top" aria-haspopup="true">
<span><?= $escaper->escapeHtml($label) ?></span>
</a>
<div class="esun-megamenu-wrapper">
<div class="esun-megamenu-inner">
<div class="esun-megamenu-sidebar">
<div class="esun-filament-categories" data-bind="foreach: menuData">
<div class="esun-filament-category" data-bind="
css: { active: $parent.currentSecondaryId() === id },
event: { click: $parent.switchSecondaryCategory.bind($parent), mouseenter: $parent.switchSecondaryCategory.bind($parent), focusin: $parent.switchSecondaryCategory.bind($parent) }
">
<span class="esun-filament-category-label">
<span data-bind="text: name"></span>
<span class="esun-megamenu-arrow" aria-hidden="true"></span>
</span>
</div>
</div>
<?php if ($featuredProduct): ?>
<div class="esun-featured-product-block">
<div class="esun-featured-title"><?= $escaper->escapeHtml(__('FEATURED PRODUCT')) ?></div>
<?php $backgroundUrl = (string)$featuredProduct['category_background_url']; ?>
<div class="esun-megamenu-featured-card"
<?php if ($backgroundUrl): ?>
style="background-image: linear-gradient(90deg, rgba(20, 33, 44, 0.48), rgba(20, 33, 44, 0.06)), url('<?= $escaper->escapeUrl($backgroundUrl) ?>');"
<?php endif; ?>>
<span class="esun-megamenu-featured-kicker">
<?= $escaper->escapeHtml($featuredProduct['label']) ?>
</span>
<span class="esun-megamenu-featured-name">
<?= $escaper->escapeHtml($featuredProduct['name']) ?>
</span>
<a class="esun-megamenu-featured-link"
href="<?= $escaper->escapeUrl($featuredProduct['url']) ?>">
<?= $escaper->escapeHtml(__('Explore Product')) ?>
</a>
</div>
</div>
<?php endif; ?>
</div>
<div class="esun-megamenu-content" data-bind="with: activeSecondaryCategory">
<!-- ko if: seriesCategories && seriesCategories.length -->
<div class="esun-series-container" data-bind="foreach: seriesCategories">
<section class="esun-series-group">
<header class="esun-series-header">
<h4 data-bind="text: name"></h4>
<a data-bind="attr: { href: url }" class="esun-view-all">
<?= $escaper->escapeHtml(__('View All')) ?> &gt;
</a>
</header>
<!-- ko if: products && products.length -->
<div class="esun-products-grid" data-bind="foreach: products">
<article class="esun-product-card">
<a data-bind="attr: { href: url, title: name }" class="esun-product-link">
<span class="esun-product-image-frame">
<img data-bind="attr: { src: image_url, alt: name }" class="esun-product-image" loading="lazy"/>
</span>
<span class="esun-product-name" data-bind="text: name"></span>
<span class="esun-product-price" data-bind="text: price"></span>
</a>
</article>
</div>
<!-- /ko -->
</section>
</div>
<!-- /ko -->
</div>
</div>
</div>
</li>
<?php
return ob_get_clean();
};
?>
<nav class="navigation esun-main-navigation"
data-action="navigation"
data-mage-init='{"Esun_MegaMenu/js/mobile-menu":{}}'>
<ul data-mage-init='{"menu":{"responsive":true, "expanded":true, "position":{"my":"left top","at":"left bottom"}}}'>
<?php foreach ($block->getMenu()->getChildren() as $node): ?>
<?php $megaRootCategoryId = $getMegaNodeRootCategoryId($node); ?>
<?php if ($megaRootCategoryId): ?>
<?php $renderedRootCategoryIds[$megaRootCategoryId] = $megaRootCategoryId; ?>
<?= /* @noEscape */ $renderMegaNode(
$megaRootCategoryId,
(string)$node->getName(),
trim((string)$node->getPositionClass() . ' ' . (string)$node->getClass())
) ?>
<?php else: ?>
<?= /* @noEscape */ $renderDefaultNode($node) ?>
<?php endif; ?>
<?php endforeach; ?>
<?php foreach ($rootCategoryIds as $rootCategoryId): ?>
<?php if (isset($renderedRootCategoryIds[$rootCategoryId])): ?>
<?php continue; ?>
<?php endif; ?>
<?= /* @noEscape */ $renderMegaNode(
(int)$rootCategoryId,
$rootCategoryId === $legacyRootCategoryId ? $viewModel->getTitle() : $viewModel->getRootCategoryName((int)$rootCategoryId)
) ?>
<?php endforeach; ?>
<?= $block->getChildHtml() ?>
</ul>
</nav>
<?php if ($componentConfigs): ?>
<script type="text/x-magento-init">
{
"*": {
"Magento_Ui/js/core/app": {
"components": <?= /* @noEscape */ json_encode($componentConfigs, JSON_HEX_TAG | JSON_HEX_APOS | JSON_HEX_QUOT | JSON_HEX_AMP) ?>
}
}
}
</script>
<?php endif; ?>