Skip to content

Commit aa8b6da

Browse files
committed
Imporve Cumulative Layout Shift
1 parent 2532407 commit aa8b6da

File tree

2 files changed

+49
-22
lines changed

2 files changed

+49
-22
lines changed

Block/Categories.php

Lines changed: 42 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,9 @@ class Categories extends \Magento\Framework\View\Element\Template implements \Ma
1515
{
1616
const DEFAULT_CACHE_TAG = 'MAGEPOW_CATEGORIES';
1717

18-
const XML_PATH = 'category_page';
18+
const XML_PATH = 'category_page';
19+
20+
const MEDIA_PATH = 'catalog/category';
1921

2022
public $helper;
2123

@@ -28,23 +30,35 @@ class Categories extends \Magento\Framework\View\Element\Template implements \Ma
2830
public $categoryFactory;
2931

3032
public $catalogHelperOutput;
33+
34+
/**
35+
* @var \Magento\Framework\Image\AdapterFactory
36+
*/
37+
protected $_imageFactory;
38+
39+
protected $_filesystem;
3140

3241
public function __construct(
33-
\Magento\Store\Model\StoreManagerInterface $storeManager,
42+
\Magento\Framework\View\Element\Template\Context $context,
43+
\Magento\Framework\Image\AdapterFactory $imageFactory,
3444
\Magento\Framework\View\Asset\Repository $viewAssetRepo,
3545
\Magento\Framework\Registry $coreRegistry,
3646
\Magento\Catalog\Model\CategoryFactory $categoryFactory,
3747
\Magento\Catalog\Helper\Output $catalogHelperOutput,
48+
\Magento\Store\Model\StoreManagerInterface $storeManager,
3849
\Magepow\Categories\Helper\Data $helper,
39-
\Magento\Framework\View\Element\Template\Context $context,
4050
array $data = []
4151
) {
4252
$this->storeManager = $storeManager;
4353
$this->viewAssetRepo = $viewAssetRepo;
4454
$this->coreRegistry = $coreRegistry;
4555
$this->categoryFactory = $categoryFactory;
4656
$this->catalogHelperOutput = $catalogHelperOutput;
47-
$this->helper = $helper;
57+
$this->_imageFactory = $imageFactory;
58+
$this->_filesystem = $context->getFilesystem();
59+
$this->_directoryRed = $this->_filesystem->getDirectoryRead(\Magento\Framework\App\Filesystem\DirectoryList::MEDIA);
60+
$this->helper = $helper;
61+
4862
parent::__construct($context, $data);
4963
}
5064

@@ -134,24 +148,32 @@ public function getDescription($category)
134148
return trim($categoryDescription);
135149
}
136150

137-
public function getImage($category)
151+
public function getImage($image)
138152
{
139-
$placeholderImageUrl = $this->viewAssetRepo->getUrl(
140-
'Magento_Catalog::images/product/placeholder/small_image.jpg'
141-
);
142-
$image = $category->getImage();
143-
if ($image != null) {
144-
$url = $this->getImageUrl($image);
153+
return $this->getImageUrl($image);
154+
}
155+
156+
public function getImageInfo($image)
157+
{
158+
if(is_object($image)){
159+
$img = $image->getImage();
160+
if(!$img) return $image;
145161
} else {
146-
$url = $placeholderImageUrl;
147-
}
148-
return $url;
162+
$img = $image;
163+
}
164+
$_image = $this->_imageFactory->create();
165+
$absPath = $this->_directoryRed->getAbsolutePath() . str_replace('/pub/media/', '', $img);
166+
if(file_exists($absPath) ){
167+
$_image->open($absPath);
168+
return $_image;
169+
}
170+
return $image;
149171
}
150-
172+
151173
public function getImageUrl($image)
152174
{
153-
$url = false;
154-
if ($image) {
175+
if(is_object($image)) $image = $image->getImage();
176+
if($image) {
155177
if (substr($image, 0, 1) === '/') {
156178
$url = $this->storeManager->getStore()->getBaseUrl(
157179
\Magento\Framework\UrlInterface::URL_TYPE_WEB
@@ -161,7 +183,9 @@ public function getImageUrl($image)
161183
\Magento\Framework\UrlInterface::URL_TYPE_MEDIA
162184
) . 'catalog/category/' . $image;
163185
}
164-
}
186+
} else {
187+
$url = $this->viewAssetRepo->getUrl('Magento_Catalog::images/product/placeholder/small_image.jpg');
188+
}
165189
return $url;
166190
}
167191

view/frontend/templates/categories_widget.phtml

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -19,22 +19,25 @@
1919
$categories = $block->getCategories();
2020
$heading = $block->getHeading();
2121
$description = $block->isShowDescription();
22-
$selector = 'magepow_categories-'.uniqid();
22+
if (empty($categories)) return;
23+
$selector = 'alo-content-'.uniqid();
2324
?>
24-
<?php if (empty($categories)) return ?>
25+
<?php $visible = (int) $this->getData('visible'); $visible++ ?>
26+
<style type="text/css"><?php echo '.' . $selector . ' .item:nth-child(n+' . $visible . '){display: none;} ' . '.' . $selector . ' .item{float:left}' ?></style>
2527
<?php if ($heading): ?>
2628
<h3 class="title"><?php echo $heading ?></h3>
2729
<?php endif; ?>
28-
<div class="<?php echo $selector ?>" data-mage-init='{"gridSlider": {}}'>
30+
<div class="magepow-categories <?php echo $selector ?>" data-mage-init='{"gridSlider": {}}'>
2931
<ul class="grid-slider"
3032
<?php foreach ($this->getFrontendCfg() as $opt) :?>
3133
data-<?php echo $opt ?>='<?php echo $this->getData($opt) ?>'
3234
<?php endforeach;?>
3335
>
3436
<?php foreach ($categories as $cat): ?>
37+
<?php $image = $this->getImageInfo($cat); ?>
3538
<li class="category-item">
3639
<a class="category-url" href="<?php echo $cat->getUrl() ?>">
37-
<img src="<?php echo $this->getImage($cat) ?>" alt="<?php echo $cat->getName() ?>"/>
40+
<img src="<?php echo $this->getImage($cat) ?>" width="<?php echo $image->getOriginalWidth() ?>" height="<?php echo $image->getOriginalHeight() ?>" alt="<?php echo $cat->getName() ?>"/>
3841
</a>
3942
<a href="<?php echo $cat->getUrl() ?>">
4043
<span class="category-name"><?php echo $cat->getName() ?></span>

0 commit comments

Comments
 (0)