Skip to content

Commit

Permalink
ENH Align rendering of image genercated by ImageShortCodeProvider wit…
Browse files Browse the repository at this point in the history
…h generic Image generation
  • Loading branch information
Maxime Rainville committed Apr 29, 2024
1 parent ff5e8a7 commit 9aa6bda
Show file tree
Hide file tree
Showing 6 changed files with 22 additions and 9 deletions.
24 changes: 17 additions & 7 deletions src/Shortcodes/ImageShortcodeProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,13 @@
use Psr\SimpleCache\CacheInterface;
use SilverStripe\Assets\File;
use SilverStripe\Assets\Image;
use SilverStripe\Assets\Storage\DBFile;
use SilverStripe\Core\Convert;
use SilverStripe\Core\Flushable;
use SilverStripe\Core\Injector\Injector;
use SilverStripe\Dev\Deprecation;
use SilverStripe\ORM\DB;
use SilverStripe\View\ArrayData;
use SilverStripe\View\HTML;
use SilverStripe\View\Parsers\ShortcodeHandler;
use SilverStripe\View\Parsers\ShortcodeParser;
Expand Down Expand Up @@ -75,10 +80,10 @@ public static function handle_shortcode($args, $content, $parser, $shortcode, $e
}

// Check if a resize is required
$manipulatedRecord = $record;
$width = null;
$height = null;
$grant = static::getGrant($record);
$src = $record->getURL($grant);
if ($record instanceof Image) {
$width = isset($args['width']) ? (int) $args['width'] : null;
$height = isset($args['height']) ? (int) $args['height'] : null;
Expand All @@ -87,7 +92,7 @@ public static function handle_shortcode($args, $content, $parser, $shortcode, $e
$resized = $record->ResizedImage($width, $height);
// Make sure that the resized image actually returns an image
if ($resized) {
$src = $resized->getURL($grant);
$manipulatedRecord = $resized;
}
}
}
Expand All @@ -102,7 +107,7 @@ public static function handle_shortcode($args, $content, $parser, $shortcode, $e
// Use all other shortcode arguments
$args,
// But enforce some values
['id' => '', 'src' => $src]
['id' => '', 'src' => '']
);

// If file was not found then use the Title value from static::find_error_record() for the alt attr
Expand All @@ -112,11 +117,14 @@ public static function handle_shortcode($args, $content, $parser, $shortcode, $e

// Clean out any empty attributes (aside from alt) and anything not whitelisted
$whitelist = static::config()->get('attribute_whitelist');
$attrs = array_filter($attrs ?? [], function ($v, $k) use ($whitelist) {
return in_array($k, $whitelist) && (strlen(trim($v ?? '')) || $k === 'alt');
}, ARRAY_FILTER_USE_BOTH);
foreach ($attrs as $key => $value) {
if (in_array($key, $whitelist) && (strlen(trim($value ?? '')) || $key === 'alt')) {
$manipulatedRecord = $manipulatedRecord->setAttribute($key, $value);
}
}

$markup = self::createImageTag($attrs);
// We're calling renderWith() with an explicit template in case someone wants to use a custom template
$markup = $manipulatedRecord->renderWith(self::class . '_Image');

// cache it for future reference
if ($fileFound) {
Expand All @@ -132,9 +140,11 @@ public static function handle_shortcode($args, $content, $parser, $shortcode, $e

/**
* Construct and return HTML image tag.
* @deprecated 2.3.0
*/
public static function createImageTag(array $attributes) : string
{
Deprecation::notice('2.3.0', 'Will be removed without equivalent functionality to replace it.');
$preparedAttributes = '';
foreach ($attributes as $attributeKey => $attributeValue) {
if (strlen($attributeValue ?? '') > 0 || $attributeKey === 'alt') {
Expand Down
2 changes: 1 addition & 1 deletion templates/DBFile_download.ss
Original file line number Diff line number Diff line change
@@ -1 +1 @@
<a href="$URL.ATT" title="$Title" <% if $Basename %>download="$Basename.ATT"<% else %>download<% end_if %>>$Title</a>
<% include SilverStripe\Assets\Storage\DBFile %>
2 changes: 1 addition & 1 deletion templates/DBFile_image.ss
Original file line number Diff line number Diff line change
@@ -1 +1 @@
<img $AttributesHTML />
<% include SilverStripe\Assets\Storage\DBFile_Image %>
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
<% include SilverStripe\Assets\Storage\DBFile_Image %>
1 change: 1 addition & 0 deletions templates/SilverStripe/Assets/Storage/DBFile.ss
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
<a href="$URL.ATT" title="$Title" <% if $Basename %>download="$Basename.ATT"<% else %>download<% end_if %>>$Title</a>
1 change: 1 addition & 0 deletions templates/SilverStripe/Assets/Storage/DBFile_Image.ss
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
<img $AttributesHTML />

0 comments on commit 9aa6bda

Please sign in to comment.