Hello–
I’m coming back to an old project that briefly used the example plugin solution shown below for generating srcset
markup.
This was an updated version of Quicktip: Reusing KirbyTags in custom tags | Kirby CMS found here Accessing new srcset via Kirbytext - #9 by colinjohnston
<?php
$originalTag = Kirby\Text\KirbyTag::$types['image'];
Kirby::plugin('my-name/plugin-name', [
'tags' => [
'image' => [
'attr' => array_merge(
$originalTag['attr'],
[
'srcset'
]),
'html' => function($tag) use ($originalTag) {
$file = $tag->file($tag->value());
// gets srcset array from config
$presets = option('thumbs.srcsets.default');
// sets srcset as kirbytag array with fallback to config array
$srcset = $tag->srcset ? explode(',', $tag->srcset): $presets;
// casts kirbytext array strings to ints
$srcset = array_map(function($item) {
return (int) $item;
}, $srcset);
$result = $originalTag['html']($tag);
if (! $file === true || is_null($srcset) === true) {
return $result;
}
$pattern = '/<img.*?>/i';
// build a new image tag with the srcset
$image = Html::img($tag->src, [
'width' => $tag->width,
'height' => $tag->height,
'class' => $tag->imgclass,
'title' => $tag->title,
'alt' => $tag->alt ?? ' ',
'srcset' => $file->srcset($srcset),
]);
// replace the old image tag
$result = preg_replace($pattern, $image , $result);
return $result;
}
]
]
]);
(I had replaced this ultimately with Bruno Meilick’s kirby3-srcset plugin, but that no longer works for me with later versions of PHP and Kirby, so I’m looking for a simple replacement that just does the srcset
output.)
I was hoping to use the above solution in the updated installation of the old project, now on Kirby 3.8.4.1 on PHP 8.1.
Issue
I’m not getting any srcset
output at all.
The output works fine otherwise—it’s exactly the same as it was prior to setting up the plugin (img
output here in context of surrounding HTML):
<figure>
<a class="hb-single" href="http://colinjohnston.local/media/pages/projects/rollbar-account-dashboard/86f1c79bab-1616036170/rollbar-items-view-old.png">
<img alt="Original Rollbar Items View" src="http://colinjohnston.local/media/pages/projects/rollbar-account-dashboard/86f1c79bab-1616036170/rollbar-items-view-old.png">
</a>
<figcaption>
<strong>Before – repetitive overload:</strong> The Item View is rich in project data. But having to wade through many individual screens to find an issue is difficult.
</figcaption>
</figure>
To test that the plugin itself is working, I added a test tag to the new image tag output, and it comes through.
If I try and add srcset: 300, 600, 900, 1200
to my KirbyTag entry, it comes out as (img
output here in context of surrounding HTML):
<figure>
<a class="hb-single" href="http://colinjohnston.local/media/pages/projects/rollbar-account-dashboard/86f1c79bab-1616036170/rollbar-items-view-old.png">
<img alt="Original Rollbar Items View" src="http://colinjohnston.local/media/pages/projects/rollbar-account-dashboard/86f1c79bab-1616036170/rollbar-items-view-old.png" srcset="300, 600, 900, 1200">
</a>
<figcaption>
<strong>Before – repetitive overload:</strong> The Item View is rich in project data. But having to wade through many individual screens to find an issue is difficult.
</figcaption>
</figure>
I’m probably missing something terribly obvious; it’s been a long time since I’ve worked with Kirby and srcset
.
Any ideas how to test, fix, or improve?