diff --git a/src/Image/Image.php b/src/Image/Image.php index 4e7b0de0b8..f012a1e30c 100644 --- a/src/Image/Image.php +++ b/src/Image/Image.php @@ -2,6 +2,7 @@ namespace Kirby\Image; +use Kirby\Cms\FileVersion; use Kirby\Content\Content; use Kirby\Exception\LogicException; use Kirby\Filesystem\File; @@ -115,15 +116,20 @@ public function height(): int */ public function html(array $attr = []): string { + $model = match (true) { + $this->model instanceof FileVersion => $this->model->original(), + default => $this->model + }; + // if no alt text explicitly provided, // try to infer from model content file if ( - $this->model !== null && - method_exists($this->model, 'content') === true && - $this->model->content() instanceof Content && - $this->model->content()->get('alt')->isNotEmpty() === true + $model !== null && + method_exists($model, 'content') === true && + $model->content() instanceof Content && + $model->content()->get('alt')->isNotEmpty() === true ) { - $attr['alt'] ??= $this->model->content()->get('alt')->value(); + $attr['alt'] ??= $model->content()->get('alt')->value(); } if ($url = $this->url()) { diff --git a/tests/Cms/Files/FileVersionTest.php b/tests/Cms/Files/FileVersionTest.php index 2cc34b869f..63c688e5c6 100644 --- a/tests/Cms/Files/FileVersionTest.php +++ b/tests/Cms/Files/FileVersionTest.php @@ -107,7 +107,11 @@ public function testToString() 'slug' => 'test' ]); - $original = new File(['filename' => 'test.jpg', 'parent' => $page]); + $original = new File([ + 'filename' => 'test.jpg', + 'parent' => $page, + 'content' => ['alt' => 'Test text'] + ]); $version = new FileVersion([ 'original' => $original, 'root' => static::FIXTURES . '/test.txt', @@ -122,6 +126,6 @@ public function testToString() 'url' => $url = 'https://assets.getkirby.com/test-200x200.jpg', ]); - $this->assertSame('', (string)$version); + $this->assertSame('Test text', (string)$version); } }