Skip to content

Commit

Permalink
Add Hyva support
Browse files Browse the repository at this point in the history
  • Loading branch information
jissereitsma committed Jul 24, 2023
1 parent 919e8fa commit 6d4514a
Show file tree
Hide file tree
Showing 7 changed files with 65 additions and 23 deletions.
8 changes: 4 additions & 4 deletions Block/PictureFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -43,9 +43,9 @@ public function create(
string $htmlTag,
string $srcAttribute = 'src'
): Picture {
/** @var Picture $block */
$block = $this->layout->createBlock(Picture::class);
$block
/** @var Picture $picture */
$picture = $this->layout->createBlock(Picture::class);
$picture
->setOriginalImage($originalImage)
->setImages($images)
->setTitle($this->getAttributeText($htmlTag, 'title'))
Expand All @@ -58,7 +58,7 @@ public function create(
->setLazyLoading($this->getAttributeText($htmlTag, 'loading') == "lazy" ?: $this->config->addLazyLoading())
->setSrcAttribute($srcAttribute)
->setDebug($this->config->isDebugging());
return $block;
return $picture;
}

/**
Expand Down
20 changes: 12 additions & 8 deletions Observer/ReplaceBlockHtml.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,22 +18,22 @@ class ReplaceBlockHtml implements ObserverInterface
* @var HtmlReplacer
*/
private $htmlReplacer;

/**
* @var ShouldModifyOutput
*/
private $shouldModifyOutput;

/**
* @var LayoutInterface
*/
private $layout;

/**
* @var Config
*/
private $config;

/**
* ReplaceTags constructor.
*
Expand All @@ -53,22 +53,26 @@ public function __construct(
$this->layout = $layout;
$this->config = $config;
}


/**
* @param Observer $observer
* @return void
*/
public function execute(Observer $observer)
{
$block = $observer->getEvent()->getBlock();
if (!$block->getData('ttl')) {
return;
}

if (!$this->config->hasFullPageCacheEnabled($this->layout)) {
return;
}

if ($this->shouldModifyOutput->shouldModifyOutput($this->layout) === false) {
return;
}

$transport = $observer->getEvent()->getTransport();
$html = $this->htmlReplacer->replace($transport->getHtml());
$transport->setHtml($html);
Expand Down
2 changes: 1 addition & 1 deletion Plugin/ReplaceTagsInHtml.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ class ReplaceTagsInHtml
* @var ShouldModifyOutput
*/
private $shouldModifyOutput;

/**
* ReplaceTags constructor.
*
Expand Down
5 changes: 4 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -30,4 +30,7 @@ A module `Foo_Bar` could add a `etc/di.xml` file to add a new convertor (a class
- AVIF
- JPEG XL
- WebP2

- Hyva compatibility
- Hyva\YireoNextGenImages\Plugin\UrlConvertorPlugin (`image\.(img|full|thumb|url`)
- Hyva\YireoNextGenImages\Plugin\ConverterPlugin (`img|full|thumb|url`)
- Hyva\YireoNextGenImages\Plugin\HtmlReplacerPlugin (`:src`): NOT NEEDED?
10 changes: 5 additions & 5 deletions Test/Unit/Util/HtmlReplacerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,7 @@ public function getTestReplaceWithTestImageArguments(): array
],
[
'<div><img src="data:image/gif;base64,foobar"/></div>',
'<div><DUMMYPICTURE/></div>'
'<div><img src="data:image/gif;base64,foobar"/></div>'
],
[
'<div style="background-image: url(http://localhost/img/test.png);"></div>',
Expand All @@ -102,12 +102,12 @@ public function getTestReplaceWithTestImageArguments(): array
'<div><DUMMYPICTURE/><DUMMYPICTURE/><DUMMYPICTURE/></div>'
],
[
"<script>var imgElement = '<img src=\"...\" />';</script>",
"<script>var imgElement = '<img src=\"...\" />';</script>",
"<div><script>var imgElement = '<img src=\"...\" />';</script></div>",
"<div><script>var imgElement = '<img src=\"...\" />';</script></div>",
],
[
'<div @click="fullscreen = false; $nextTick(() => calcPageSize())"><img src="/img/test.png"/></div>',
'<div @click="fullscreen = false; $nextTick(() => calcPageSize())"><DUMMYPICTURE/></div>'
'<div><div @click="fullscreen = false; $nextTick(() => calcPageSize())"><img src="/img/test.png"/></div></div>',
'<div><div @click="fullscreen = false; $nextTick(() => calcPageSize())"><DUMMYPICTURE/></div></div>'
],
];
}
Expand Down
4 changes: 0 additions & 4 deletions etc/frontend/events.xml
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,4 @@
<event name="view_block_abstract_to_html_after">
<observer name="Yireo_NextGenImages::replaceBlockHtml" instance="Yireo\NextGenImages\Observer\ReplaceBlockHtml" />
</event>

<event name="hyva_graphql_render_before_slider_products_query">
<observer name="Yireo_NextGenImages::addUrlWebpToGraphqlQuery" instance="Yireo\NextGenImages\Observer\AddUrlWebpToGraphqlQuery"/>
</event>
</config>
39 changes: 39 additions & 0 deletions view/frontend/templates/hyva/picture.phtml
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
<?php declare(strict_types=1);

use Yireo\NextGenImages\Block\Picture;

/** @var $block Picture */
$width = ($block->getWidth()) ? ' width="' . $block->getWidth() . '"' : '';
$height = ($block->getHeight()) ? ' height="' . $block->getHeight() . '"' : '';
$class = ($block->getClass()) ? ' class="' . $block->getClass() . '"' : '';
$lazyLoading = ($block->getLazyLoading()) ? ' loading="lazy" ' : '';

$originalTag = trim($block->getOriginalTag());
$originalTag = preg_replace('/(\/?)>$/', $lazyLoading . '\1>', $originalTag);

$originalImage = $block->getOriginalImage();
$originalImageType = $block->getOriginalImageType();

$srcsetAttribute = $block->getIsDataSrc() ? ':data-srcset' : ':srcset';

?>
<?php if ($block->getDebug()): ?>
<!-- <?= /* @noEscape */
$block->getOriginalTag() ?> -->
<?php endif; ?>

<picture<?= /* @noEscape */
$class ?>>
<?php foreach ($block->getImages() as $image): ?>
<source type="<?= /* @noEscape */
$image->getMimetype() ?>" <?= /* @noEscape */
$srcsetAttribute ?>="<?= /* @noEscape */
$image->getUrl() ?>">
<?php endforeach; ?>
<source type="<?= /* @noEscape */
$originalImageType ?>" <?= /* @noEscape */
$srcsetAttribute ?>="<?= /* @noEscape */
$originalImage ?>">
<?= /* @noEscape */
$originalTag ?>
</picture>

0 comments on commit 6d4514a

Please sign in to comment.