Skip to content

Commit 92d0632

Browse files
committed
Add noscript tag option
1 parent 86e7e5e commit 92d0632

File tree

9 files changed

+116
-12
lines changed

9 files changed

+116
-12
lines changed

Model/Config.php

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ class Config extends \Magento\Framework\App\Helper\AbstractHelper
1818
const XML_PATH_ENABLED = 'mflazyzoad/general/enabled';
1919
const XML_PATH_AMP_ENABLED = 'pramp/general/enabled';
2020
const XML_PATH_LAZY_BLOCKS = 'mflazyzoad/general/lazy_blocks';
21+
const XML_PATH_LAZY_NOSCRIPT = 'mflazyzoad/general/noscript';
2122

2223
/**
2324
* @var array
@@ -42,6 +43,14 @@ public function getConfig($path)
4243
);
4344
}
4445

46+
/**
47+
* @return bool
48+
*/
49+
public function isNoScriptEnabled()
50+
{
51+
return (bool)$this->getConfig(self::XML_PATH_LAZY_NOSCRIPT);
52+
}
53+
4554
/**
4655
* Retrieve alloved blocks info
4756
* @return array
Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
<?php
2+
/**
3+
* Copyright © Magefan ([email protected]). All rights reserved.
4+
* Please visit Magefan.com for license details (https://magefan.com/end-user-license-agreement).
5+
*
6+
* Glory to Ukraine! Glory to the heroes!
7+
*/
8+
9+
declare(strict_types = 1);
10+
11+
namespace Magefan\LazyLoad\Observer;
12+
13+
use Magefan\LazyLoad\Model\Config;
14+
use Magento\Framework\Event\ObserverInterface;
15+
16+
/**
17+
* Class LayoutLoadBeforeObserver used to add attribute to layout
18+
*/
19+
class LayoutLoadBeforeObserver implements ObserverInterface
20+
{
21+
/**
22+
* @var Config
23+
*/
24+
protected $config;
25+
26+
/**
27+
* LayoutLoadBeforeObserver constructor.
28+
* @param Config $config
29+
*/
30+
public function __construct(
31+
Config $config
32+
) {
33+
$this->config = $config;
34+
}
35+
36+
/**
37+
* @param \Magento\Framework\Event\Observer $observer
38+
* @return void
39+
* @SuppressWarnings(PHPMD.UnusedFormalParameter)
40+
*/
41+
public function execute(\Magento\Framework\Event\Observer $observer)
42+
{
43+
if ($this->config->getEnabled()) {
44+
$layout = $observer->getLayout();
45+
if ($this->config->isNoScriptEnabled()) {
46+
$layout->getUpdate()->addHandle('mflazyzoad_no_js');
47+
}
48+
}
49+
}
50+
}

Plugin/BlockPlugin.php

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -79,12 +79,16 @@ public function afterToHtml(\Magento\Framework\View\Element\AbstractBlock $block
7979

8080
$html = str_replace($pixelSrc, $tmpSrc, $html);
8181

82+
$noscript = '';
83+
if ($this->config->isNoScriptEnabled()) {
84+
$noscript = '<noscript>
85+
<img src="$2" $1 $3 />
86+
</noscript>';
87+
}
88+
8289
$html = preg_replace('#<img(?!\s+mfdislazy)([^>]*)(?:\ssrc="([^"]*)")([^>]*)\/?>#isU', '<img ' .
8390
' data-original="$2" $1 $3/>
84-
<noscript>
85-
<img src="$2" $1 $3 />
86-
</noscript>
87-
', $html);
91+
' . $noscript, $html);
8892

8993
$html = str_replace(' data-original=', $pixelSrc . ' data-original=', $html);
9094

etc/adminhtml/system.xml

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,11 @@
3838
folder/sometemplate.phtml
3939
]]></comment>
4040
</field>
41+
<field id="noscript" showInDefault="1" showInStore="1" showInWebsite="1" sortOrder="40" translate="label comment" type="select">
42+
<label>Include Noscript HTML Tag</label>
43+
<comment>The noscript HTML element defines a section of HTML to be inserted if a script type on the page is unsupported or if scripting is currently turned off in the browser. This option enables to display of images even when JavaScript is disabled in the browser and lazy load js script cannot be loaded. Note that when enabled it adds extra HTML tags to the page.</comment>
44+
<source_model>Magento\Config\Model\Config\Source\Yesno</source_model>
45+
</field>
4146
</group>
4247
</section>
4348
</system>

