init
This commit is contained in:
@@ -0,0 +1,116 @@
|
||||
<?php
|
||||
/**
|
||||
* Icon category mega menu. Kept separate from the existing product mega menu.
|
||||
*
|
||||
* @var \Magento\Framework\View\Element\Template $block
|
||||
* @var \Magento\Framework\Escaper $escaper
|
||||
*/
|
||||
$rootCategoryId = (int)$block->getData('root_category_id');
|
||||
$label = (string)$block->getData('menu_label');
|
||||
$class = (string)$block->getData('menu_class');
|
||||
$menuUrl = (string)$block->getData('menu_url');
|
||||
$description = trim((string)$block->getData('menu_description'));
|
||||
$menuItems = (array)$block->getData('menu_items');
|
||||
$hasImages = false;
|
||||
$hasDescription = $description !== '';
|
||||
|
||||
foreach ($menuItems as $menuItem) {
|
||||
if (!empty($menuItem['icon_url'])) {
|
||||
$hasImages = true;
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
$layoutClass = $hasImages
|
||||
? 'esun-icon-megamenu--visual'
|
||||
: 'esun-icon-megamenu--text-only';
|
||||
$parentClass = $hasImages ? 'parent ' : '';
|
||||
$linkedMenuClass = !$hasImages ? 'esun-linked-megamenu-trigger' : '';
|
||||
$descriptionClass = $hasDescription ? 'has-description' : 'no-description';
|
||||
$textDividerIndex = !$hasImages && count($menuItems) > 1
|
||||
? (int)ceil(count($menuItems) / 2)
|
||||
: -1;
|
||||
?>
|
||||
<li class="<?= $escaper->escapeHtmlAttr(trim('level0 level-top ' . $parentClass . 'category-item esun-megamenu-trigger esun-icon-megamenu-trigger ' . $linkedMenuClass . ' ' . $class)) ?>"
|
||||
data-category="<?= $rootCategoryId ?>">
|
||||
<a href="<?= !$hasImages ? $escaper->escapeUrl($menuUrl) : 'javascript:void(0)' ?>"
|
||||
class="level-top"
|
||||
aria-haspopup="true">
|
||||
<span><?= $escaper->escapeHtml($label) ?></span>
|
||||
</a>
|
||||
<?php if (!$hasImages): ?>
|
||||
<button class="esun-megamenu-toggle"
|
||||
type="button"
|
||||
aria-label="<?= $escaper->escapeHtmlAttr(__('Toggle %1 submenu', $label)) ?>"
|
||||
aria-expanded="false">
|
||||
<span aria-hidden="true"></span>
|
||||
</button>
|
||||
<?php endif; ?>
|
||||
|
||||
<div class="esun-megamenu-wrapper esun-icon-megamenu-wrapper <?= $escaper->escapeHtmlAttr($layoutClass . ' ' . $descriptionClass) ?>">
|
||||
<div class="esun-icon-megamenu-inner">
|
||||
<?php if ($hasImages && $hasDescription): ?>
|
||||
<aside class="esun-icon-megamenu-intro">
|
||||
<div class="esun-icon-megamenu-intro-content">
|
||||
<?= /* @noEscape */ $description ?>
|
||||
</div>
|
||||
</aside>
|
||||
<?php endif; ?>
|
||||
|
||||
<div class="esun-icon-megamenu-items">
|
||||
<?php foreach ($menuItems as $itemIndex => $item): ?>
|
||||
<?php $hasItemImage = !empty($item['icon_url']); ?>
|
||||
<?php $hasChildren = !empty($item['children']); ?>
|
||||
<?php $dividerClass = (int)$itemIndex === $textDividerIndex ? 'is-second-half-start' : ''; ?>
|
||||
<section class="esun-icon-megamenu-column <?= $hasItemImage ? 'has-image' : 'no-image' ?> <?= $dividerClass ?>">
|
||||
<?php if ($hasItemImage): ?>
|
||||
<?php if (!$hasChildren): ?>
|
||||
<a class="esun-icon-megamenu-image-wrap"
|
||||
href="<?= $escaper->escapeUrl((string)$item['url']) ?>">
|
||||
<?php else: ?>
|
||||
<div class="esun-icon-megamenu-image-wrap">
|
||||
<?php endif; ?>
|
||||
<img class="esun-icon-megamenu-image"
|
||||
src="<?= $escaper->escapeUrl((string)$item['icon_url']) ?>"
|
||||
alt="<?= $escaper->escapeHtmlAttr((string)$item['name']) ?>"
|
||||
loading="lazy"/>
|
||||
<span class="esun-icon-megamenu-image-title">
|
||||
<?= $escaper->escapeHtml((string)$item['name']) ?>
|
||||
</span>
|
||||
<?php if (!$hasChildren): ?>
|
||||
</a>
|
||||
<?php else: ?>
|
||||
</div>
|
||||
<?php endif; ?>
|
||||
<?php endif; ?>
|
||||
<div class="esun-icon-megamenu-content">
|
||||
<?php if (!$hasItemImage): ?>
|
||||
<?php if (!$hasChildren): ?>
|
||||
<a class="esun-icon-megamenu-title"
|
||||
href="<?= $escaper->escapeUrl((string)$item['url']) ?>">
|
||||
<?= $escaper->escapeHtml((string)$item['name']) ?>
|
||||
</a>
|
||||
<?php else: ?>
|
||||
<span class="esun-icon-megamenu-title">
|
||||
<?= $escaper->escapeHtml((string)$item['name']) ?>
|
||||
</span>
|
||||
<?php endif; ?>
|
||||
<?php endif; ?>
|
||||
<?php if ($hasChildren): ?>
|
||||
<ul class="esun-icon-megamenu-links">
|
||||
<?php foreach ((array)$item['children'] as $child): ?>
|
||||
<li>
|
||||
<a href="<?= $escaper->escapeUrl((string)$child['url']) ?>">
|
||||
<?= $escaper->escapeHtml((string)$child['name']) ?>
|
||||
</a>
|
||||
</li>
|
||||
<?php endforeach; ?>
|
||||
</ul>
|
||||
<?php endif; ?>
|
||||
</div>
|
||||
</section>
|
||||
<?php endforeach; ?>
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
</li>
|
||||
@@ -0,0 +1,239 @@
|
||||
<?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')) ?> >
|
||||
</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; ?>
|
||||
Reference in New Issue
Block a user