Skip to content

Commit

Permalink
Add preload template and options via layout
Browse files Browse the repository at this point in the history
  • Loading branch information
Rob Aimes committed Nov 20, 2020
1 parent 6869281 commit 57a8b1e
Show file tree
Hide file tree
Showing 3 changed files with 84 additions and 0 deletions.
42 changes: 42 additions & 0 deletions Block/Preload.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
<?php
/**
* Copyright © Fisheye Media Ltd. All rights reserved.
* See LICENCE.txt for licence details.
*/

namespace Fisheye\Lazyload\Block;

use Magento\Framework\View\Element\Template;
use Magento\Framework\Serialize\SerializerInterface;

class Preload extends Template
{
/**
* @var SerializerInterface
*/
protected $serializer;

/**
* @param Template\Context $context
* @param SerializerInterface $serializer
* @param array $data
*/
public function __construct(
Template\Context $context,
SerializerInterface $serializer,
array $data = []
) {
parent::__construct($context, $data);
$this->serializer = $serializer;
}

/**
* @return string
*/
public function getJsConfig(): string
{
return $this->serializer->serialize([
'preload_selectors' => $this->getData('preload_elements'),
]);
}
}
28 changes: 28 additions & 0 deletions view/frontend/layout/default.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
<?xml version="1.0"?>
<!--
/**
* Copyright © Fisheye Media Ltd. All rights reserved.
* See LICENCE.txt for licence details.
*/
-->
<page xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:noNamespaceSchemaLocation="urn:magento:framework:View/Layout/etc/page_configuration.xsd">
<body>
<referenceContainer name="before.body.end">
<block name="lazyload.preload.config"
class="Fisheye\Lazyload\Block\Preload"
template="Fisheye_Lazyload::preload.phtml"
ifconfig="catalog/product_image_lazyload/preload">
<arguments>
<!--
* Item "name" attribute should be descriptive to the area
* Item value should be a CSS selector vage enough to select groups of elements for efficiency
-->
<argument name="preload_elements" xsi:type="array">
<item name="header_images" xsi:type="string">header .lazyload</item>
</argument>
</arguments>
</block>
</referenceContainer>
</body>
</page>
14 changes: 14 additions & 0 deletions view/frontend/templates/preload.phtml
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
<?php
/**
* Copyright © Fisheye Media Ltd. All rights reserved.
* See LICENCE.txt for licence details.
*/
/** @var Fisheye\Lazyload\Block\Preload $block */
?>
<script type="text/x-magento-init">
{
"*": {
"Fisheye_Lazyload/js/preload": <?= /* @noEscape */ $block->getJsConfig() ?>
}
}
</script>

0 comments on commit 57a8b1e

Please sign in to comment.