Skip to content
This repository was archived by the owner on Oct 13, 2025. It is now read-only.

Commit 92ad781

Browse files
author
Matthew Hailwood
committed
Image Alt Text
1 parent 3b84827 commit 92ad781

File tree

2 files changed

+61
-0
lines changed

2 files changed

+61
-0
lines changed

_config/config.yml

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,10 @@ SiteTree:
99
extensions:
1010
- ExtraPageFieldsExtension
1111

12+
Image:
13+
extensions:
14+
- ImageExtension
15+
1216
#SiteTree:
1317
# extensions:
1418
# - FooterMenuExtension

code/ImageExtension.php

Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
<?php
2+
3+
/**
4+
* Class ImageExtension
5+
*
6+
* @property Image $owner
7+
*/
8+
class ImageExtension extends DataExtension {
9+
10+
private static $db = [
11+
'AltText' => 'Varchar(255)',
12+
];
13+
14+
private static $has_one = [
15+
'OwningPage' => 'Page'
16+
];
17+
18+
public function updateCMSFields(FieldList $fields) {
19+
20+
Requirements::customCSS(
21+
<<<CSS
22+
form.small .field input.text,
23+
form.small .field textarea,
24+
form.small .field select,
25+
form.small .field .TreeDropdownField,
26+
.field.small input.text,
27+
.field.small textarea,
28+
.field.small select,
29+
.field.small .TreeDropdownField {
30+
width: 100%;
31+
}
32+
CSS
33+
);
34+
35+
$fields->dataFieldByName('Title')->setTitle(_t('Linkable.TITLE', 'Title Attribute'))
36+
->setDescription('Describe the image to humans');
37+
38+
/** @var TextField $altText */
39+
$fields->addFieldToTab('Root.Main', $altText = TextField::create('AltText', _t('Linkable.SEOTEXT', 'Alt Attribute')), 'Name');
40+
$altText->setDescription('Describe the image to google');
41+
42+
$fields->removeByName('OwningPageID');
43+
}
44+
45+
public function getDownloadAttribute() {
46+
/** @var File $component */
47+
if ($this->owner->Type === 'File' && $component = $this->owner->getComponent($this->owner->Type)) {
48+
if ($component->exists()) {
49+
return ' download="' . $component->Name . '" ';
50+
}
51+
52+
}
53+
54+
return null;
55+
}
56+
57+
}

0 commit comments

Comments
 (0)