From 0c150eec426a2cd55eed95428ae524ce6f2f6c5b Mon Sep 17 00:00:00 2001 From: Ryan Cramer Date: Thu, 21 Feb 2019 09:57:10 -0500 Subject: [PATCH] Update Pageimage::render() method to accept manually defined alt or class attributes --- wire/core/Pageimage.php | 22 +++++++++++++++++++--- 1 file changed, 19 insertions(+), 3 deletions(-) diff --git a/wire/core/Pageimage.php b/wire/core/Pageimage.php index 0daa87b8..02db47d1 100644 --- a/wire/core/Pageimage.php +++ b/wire/core/Pageimage.php @@ -1704,6 +1704,8 @@ class Pageimage extends Pagefile { * - `height` (int): Target height or 0 for current image size (or proportional if width specified). * - `markup` (string): Markup template string (same as $markup argument), or omit for default (same as $markup argument). * - `link` (bool): Link image to original size? Though you may prefer to do this with your own $markup (see examples). (default=false) + * - `alt` (string): Text to use for “alt” attribute (default=text from image description). + * - `class` (string): Text to use for “class” attribute, if `{class}` present in markup (default=''). * - Plus any option available to the $options argument on the `Pageimage::size()` method. * - If you only need width and/or height, you can specify a width x height string, i.e. 123x456 (use 0 for proportional). * @return string @@ -1719,7 +1721,12 @@ class Pageimage extends Pagefile { } if(empty($markup)) { - $markup = "{description}"; + // provide default markup + if(empty($options['class'])) { + $markup = "{alt}"; + } else { + $markup = "{alt}"; + } } if(is_string($options)) { @@ -1739,7 +1746,7 @@ class Pageimage extends Pagefile { $replacements = array(); $properties = array( 'url', 'httpUrl', 'URL', 'HTTPURL', - 'description', 'alt', 'tags', 'ext', + 'description', 'alt', 'tags', 'ext', 'class', 'width', 'height', 'hidpiWidth', 'hidpiHeight', ); @@ -1757,10 +1764,19 @@ class Pageimage extends Pagefile { foreach($properties as $property) { $tag = '{' . $property . '}'; if(strpos($markup, $tag) === false) continue; - $value = $sanitizer->entities1($image->get($property)); + if(($property === 'alt' || $property === 'class') && isset($options[$property])) { + $value = $sanitizer->entities($options[$property]); + } else { + $value = $sanitizer->entities1($image->get($property)); + } $replacements[$tag] = $value; } + if(strpos($markup, '{class}')) { + $class = isset($options['class']) ? $this->wire('sanitizer')->entities($options['class']) : 'pw-pageimage'; + $replacements["{class}"] = $class; + } + if(strpos($markup, '{original.') !== false) { if(!$original) $original = $image->getOriginal(); if(!$original) $original = $image;