Skip to content

Commit

Permalink
Merge pull request #18 from ProxiBlue/master
Browse files Browse the repository at this point in the history
normalise attribute & thumbnail usage
  • Loading branch information
magiccart authored Feb 2, 2024
2 parents 93e63a3 + 594344b commit fab4273
Show file tree
Hide file tree
Showing 4 changed files with 62 additions and 49 deletions.
53 changes: 33 additions & 20 deletions Block/Categories.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,28 +18,30 @@ class Categories extends \Magento\Framework\View\Element\Template implements \Ma
const XML_PATH = 'category_page';

const MEDIA_PATH = 'catalog/category';

public $helper;

public $helperImage;

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,
Expand All @@ -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;
Expand All @@ -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()
Expand All @@ -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()
{
Expand All @@ -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();

Expand Down Expand Up @@ -213,7 +226,7 @@ public function getImageUrl($image)

return $url;
}

public function isExcluded($id)
{
$excluded = explode(',', $this->getExcludeCategory() ?? '');
Expand Down
26 changes: 13 additions & 13 deletions Block/Cmspage.php
Original file line number Diff line number Diff line change
Expand Up @@ -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()
{
Expand All @@ -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();

Expand Down
14 changes: 7 additions & 7 deletions view/frontend/templates/categories.phtml
Original file line number Diff line number Diff line change
Expand Up @@ -33,31 +33,31 @@ $itemAmount = $block->getItemAmount();
<div class="category-image-container">
<a class="magepow-categories-category" href="<?php echo $cat->getUrl() ?>">
<div class="magepow-categories-category-image">
<img loading="lazy" src="<?php echo $this->getImage($cat) ?>" alt="<?php echo $cat->getName() ?>"/>
<img loading="lazy" src="<?php echo $block->getImage($cat) ?>" alt="<?= $block->escapeHtml($cat->getName()) ?>"/>
</div>
<div class="magepow-categories-category-name">
<span><?php echo $cat->getName() ?></span>
<span><?= $block->escapeHtml($cat->getName()) ?></span>
</div>
</a>
</div>
<?php if($description): ?>
<div class="magepow-categories-category-description">
<?php echo $this->getDescription($cat) ?>
<?= $this->getDescription($cat) ?>
</div>
<?php endif; ?>
<?php if($itemAmount): ?>
<div class="category-item-amount">
<?php echo "(".$cat->getProductCollection()->count();
if($cat->getProductCollection()->count()>0)echo " items)";
else echo " item)";
else echo " item)";
?>
</div>
<?php endif; ?>
<div class="btn-link">
<a class="magepow-categories-category" href="<?php echo $cat->getUrl() ?>"><?= $block->escapeHtml(__('Show now')) ?></a>
</div>
</div>
</div>

</li>
<?php endforeach; ?>
</ul>
</ul>
18 changes: 9 additions & 9 deletions view/frontend/templates/cmspage.phtml
Original file line number Diff line number Diff line change
Expand Up @@ -33,33 +33,33 @@ $itemAmount = $block->getItemAmount();
<div class="category-image-container">
<a class="magepow-categories-category" href="<?php echo $cat->getUrl() ?>">
<div class="magepow-categories-category-image">
<img loading="lazy" src="<?php echo $this->getImage($cat) ?>" alt="<?php echo $cat->getName() ?>"/>
<img loading="lazy" src="<?php echo $block->getImage($cat) ?>" alt="<?= $block->escapeHtml($cat->getName()) ?>"/>
</div>
<div class="magepow-categories-category-name">
<span><?php echo $cat->getName() ?></span>
<span><?= $block->escapeHtml($cat->getName()) ?></span>
</div>
</a>
</div>

<?php if($description): ?>
<div class="magepow-categories-category-description">
<?php echo $this->getDescription($cat) ?>
<?php echo $block->getDescription($cat) ?>
</div>
<?php endif; ?>
<?php if($itemAmount): ?>
<div class="category-item-amount">
<?php echo "(".$cat->getProductCollection()->count();
if($cat->getProductCollection()->count()>0)echo " items)";
else echo " item)";
else echo " item)";
?>
</div>
<?php endif; ?>
<div class="btn-link">
<a class="magepow-categories-category" href="<?php echo $cat->getUrl() ?>"><?= $block->escapeHtml(__('Show now')) ?></a>
</div>
</div>
</div>


</li>
<?php endforeach; ?>
</ul>
</ul>

0 comments on commit fab4273

Please sign in to comment.