![]() Server : Apache System : Linux server2.corals.io 4.18.0-348.2.1.el8_5.x86_64 #1 SMP Mon Nov 15 09:17:08 EST 2021 x86_64 User : corals ( 1002) PHP Version : 7.4.33 Disable Function : exec,passthru,shell_exec,system Directory : /home/corals/old/app/code/Cnc/Catalog/Plugin/Block/Html/ |
<?php /** * Copyright (c) 2019 Kaliop Digital Commerce (https://digitalcommerce.kaliop.com) All Rights Reserved. * https://opensource.org/licenses/OSL-3.0 Open Software License (OSL 3.0) * cnc_catalog_m2 * <[email protected]> */ declare(strict_types=1); namespace Cnc\Catalog\Plugin\Block\Html; use Cnc\Catalog\Model\Config as CatalogConfig; use Cnc\Theme\Block\Html\Topmenu as OriginalTopMenu; use Magento\Catalog\Helper\Category as CategoryHelper; use Magento\Catalog\Model\Category; use Magento\Catalog\Model\Layer\Resolver; use Magento\Catalog\Model\ResourceModel\Category\Collection as CategoryCollection; use Magento\Catalog\Model\ResourceModel\Category\StateDependentCollectionFactory; use Magento\Framework\Data\Collection; use Magento\Framework\Data\Tree\Node; use Magento\Framework\Exception\LocalizedException; use Magento\Framework\Exception\NoSuchEntityException; use Magento\Store\Model\StoreManagerInterface; class Topmenu { /** * @var CategoryHelper */ protected $catalogCategory; /** * @var StateDependentCollectionFactory */ private $collectionFactory; /** * @var StoreManagerInterface */ private $storeManager; /** * @var Resolver */ private $layerResolver; /** * Topmenu constructor. * * @param CategoryHelper $catalogCategory * @param StateDependentCollectionFactory $categoryCollectionFactory * @param StoreManagerInterface $storeManager * @param Resolver $layerResolver */ public function __construct( CategoryHelper $catalogCategory, StateDependentCollectionFactory $categoryCollectionFactory, StoreManagerInterface $storeManager, Resolver $layerResolver ) { $this->catalogCategory = $catalogCategory; $this->collectionFactory = $categoryCollectionFactory; $this->storeManager = $storeManager; $this->layerResolver = $layerResolver; } /** * KDC: Native method, not modified, simple copy/paste from original plugin * Build category tree for menu block. * * @param OriginalTopMenu $subject * @param string $outermostClass * @param string $childrenWrapClass * @param int $limit * * @throws LocalizedException * @throws NoSuchEntityException */ public function beforeGetHtml( OriginalTopMenu $subject, $outermostClass = '', $childrenWrapClass = '', $limit = 0 ) { $rootId = $this->storeManager->getStore()->getRootCategoryId(); $storeId = $this->storeManager->getStore()->getId(); /** @var CategoryCollection $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(); if (!isset($mapping[$categoryParentId])) { $parentIds = $category->getParentIds(); foreach ($parentIds as $parentId) { if (isset($mapping[$parentId])) { $categoryParentId = $parentId; } } } /** @var Node $parentCategoryNode */ $parentCategoryNode = $mapping[$categoryParentId]; $categoryNode = new Node( $this->getCategoryAsArray( $category, $currentCategory, $category->getParentId() == $categoryParentId ), 'id', $parentCategoryNode->getTree(), $parentCategoryNode ); $parentCategoryNode->addChild($categoryNode); $mapping[$category->getId()] = $categoryNode; //add node in stack } } /** * Get Category Tree * * @param int $storeId * @param int $rootId * * @return CategoryCollection * @throws LocalizedException */ protected function getCategoryTree($storeId, $rootId) { /** @var CategoryCollection $collection */ $collection = $this->collectionFactory->create(); $collection->setStoreId($storeId); $collection->addAttributeToSelect('name'); $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); $collection->addOrder('parent_id', Collection::SORT_ORDER_ASC); $collection->addOrder('entity_id', Collection::SORT_ORDER_ASC); // Category icon $collection->addAttributeToSelect(CatalogConfig::CATEGORY_ATTRIBUTE_CODE_ICON_CSS_CLASS); // Menu push 1 - Custom attributes $collection->addAttributeToSelect(CatalogConfig::CATEGORY_ATTRIBUTE_CODE_MENU_PUSH_TITLE_1); $collection->addAttributeToSelect(CatalogConfig::CATEGORY_ATTRIBUTE_CODE_MENU_PUSH_TEXT_1); $collection->addAttributeToSelect(CatalogConfig::CATEGORY_ATTRIBUTE_CODE_MENU_PUSH_CTA_TEXT_1); $collection->addAttributeToSelect(CatalogConfig::CATEGORY_ATTRIBUTE_CODE_MENU_PUSH_CTA_LINK_1); $collection->addAttributeToSelect(CatalogConfig::CATEGORY_ATTRIBUTE_CODE_MENU_PUSH_IMAGE_1); // Menu push 2 - Custom attributes $collection->addAttributeToSelect(CatalogConfig::CATEGORY_ATTRIBUTE_CODE_MENU_PUSH_TITLE_2); $collection->addAttributeToSelect(CatalogConfig::CATEGORY_ATTRIBUTE_CODE_MENU_PUSH_TEXT_2); $collection->addAttributeToSelect(CatalogConfig::CATEGORY_ATTRIBUTE_CODE_MENU_PUSH_CTA_TEXT_2); $collection->addAttributeToSelect(CatalogConfig::CATEGORY_ATTRIBUTE_CODE_MENU_PUSH_CTA_LINK_2); $collection->addAttributeToSelect(CatalogConfig::CATEGORY_ATTRIBUTE_CODE_MENU_PUSH_IMAGE_2); return $collection; } /** * KDC: Native method, not modified, simple copy/paste from original plugin * Get current Category from catalog layer * @return Category */ private function getCurrentCategory() { $catalogLayer = $this->layerResolver->get(); if (!$catalogLayer) { return null; } return $catalogLayer->getCurrentCategory(); } /** * Convert category to array * * @param Category $category * @param Category $currentCategory * @param bool $isParentActive * * @return array */ private function getCategoryAsArray($category, $currentCategory, $isParentActive) { // Get all data in one go, in order to not do getData per attributes // And so, avoid to fire some not needed plugins here (afterGetData/beforeGetData) // on each getData attribute calls $categoryData = $category->getData(); return [ 'name' => $category->getName(), 'id' => 'category-node-' . $category->getId(), 'url' => $this->catalogCategory->getCategoryUrl($category), 'has_active' => in_array( (string)$category->getId(), explode('/', $currentCategory->getPath()), true ), 'is_active' => $category->getId() == $currentCategory->getId(), 'is_category' => true, 'is_parent_active' => $isParentActive, //@codingStandardsIgnoreStart CatalogConfig::CATEGORY_ATTRIBUTE_CODE_ICON_CSS_CLASS => isset($categoryData[CatalogConfig::CATEGORY_ATTRIBUTE_CODE_ICON_CSS_CLASS]) ? $categoryData[CatalogConfig::CATEGORY_ATTRIBUTE_CODE_ICON_CSS_CLASS] : '', CatalogConfig::CATEGORY_ATTRIBUTE_CODE_MENU_PUSH_TITLE_1 => isset($categoryData[CatalogConfig::CATEGORY_ATTRIBUTE_CODE_MENU_PUSH_TITLE_1]) ? $categoryData[CatalogConfig::CATEGORY_ATTRIBUTE_CODE_MENU_PUSH_TITLE_1] : '', CatalogConfig::CATEGORY_ATTRIBUTE_CODE_MENU_PUSH_TEXT_1 => isset($categoryData[CatalogConfig::CATEGORY_ATTRIBUTE_CODE_MENU_PUSH_TEXT_1]) ? $categoryData[CatalogConfig::CATEGORY_ATTRIBUTE_CODE_MENU_PUSH_TEXT_1] : '', CatalogConfig::CATEGORY_ATTRIBUTE_CODE_MENU_PUSH_CTA_TEXT_1 => isset($categoryData[CatalogConfig::CATEGORY_ATTRIBUTE_CODE_MENU_PUSH_CTA_TEXT_1]) ? $categoryData[CatalogConfig::CATEGORY_ATTRIBUTE_CODE_MENU_PUSH_CTA_TEXT_1] : '', CatalogConfig::CATEGORY_ATTRIBUTE_CODE_MENU_PUSH_CTA_LINK_1 => isset($categoryData[CatalogConfig::CATEGORY_ATTRIBUTE_CODE_MENU_PUSH_CTA_LINK_1]) ? $categoryData[CatalogConfig::CATEGORY_ATTRIBUTE_CODE_MENU_PUSH_CTA_LINK_1] : '', CatalogConfig::CATEGORY_ATTRIBUTE_CODE_MENU_PUSH_IMAGE_1 => isset($categoryData[CatalogConfig::CATEGORY_ATTRIBUTE_CODE_MENU_PUSH_IMAGE_1]) ? $categoryData[CatalogConfig::CATEGORY_ATTRIBUTE_CODE_MENU_PUSH_IMAGE_1] : '', CatalogConfig::CATEGORY_ATTRIBUTE_CODE_MENU_PUSH_TITLE_2 => isset($categoryData[CatalogConfig::CATEGORY_ATTRIBUTE_CODE_MENU_PUSH_TITLE_2]) ? $categoryData[CatalogConfig::CATEGORY_ATTRIBUTE_CODE_MENU_PUSH_TITLE_2] : '', CatalogConfig::CATEGORY_ATTRIBUTE_CODE_MENU_PUSH_TEXT_2 => isset($categoryData[CatalogConfig::CATEGORY_ATTRIBUTE_CODE_MENU_PUSH_TEXT_2]) ? $categoryData[CatalogConfig::CATEGORY_ATTRIBUTE_CODE_MENU_PUSH_TEXT_2] : '', CatalogConfig::CATEGORY_ATTRIBUTE_CODE_MENU_PUSH_CTA_TEXT_2 => isset($categoryData[CatalogConfig::CATEGORY_ATTRIBUTE_CODE_MENU_PUSH_CTA_TEXT_2]) ? $categoryData[CatalogConfig::CATEGORY_ATTRIBUTE_CODE_MENU_PUSH_CTA_TEXT_2] : '', CatalogConfig::CATEGORY_ATTRIBUTE_CODE_MENU_PUSH_CTA_LINK_2 => isset($categoryData[CatalogConfig::CATEGORY_ATTRIBUTE_CODE_MENU_PUSH_CTA_LINK_2]) ? $categoryData[CatalogConfig::CATEGORY_ATTRIBUTE_CODE_MENU_PUSH_CTA_LINK_2] : '', CatalogConfig::CATEGORY_ATTRIBUTE_CODE_MENU_PUSH_IMAGE_2 => isset($categoryData[CatalogConfig::CATEGORY_ATTRIBUTE_CODE_MENU_PUSH_IMAGE_2]) ? $categoryData[CatalogConfig::CATEGORY_ATTRIBUTE_CODE_MENU_PUSH_IMAGE_2] : '', //@codingStandardsIgnoreEnd ]; } /** * KDC: Native method, not modified, simple copy/paste from original plugin * Add list of associated identities to the top menu block for caching purposes. * * @param OriginalTopMenu $subject * * @throws LocalizedException * @throws NoSuchEntityException */ public function beforeGetIdentities(OriginalTopMenu $subject) { $subject->addIdentity(Category::CACHE_TAG); $rootId = $this->storeManager->getStore()->getRootCategoryId(); $storeId = $this->storeManager->getStore()->getId(); /** @var CategoryCollection $collection */ $collection = $this->getCategoryTree($storeId, $rootId); $mapping = [$rootId => $subject->getMenu()]; // use nodes stack to avoid recursion foreach ($collection as $category) { if (!isset($mapping[$category->getParentId()])) { continue; } $subject->addIdentity(Category::CACHE_TAG . '_' . $category->getId()); } } }