Skip to content

Commit fab4273

Browse files
authored
Merge pull request #18 from ProxiBlue/master
normalise attribute & thumbnail usage
2 parents 93e63a3 + 594344b commit fab4273

File tree

4 files changed

+62
-49
lines changed

4 files changed

+62
-49
lines changed

Block/Categories.php

Lines changed: 33 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -18,28 +18,30 @@ class Categories extends \Magento\Framework\View\Element\Template implements \Ma
1818
const XML_PATH = 'category_page';
1919

2020
const MEDIA_PATH = 'catalog/category';
21-
21+
2222
public $helper;
2323

2424
public $helperImage;
2525

2626
public $storeManager;
2727

2828
public $viewAssetRepo;
29-
29+
3030
public $coreRegistry;
3131

3232
public $categoryFactory;
3333

3434
public $catalogHelperOutput;
3535

36+
public $attributesToSelect;
37+
3638
/**
3739
* @var \Magento\Framework\Image\AdapterFactory
3840
*/
3941
protected $_imageFactory;
4042

4143
protected $_filesystem;
42-
44+
4345
public function __construct(
4446
\Magento\Framework\View\Element\Template\Context $context,
4547
\Magento\Framework\Image\AdapterFactory $imageFactory,
@@ -50,7 +52,7 @@ public function __construct(
5052
\Magento\Store\Model\StoreManagerInterface $storeManager,
5153
\Magepow\Categories\Helper\Data $helper,
5254
array $data = []
53-
) {
55+
) {
5456
$this->storeManager = $storeManager;
5557
$this->coreRegistry = $coreRegistry;
5658
$this->categoryFactory = $categoryFactory;
@@ -62,6 +64,19 @@ public function __construct(
6264
$this->helper = $helper;
6365

6466
parent::__construct($context, $data);
67+
68+
$this->attributesToSelect = [
69+
'name',
70+
'url_key',
71+
'url_path',
72+
'image',
73+
'description'
74+
];
75+
if($this->isShowThumbnail()) {
76+
$this->attributesToSelect[] = 'magepow_thumbnail';
77+
unset($this->attributesToSelect['image']);
78+
}
79+
6580
}
6681

6782
protected function getCacheLifetime()
@@ -86,35 +101,35 @@ public function getIdentities()
86101
return [self::DEFAULT_CACHE_TAG, self::DEFAULT_CACHE_TAG . '_' . $categoryId];
87102
}
88103

89-
public function getLayout()
104+
public function getLayout()
90105
{
91106
return $this->helper->getConfig(self::XML_PATH . '/layout');
92107
}
93108

94-
public function getHeading()
109+
public function getHeading()
95110
{
96111
return $this->helper->getConfig(self::XML_PATH . '/heading');
97-
}
112+
}
98113

99-
public function isShowDescription()
114+
public function isShowDescription()
100115
{
101116
return $this->helper->getConfig(self::XML_PATH . '/description');
102-
}
117+
}
103118

104-
public function isShowThumbnail()
119+
public function isShowThumbnail()
105120
{
106121
return $this->helper->getConfig(self::XML_PATH . '/thumbnail');
107-
}
122+
}
108123

109-
public function getItemAmount()
124+
public function getItemAmount()
110125
{
111126
return $this->helper->getConfig(self::XML_PATH . '/item_amount');
112-
}
113-
114-
public function getSortAttribute()
127+
}
128+
129+
public function getSortAttribute()
115130
{
116131
return $this->helper->getConfig(self::XML_PATH . '/sort_attribute');
117-
}
132+
}
118133

119134
public function getExcludeCategory()
120135
{
@@ -136,10 +151,8 @@ public function getCategories()
136151
if ($this->isExcluded($categoryId)) return;
137152

138153
$sortAttribute = $this->getSortAttribute();
139-
$attributesSelect = ['name', 'url_key', 'url_path', 'image','description'];
140-
if($this->isShowThumbnail()) $attributesSelect[] = 'magepow_thumbnail';
141154
$categories = $this->categoryFactory->create()->getCollection()
142-
->addAttributeToSelect($attributesSelect)
155+
->addAttributeToSelect($this->attributesToSelect)
143156
->addAttributeToFilter('parent_id', $categoryId)
144157
->addIsActiveFilter();
145158

@@ -213,7 +226,7 @@ public function getImageUrl($image)
213226

214227
return $url;
215228
}
216-
229+
217230
public function isExcluded($id)
218231
{
219232
$excluded = explode(',', $this->getExcludeCategory() ?? '');

Block/Cmspage.php

Lines changed: 13 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -18,40 +18,40 @@ class Cmspage extends Categories
1818

1919
const XML_PATH = 'home_page';
2020

21-
public function getLayout()
21+
public function getLayout()
2222
{
2323
return $this->helper->getConfig(self::XML_PATH . '/layout');
2424
}
2525

26-
public function getHeading()
26+
public function getHeading()
2727
{
2828
return $this->helper->getConfig(self::XML_PATH . '/heading');
29-
}
29+
}
3030

