-
Notifications
You must be signed in to change notification settings - Fork 0
/
extend.php
40 lines (31 loc) · 1.22 KB
/
extend.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
<?php
namespace ClarkWinkelmann\DefaultImageAlt;
use Flarum\Extend;
use Flarum\Locale\Translator;
use Flarum\Post\Post;
use Illuminate\Support\Arr;
use s9e\TextFormatter\Utils;
return [
new Extend\Locales(__DIR__ . '/locale'),
(new Extend\Formatter())
->render(function ($renderer, $context, $xml) {
return Utils::replaceAttributes($xml, 'IMG', function (array $attributes) use ($context): array {
// If there's already an alt text, skip our code
if (Arr::get($attributes, 'alt')) {
return $attributes;
}
/**
* @var $translator Translator
*/
$translator = resolve(Translator::class);
$key = 'clarkwinkelmann-default-image-alt.api.alt-text-generic';
$params = [];
if ($context instanceof Post && $context->user) {
$key = 'clarkwinkelmann-default-image-alt.api.alt-text-author';
$params['username'] = $context->user->display_name;
}
$attributes['alt'] = $translator->trans($key, $params);
return $attributes;
});
}),
];