From 594344be90ce8c8105274cf0114e0d9016b2c5cd Mon Sep 17 00:00:00 2001 From: Lucas van Staden Date: Thu, 1 Feb 2024 10:56:12 +0800 Subject: [PATCH] adjust to use same attributes to select and set cmspage to also use thumbnails if enabled --- Block/Categories.php | 53 +++++++++++++++--------- Block/Cmspage.php | 26 ++++++------ view/frontend/templates/categories.phtml | 14 +++---- view/frontend/templates/cmspage.phtml | 18 ++++---- 4 files changed, 62 insertions(+), 49 deletions(-) diff --git a/Block/Categories.php b/Block/Categories.php index 3f1f421..71a0c23 100644 --- a/Block/Categories.php +++ b/Block/Categories.php @@ -18,7 +18,7 @@ class Categories extends \Magento\Framework\View\Element\Template implements \Ma const XML_PATH = 'category_page'; const MEDIA_PATH = 'catalog/category'; - + public $helper; public $helperImage; @@ -26,20 +26,22 @@ class Categories extends \Magento\Framework\View\Element\Template implements \Ma public $storeManager; public $viewAssetRepo; - + public $coreRegistry; public $categoryFactory; public $catalogHelperOutput; + public $attributesToSelect; + /** * @var \Magento\Framework\Image\AdapterFactory */ protected $_imageFactory; protected $_filesystem; - + public function __construct( \Magento\Framework\View\Element\Template\Context $context, \Magento\Framework\Image\AdapterFactory $imageFactory, @@ -50,7 +52,7 @@ public function __construct( \Magento\Store\Model\StoreManagerInterface $storeManager, \Magepow\Categories\Helper\Data $helper, array $data = [] - ) { + ) { $this->storeManager = $storeManager; $this->coreRegistry = $coreRegistry; $this->categoryFactory = $categoryFactory; @@ -62,6 +64,19 @@ public function __construct( $this->helper = $helper; parent::__construct($context, $data); + + $this->attributesToSelect = [ + 'name', + 'url_key', + 'url_path', + 'image', + 'description' + ]; + if($this->isShowThumbnail()) { + $this->attributesToSelect[] = 'magepow_thumbnail'; + unset($this->attributesToSelect['image']); + } + } protected function getCacheLifetime() @@ -86,35 +101,35 @@ public function getIdentities() return [self::DEFAULT_CACHE_TAG, self::DEFAULT_CACHE_TAG . '_' . $categoryId]; } - public function getLayout() + public function getLayout() { return $this->helper->getConfig(self::XML_PATH . '/layout'); } - public function getHeading() + public function getHeading() { return $this->helper->getConfig(self::XML_PATH . '/heading'); - } + } - public function isShowDescription() + public function isShowDescription() { return $this->helper->getConfig(self::XML_PATH . '/description'); - } + } - public function isShowThumbnail() + public function isShowThumbnail() { return $this->helper->getConfig(self::XML_PATH . '/thumbnail'); - } + } - public function getItemAmount() + public function getItemAmount() { return $this->helper->getConfig(self::XML_PATH . '/item_amount'); - } - - public function getSortAttribute() + } + + public function getSortAttribute() { return $this->helper->getConfig(self::XML_PATH . '/sort_attribute'); - } + } public function getExcludeCategory() { @@ -136,10 +151,8 @@ public function getCategories() if ($this->isExcluded($categoryId)) return; $sortAttribute = $this->getSortAttribute(); - $attributesSelect = ['name', 'url_key', 'url_path', 'image','description']; - if($this->isShowThumbnail()) $attributesSelect[] = 'magepow_thumbnail'; $categories = $this->categoryFactory->create()->getCollection() - ->addAttributeToSelect($attributesSelect) + ->addAttributeToSelect($this->attributesToSelect) ->addAttributeToFilter('parent_id', $categoryId) ->addIsActiveFilter(); @@ -213,7 +226,7 @@ public function getImageUrl($image) return $url; } - + public function isExcluded($id) { $excluded = explode(',', $this->getExcludeCategory() ?? ''); diff --git a/Block/Cmspage.php b/Block/Cmspage.php index 5ef30a8..9bc6291 100644 --- a/Block/Cmspage.php +++ b/Block/Cmspage.php @@ -18,40 +18,40 @@ class Cmspage extends Categories const XML_PATH = 'home_page'; - public function getLayout() + public function getLayout() { return $this->helper->getConfig(self::XML_PATH . '/layout'); } - public function getHeading() + public function getHeading() { return $this->helper->getConfig(self::XML_PATH . '/heading'); - } + } - public function isShowDescription() + public function isShowDescription() { return $this->helper->getConfig(self::XML_PATH . '/description'); - } + } - public function isShowThumbnail() + public function isShowThumbnail() { return $this->helper->getConfig(self::XML_PATH . '/thumbnail'); - } + } - public function getItemAmount() + public function getItemAmount() { return $this->helper->getConfig(self::XML_PATH . '/item_amount'); - } + } - public function getSortAttribute() + public function getSortAttribute() { return $this->helper->getConfig(self::XML_PATH . '/sort_attribute'); - } + } public function getCategorySelect() { return $this->helper->getConfig(self::XML_PATH . '/category_select'); - } + } public function getCategories() { @@ -60,7 +60,7 @@ public function getCategories() $sortAttribute = $this->getSortAttribute(); $categories = $this->categoryFactory->create()->getCollection() - ->addAttributeToSelect(['name', 'url_key', 'url_path', 'image', 'description']) + ->addAttributeToSelect($this->attributesToSelect) ->addIdFilter($categoryIds) ->addIsActiveFilter(); diff --git a/view/frontend/templates/categories.phtml b/view/frontend/templates/categories.phtml index 16101d3..8a04e33 100644 --- a/view/frontend/templates/categories.phtml +++ b/view/frontend/templates/categories.phtml @@ -33,31 +33,31 @@ $itemAmount = $block->getItemAmount();
- <?php echo $cat->getName() ?> + <?= $block->escapeHtml($cat->getName()) ?>
- getName() ?> + escapeHtml($cat->getName()) ?>
- getDescription($cat) ?> + getDescription($cat) ?>
getProductCollection()->count(); if($cat->getProductCollection()->count()>0)echo " items)"; - else echo " item)"; + else echo " item)"; ?>
+ - + - \ No newline at end of file + diff --git a/view/frontend/templates/cmspage.phtml b/view/frontend/templates/cmspage.phtml index 9841aa2..430446e 100644 --- a/view/frontend/templates/cmspage.phtml +++ b/view/frontend/templates/cmspage.phtml @@ -33,33 +33,33 @@ $itemAmount = $block->getItemAmount();
- <?php echo $cat->getName() ?> + <?= $block->escapeHtml($cat->getName()) ?>
- getName() ?> + escapeHtml($cat->getName()) ?>
- +
- getDescription($cat) ?> + getDescription($cat) ?>
getProductCollection()->count(); if($cat->getProductCollection()->count()>0)echo " items)"; - else echo " item)"; + else echo " item)"; ?>
+ - - + + - \ No newline at end of file +