31-
public function isShowDescription()
31+
public function isShowDescription()
3232
{
3333
return $this->helper->getConfig(self::XML_PATH . '/description');
34-
}
34+
}
3535

36-
public function isShowThumbnail()
36+
public function isShowThumbnail()
3737
{
3838
return $this->helper->getConfig(self::XML_PATH . '/thumbnail');
39-
}
39+
}
4040

41-
public function getItemAmount()
41+
public function getItemAmount()
4242
{
4343
return $this->helper->getConfig(self::XML_PATH . '/item_amount');
44-
}
44+
}
4545

46-
public function getSortAttribute()
46+
public function getSortAttribute()
4747
{
4848
return $this->helper->getConfig(self::XML_PATH . '/sort_attribute');
49-
}
49+
}
5050

5151
public function getCategorySelect()
5252
{
5353
return $this->helper->getConfig(self::XML_PATH . '/category_select');
54-
}
54+
}
5555

5656
public function getCategories()
5757
{
@@ -60,7 +60,7 @@ public function getCategories()
6060

6161
$sortAttribute = $this->getSortAttribute();
6262
$categories = $this->categoryFactory->create()->getCollection()
63-
->addAttributeToSelect(['name', 'url_key', 'url_path', 'image', 'description'])
63+
->addAttributeToSelect($this->attributesToSelect)
6464
->addIdFilter($categoryIds)
6565
->addIsActiveFilter();
6666

view/frontend/templates/categories.phtml

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -33,31 +33,31 @@ $itemAmount = $block->getItemAmount();
3333
<div class="category-image-container">
3434
<a class="magepow-categories-category" href="<?php echo $cat->getUrl() ?>">
3535
<div class="magepow-categories-category-image">
36-
<img loading="lazy" src="<?php echo $this->getImage($cat) ?>" alt="<?php echo $cat->getName() ?>"/>
36+
<img loading="lazy" src="<?php echo $block->getImage($cat) ?>" alt="<?= $block->escapeHtml($cat->getName()) ?>"/>
3737
</div>
3838
<div class="magepow-categories-category-name">
39-
<span><?php echo $cat->getName() ?></span>
39+
<span><?= $block->escapeHtml($cat->getName()) ?></span>
4040
</div>
4141
</a>
4242
</div>
4343
<?php if($description): ?>
4444
<div class="magepow-categories-category-description">
45-
<?php echo $this->getDescription($cat) ?>
45+
<?= $this->getDescription($cat) ?>
4646
</div>
4747
<?php endif; ?>
4848
<?php if($itemAmount): ?>
4949
<div class="category-item-amount">
5050
<?php echo "(".$cat->getProductCollection()->count();
5151
if($cat->getProductCollection()->count()>0)echo " items)";
52-
else echo " item)";
52+
else echo " item)";
5353
?>
5454
</div>
5555
<?php endif; ?>
5656
<div class="btn-link">
5757
<a class="magepow-categories-category" href="<?php echo $cat->getUrl() ?>"><?= $block->escapeHtml(__('Show now')) ?></a>
58-
</div>
58+
</div>
5959
</div>
60-
60+
6161
</li>
6262
<?php endforeach; ?>
63-
</ul>
63+
</ul>

view/frontend/templates/cmspage.phtml

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -33,33 +33,33 @@ $itemAmount = $block->getItemAmount();
3333
<div class="category-image-container">
3434
<a class="magepow-categories-category" href="<?php echo $cat->getUrl() ?>">
3535
<div class="magepow-categories-category-image">
36-
<img loading="lazy" src="<?php echo $this->getImage($cat) ?>" alt="<?php echo $cat->getName() ?>"/>
36+
<img loading="lazy" src="<?php echo $block->getImage($cat) ?>" alt="<?= $block->escapeHtml($cat->getName()) ?>"/>
3737
</div>
3838
<div class="magepow-categories-category-name">
39-
<span><?php echo $cat->getName() ?></span>
39+
<span><?= $block->escapeHtml($cat->getName()) ?></span>
4040
</div>
4141
</a>
4242
</div>
43-
43+
4444
<?php if($description): ?>
4545
<div class="magepow-categories-category-description">
46-
<?php echo $this->getDescription($cat) ?>
46+
<?php echo $block->getDescription($cat) ?>
4747
</div>
4848
<?php endif; ?>
4949
<?php if($itemAmount): ?>
5050
<div class="category-item-amount">
5151
<?php echo "(".$cat->getProductCollection()->count();
5252
if($cat->getProductCollection()->count()>0)echo " items)";
53-
else echo " item)";
53+
else echo " item)";
5454
?>
5555
</div>
5656
<?php endif; ?>
5757
<div class="btn-link">
5858
<a class="magepow-categories-category" href="<?php echo $cat->getUrl() ?>"><?= $block->escapeHtml(__('Show now')) ?></a>
59-
</div>
59+
</div>
6060
</div>
61-
62-
61+
62+
6363
</li>
6464
<?php endforeach; ?>
65-
</ul>
65+
</ul>

0 commit comments

Comments
 (0)