Skip to content

Commit 91f98da

Browse files
committed
feat: add readAccessible() file method
1 parent fdcfe65 commit 91f98da

File tree

4 files changed

+78
-8
lines changed

4 files changed

+78
-8
lines changed

.php-cs-fixer.php

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,10 @@
11
<?php
22

33
$finder = PhpCsFixer\Finder::create()
4+
->exclude('site/plugins')
5+
->exclude('storage')
46
->exclude('vendor')
7+
->exclude('node_modules')
58
->ignoreDotFiles(true)
69
->in(__DIR__);
710

@@ -10,6 +13,7 @@
1013
->setUsingCache(true)
1114
->setCacheFile(__DIR__ . '/.cache/.php-cs-fixer.cache')
1215
->setRules([
16+
'@PHP84Migration' => true,
1317
'@PSR12' => true,
1418
'align_multiline_comment' => ['comment_type' => 'phpdocs_only'],
1519
'array_indentation' => true,
@@ -26,7 +30,7 @@
2630
'explicit_string_variable' => true,
2731
'full_opening_tag' => true,
2832
'function_declaration' => ['closure_function_spacing' => 'one', 'closure_fn_spacing' => 'one'],
29-
'function_typehint_space' => true,
33+
'type_declaration_spaces' => true,
3034
'include' => true,
3135
'logical_operators' => true,
3236
'magic_constant_casing' => true,
@@ -35,8 +39,8 @@
3539
'modernize_types_casting' => true,
3640
'multiline_comment_opening_closing' => true,
3741
'native_function_casing' => true,
38-
'native_function_type_declaration_casing' => true,
39-
'new_with_braces' => true,
42+
'native_type_declaration_casing' => true,
43+
'new_with_parentheses' => true,
4044
'no_blank_lines_after_phpdoc' => true,
4145
'no_empty_comment' => true,
4246
'no_empty_phpdoc' => true,

composer.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
"name": "timnarr/kirby-helpers",
33
"description": "This plugin provides a collection of helper functions for Kirby",
44
"type": "kirby-plugin",
5-
"version": "0.3.5",
5+
"version": "0.4.0",
66
"license": "MIT",
77
"homepage": "https://github.com/timnarr/kirby-helpers",
88
"authors": [

index.php

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
'options' => [
77
'vite' => [
88
'manifestPath' => kirby()->root() . '/build/manifest.json',
9-
]
9+
],
1010
],
1111
'fieldMethods' => [
1212
'ensureLeft' => function (Field $field, string $prefix): string {
@@ -16,13 +16,18 @@
1616
return ensureRight($field->value, $suffix);
1717
},
1818
],
19+
'fileMethods' => [
20+
'readAccessible' => function (string $title = '', string $description = '', bool $isDecorative = false) {
21+
return readAccessible($this, $title, $description, $isDecorative);
22+
},
23+
],
1924
'pageMethods' => [
2025
'hasTranslations' => function (): bool {
2126
return !empty(getAvailableTranslations($this));
2227
},
2328
'getTranslations' => function (): array {
2429
return getAvailableTranslations($this);
25-
}
30+
},
2631
],
2732
'translations' => [
2833
'en' => [
@@ -40,6 +45,6 @@
4045
'link_label_external' => 'Externer Link: { url } (Öffnet neuen Tab)',
4146
'link_label_mail' => 'E-Mail schreiben an: { mail } (Öffnet neues Fenster Ihres E-Mail Programms)',
4247
'link_label_tel' => 'Telefonnummer anrufen: { tel } (Öffnet neues Fenster/Programm)',
43-
]
44-
]
48+
],
49+
],
4550
]);

src/misc.php

Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -177,3 +177,64 @@ function getAvailableTranslations(Page $page): array
177177
return $availableTranslations;
178178
}
179179
}
180+
181+
182+
/**
183+
* Reads the SVG content and adds accessibility attributes based on custom fields.
184+
*
185+
* @param File $file The file object representing the SVG.
186+
* @param string $title The title for the SVG (optional).
187+
* @param string $description The description for the SVG (optional).
188+
* @param bool $isDecorative Whether the SVG is decorative (optional).
189+
* @return string The modified SVG content with accessibility attributes.
190+
*/
191+
if (!function_exists('readAccessible')) {
192+
function readAccessible(File $file, string $title = '', string $description = '', bool $isDecorative = false): string
193+
{
194+
if ($file->extension() !== 'svg') {
195+
return $file->read();
196+
}
197+
198+
$svgContent = $file->read();
199+
200+
// Try to get values from custom fields if not provided
201+
if (empty($title) && $file->svgTitle()->isNotEmpty()) {
202+
$title = $file->svgTitle()->value();
203+
}
204+
205+
if (empty($description) && $file->svgDescription()->isNotEmpty()) {
206+
$description = $file->svgDescription()->value();
207+
}
208+
209+
// Check if marked as decorative in custom field
210+
if ($file->svgDecorative()->toBool()) {
211+
$isDecorative = true;
212+
}
213+
214+
if ($isDecorative) {
215+
$svgContent = str_replace(
216+
'<svg',
217+
'<svg aria-hidden="true"',
218+
$svgContent
219+
);
220+
} else {
221+
$uniqueId = uniqid('svg-');
222+
$finalTitle = $title ?: $file->alt()->or($file->name())->value();
223+
224+
// aria-labelledby="uniqueTitleID uniqueDescID" (use the title and desc ID’s) – both title and description are included in aria-labelledby because it has better screen-reader support than aria-describedby (see tip #4)
225+
$ariaAttributes = 'role="img" aria-labelledby="' . $uniqueId . '-title"';
226+
if ($description) {
227+
$ariaAttributes = 'role="img" aria-labelledby="' . $uniqueId . '-title ' . $uniqueId . '-desc"';
228+
}
229+
230+
$svgContent = str_replace('<svg', '<svg ' . $ariaAttributes, $svgContent);
231+
232+
$titleElement = '<title id="' . $uniqueId . '-title">' . Html::encode($finalTitle) . '</title>';
233+
$descElement = $description ? '<desc id="' . $uniqueId . '-desc">' . Html::encode($description) . '</desc>' : '';
234+
235+
$svgContent = preg_replace('/(<svg[^>]*>)/', '$1' . $titleElement . $descElement, $svgContent);
236+
}
237+
238+
return $svgContent;
239+
}
240+
}

0 commit comments

Comments
 (0)