etc/config.xml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ blog.post.relatedproducts
2525
product.info.description</lazy_blocks>
2626
<mfmodule>LazyLoad</mfmodule>
2727
<mftype>1</mftype>
28+
<noscript>1</noscript>
2829
</general>
2930
</mflazyzoad>
3031
</default>

etc/frontend/events.xml

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
<?xml version="1.0"?>
2+
<!--
3+
/**
4+
* Copyright © Magefan ([email protected]). All rights reserved.
5+
* Please visit Magefan.com for license details (https://magefan.com/end-user-license-agreement).
6+
*
7+
* Glory to Ukraine! Glory to the heroes!
8+
*/
9+
-->
10+
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:Event/etc/events.xsd">
11+
<event name="layout_load_before">
12+
<observer name="magefan_lazyload_layout_load_before" instance="Magefan\LazyLoad\Observer\LayoutLoadBeforeObserver" />
13+
</event>
14+
</config>

view/frontend/layout/default.xml

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,6 @@
99
-->
1010
<page xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:View/Layout/etc/page_configuration.xsd">
1111
<body>
12-
<attribute name="class" value="mflazyzoad-no-js" />
1312
<referenceBlock name="after.body.start">
1413
<block class="Magefan\LazyLoad\Block\Lazy" name="magefan.lazyload" template="Magefan_LazyLoad::lazy.phtml" >
1514
<arguments>
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
<?xml version="1.0"?>
2+
<!--
3+
/**
4+
* Copyright © Magefan ([email protected]). All rights reserved.
5+
* Please visit Magefan.com for license details (https://magefan.com/end-user-license-agreement).
6+
*
7+
* Glory to Ukraine! Glory to the heroes!
8+
*/
9+
-->
10+
<page xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:View/Layout/etc/page_configuration.xsd">
11+
<body>
12+
<attribute name="class" value="mflazyzoad-no-js" />
13+
</body>
14+
</page>

view/frontend/templates/lazy.phtml

Lines changed: 15 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -6,15 +6,23 @@
66
* Glory to Ukraine! Glory to the heroes!
77
*/
88
?>
9-
<style>
10-
.mflazyzoad-no-js [data-original],
11-
.mflazyzoad-no-js [data-originalset] {
12-
display: none!important;
13-
}
14-
</style>
9+
<?php
10+
$objectManager = \Magento\Framework\App\ObjectManager::getInstance();
11+
$config = $objectManager->get(\Magefan\LazyLoad\Model\Config::class);
12+
?>
13+
<?php if ($config->isNoScriptEnabled()) { ?>
14+
<style>
15+
.mflazyzoad-no-js [data-original],
16+
.mflazyzoad-no-js [data-originalset] {
17+
display: none!important;
18+
}
19+
</style>
20+
<?php } ?>
1521
<script data-rocketjavascript="false">
1622
(function(){
17-
document.body.className = document.body.className.replace('mflazyzoad-no-js', '');
23+
<?php if ($config->isNoScriptEnabled()) { ?>
24+
document.body.className = document.body.className.replace('mflazyzoad-no-js', '');
25+
<?php } ?>
1826
var jsSrc = '<?php echo $this->getViewFileUrl('Magefan_LazyLoad::js/lazyload.min.js'); ?>';
1927
function loadScript(e,t){var a,n,r;n=!1,(a=document.createElement("script")).type="text/javascript",a.src=e,a.onload=a.onreadystatechange=function(){n||this.readyState&&"complete"!=this.readyState||(n=!0,t())},(r=document.getElementsByTagName("script")[0]).parentNode.insertBefore(a,r)}
2028
loadScript(jsSrc, function(){

0 commit comments

Comments
 (0)