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 May 2, 2024
1 parent ff5e8a7 commit 186d529
Show file tree
Hide file tree
Showing 8 changed files with 305 additions and 130 deletions.
24 changes: 19 additions & 5 deletions src/ImageManipulation.php
Original file line number Diff line number Diff line change
Expand Up @@ -1150,8 +1150,8 @@ protected function getDefaultAttributes(): array
$attributes = [];
if ($this->getIsImage()) {
$attributes = [
'width' => $this->getWidth(),
'height' => $this->getHeight(),
'width' => $this->getWidth() ?: null,
'height' => $this->getHeight() ?: null,
'alt' => $this->getTitle(),
'src' => $this->getURL(false)
];
Expand All @@ -1176,11 +1176,25 @@ public function getAttributes()

$attributes = array_merge($defaultAttributes, $this->attributes);

// We need to suppress the `loading="eager"` attribute after we merge the default attributes
if (isset($attributes['loading']) && $attributes['loading'] === 'eager') {
unset($attributes['loading']);
if (isset($attributes['loading'])) {
// Check if the dimensions for the image have been set
$dimensionsUnset = (
empty($attributes['width']) ||
empty($attributes['height']) ||
$attributes['width'] === 'auto' ||
$attributes['height'] === 'auto'
);

// We need to suppress the `loading="eager"` attribute after we merge the default attributes
// or if dimensions are not set
if ($attributes['loading'] === 'eager' || $dimensionsUnset) {
unset($attributes['loading']);
}
}




$this->extend('updateAttributes', $attributes);

return $attributes;
Expand Down
43 changes: 32 additions & 11 deletions src/Shortcodes/ImageShortcodeProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
use SilverStripe\Assets\Image;
use SilverStripe\Core\Flushable;
use SilverStripe\Core\Injector\Injector;
use SilverStripe\View\HTML;
use SilverStripe\Dev\Deprecation;
use SilverStripe\View\Parsers\ShortcodeHandler;
use SilverStripe\View\Parsers\ShortcodeParser;

Expand Down Expand Up @@ -75,25 +75,40 @@ 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;

// Resize the image if custom dimensions are provided
$hasCustomDimensions = ($width && $height);
if ($hasCustomDimensions && (($width != $record->getWidth()) || ($height != $record->getHeight()))) {
$resized = $record->ResizedImage($width, $height);
$resized = $manipulatedRecord->ResizedImage($width, $height);
// Make sure that the resized image actually returns an image
if ($resized) {
$src = $resized->getURL($grant);
$manipulatedRecord = $resized;
}
}

// If only one of width or height is provided, set the other to auto
if ($width && !$height) {
$args['height'] = 'auto';
}

if (!$width && $height) {
$args['width'] = 'auto';
}
}

// Determine whether loading="lazy" is set
$args = self::updateLoadingValue($args, $width, $height);
// Set lazy loading attribute
if (!empty($args['loading'])) {
$loading = strtolower($args['loading']);
unset($args['loading']);
$manipulatedRecord = $manipulatedRecord->LazyLoad($loading !== 'eager');
}

// Build the HTML tag
$attrs = array_merge(
Expand All @@ -102,7 +117,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 +127,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 +150,12 @@ 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 186d529

Please sign in to comment.