Skip to content

Commit

Permalink
Fix alt attribute for FileVersion #6852
Browse files Browse the repository at this point in the history
  • Loading branch information
afbora committed Dec 9, 2024
1 parent 61d3b90 commit 9a69f51
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 7 deletions.
16 changes: 11 additions & 5 deletions src/Image/Image.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

namespace Kirby\Image;

use Kirby\Cms\FileVersion;
use Kirby\Content\Content;
use Kirby\Exception\LogicException;
use Kirby\Filesystem\File;
Expand Down Expand Up @@ -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()) {
Expand Down
8 changes: 6 additions & 2 deletions tests/Cms/Files/FileVersionTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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',
Expand All @@ -122,6 +126,6 @@ public function testToString()
'url' => $url = 'https://assets.getkirby.com/test-200x200.jpg',
]);

$this->assertSame('<img alt="" src="' . $url . '">', (string)$version);
$this->assertSame('<img alt="Test text" src="' . $url . '">', (string)$version);
}
}

0 comments on commit 9a69f51

Please sign in to comment.