From b5200d2dc2d856e9f4d6c1b8daa2066f16b10394 Mon Sep 17 00:00:00 2001 From: Bruno Meilick Date: Wed, 27 Feb 2019 17:49:42 +0100 Subject: [PATCH] =?UTF-8?q?added=20additional=20attributes=20for=20using?= =?UTF-8?q?=20kirbytag=20(srcset:=20=E2=80=A6)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit link, linkclass, class, imgclass, target, rel this also relates to #7 concering usage of class attr Signed-off-by: Bruno Meilick --- classes/Srcset.php | 4 +++- composer.json | 2 +- index.php | 43 +++++++++++++++++++++++++------------ readme.md | 6 ++++-- snippets/srcset-img.php | 4 ++-- snippets/srcset-picture.php | 4 ++-- snippets/srcset.php | 4 ++-- 7 files changed, 43 insertions(+), 24 deletions(-) diff --git a/classes/Srcset.php b/classes/Srcset.php index 0d15fe1..6620b36 100644 --- a/classes/Srcset.php +++ b/classes/Srcset.php @@ -4,7 +4,7 @@ class Srcset { - public static function srcset(\Kirby\Cms\File $file, $preset = 'default', $lazy = null, $prefix = null) + public static function srcset(\Kirby\Cms\File $file, $preset = 'default', $lazy = null, $prefix = null, $class = null, $imgclass = null) { if (!$file || !is_a($file, 'Kirby\Cms\File')) { return null; @@ -22,6 +22,8 @@ public static function srcset(\Kirby\Cms\File $file, $preset = 'default', $lazy 'sources' => \Bnomei\Srcset::sources($file, $preset, $lazy), 'autoSizes' => option('bnomei.srcset.autosizes'), 'prefix' => $prefix ? $prefix : option('bnomei.srcset.prefix'), + 'class' => $class ? $class : option('bnomei.srcset.class', ''), + 'imgclass' => $imgclass ? $imgclass : option('bnomei.srcset.imgclass', ''), ], true); } diff --git a/composer.json b/composer.json index 2998e8e..9f7a1f5 100644 --- a/composer.json +++ b/composer.json @@ -1,7 +1,7 @@ { "name": "bnomei/kirby3-srcset", "type": "kirby-plugin", - "version": "1.3.1", + "version": "1.4.0", "license": "MIT", "description": "Kirby 3 Plugin for creating image srcset using picture or image element", "authors": [ diff --git a/index.php b/index.php index af25d8e..0348318 100644 --- a/index.php +++ b/index.php @@ -30,24 +30,39 @@ ], 'tags' => [ 'srcset' => [ - 'attr' => ['preset', 'lazy', 'link', 'class', 'prefix'], + // https://getkirby.com/docs/reference/text/kirbytags/image + 'attr' => ['preset', 'lazy', 'prefix', 'class', 'imgclass', 'link', 'linkclass', 'target', 'rel'], 'html' => function ($tag) { try { $file = Kirby::instance()->file($tag->value, $tag->parent()); - $preset = (string) $tag->preset; - $prefix = (string) $tag->prefix; - if (\Kirby\Toolkit\Str::contains($preset, ' ') || \Kirby\Toolkit\Str::contains($preset, ',')) { - $preset = str_replace(['[',']',',',' ','px'], ['','',' ',' ',''], $preset); - $preset = array_map(function ($v) { - return trim($v); - }, explode(' ', $preset)); - } if ($file) { - $srcset = \Bnomei\Srcset::srcset($file, $preset, boolval($tag->lazy), $prefix); - if ($tag->link && $tag->class) { - return ''.$srcset.''; - } elseif ($tag->link) { - return ''.$srcset.''; + $preset = (string) $tag->preset; + if (\Kirby\Toolkit\Str::contains($preset, ' ') || \Kirby\Toolkit\Str::contains($preset, ',')) { + $preset = str_replace(['[',']',',',' ','px'], ['','',' ',' ',''], $preset); + $preset = array_map(function ($v) { + return trim($v); + }, explode(' ', $preset)); + } + $prefix = (string) $tag->prefix; + $class = $tag->class ? trim($tag->class) : null; + $imgclass = $tag->imgclass ? trim($tag->imgclass) : null; + $srcset = \Bnomei\Srcset::srcset($file, $preset, boolval($tag->lazy), $prefix, $class, $imgclass); + if ($tag->link) { + $attr = [ + 'href' => trim($tag->link), + ]; + if ($tag->linkclass) { + $attr['class'] = trim($tag->linkclass); + } + if ($tag->target) { + $attr['target'] = trim($tag->target); + } + if ($tag->rel) { + $attr['rel'] = trim($tag->rel); + } + // wrap $srcset in array to avoid encoding + // https://github.com/getkirby/kirby/blob/master/src/Toolkit/Html.php#L367 + return \Kirby\Toolkit\Html::tag('a', [$srcset], $attr); } else { return $srcset.PHP_EOL; } diff --git a/readme.md b/readme.md index d3aaf34..1812991 100644 --- a/readme.md +++ b/readme.md @@ -91,9 +91,11 @@ echo $page->image('ukulele.jpg')->srcset('breakpoints', true, 'data-flickity-laz # but NOT ranges (like imageset does) (srcset: myfile.jpg preset: 320-960, 3) // fails! -# link and class do work like with (image: ) +# class, imgclass, link, linkclass, target and rel do work similar to (image: ) +https://getkirby.com/docs/reference/text/kirbytags/image + (srcset: myfile.jpg link: mypdf.pdf) -(srcset: myfile.jpg class: myclass) +(srcset: myfile.jpg imglass: myclass) ``` diff --git a/snippets/srcset-img.php b/snippets/srcset-img.php index 38fa74b..52d3236 100644 --- a/snippets/srcset-img.php +++ b/snippets/srcset-img.php @@ -1,5 +1,5 @@ - data-preset="" srcset="" src="" alt="" /> + data-preset="" srcset="" src="" alt="" /> diff --git a/snippets/srcset-picture.php b/snippets/srcset-picture.php index deb704a..1f71f9a 100644 --- a/snippets/srcset-picture.php +++ b/snippets/srcset-picture.php @@ -1,4 +1,4 @@ -srcset="" type="" /> src="" alt="" /> +srcset="" type="" /> src="" alt="" /> diff --git a/snippets/srcset.php b/snippets/srcset.php index be2a2cb..77dc6d2 100644 --- a/snippets/srcset.php +++ b/snippets/srcset.php @@ -1,4 +1,4 @@ -srcset="" type="" /> src="" alt="" /> +srcset="" type="" /> src="" alt="" />