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

Commit d504ab1

Browse files
author
Matthew Hailwood
committed
Fixing something...
1 parent 6dd16de commit d504ab1

File tree

1 file changed

+56
-0
lines changed

1 file changed

+56
-0
lines changed

code/LinkExtension.php

Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
<?php
2+
3+
/**
4+
* Class LinkExtension
5+
*
6+
* @property Link $owner
7+
*/
8+
class LinkExtension extends DataExtension {
9+
10+
private static $db = [
11+
'LinkSeoText' => 'Varchar(255)',
12+
'ForceDownload' => 'Boolean'
13+
];
14+
15+
private static $has_one = [
16+
'OwningPage' => 'Page'
17+
];
18+
19+
public function updateCMSFields(FieldList $fields) {
20+
21+
$fields->dataFieldByName('Title')->setTitle(_t('Linkable.TITLE', 'Link Text'));
22+
23+
$fields->dataFieldByName('OpenInNewWindow')->hideIf("Type")->isEqualTo("File")->end();
24+
25+
/** @var TextField $seoText */
26+
$fields->addFieldToTab('Root.Main', $seoText = TextField::create('LinkSeoText', _t('Linkable.SEOTEXT', 'SEO Title Attribute')), 'Type');
27+
$seoText->setDescription('Optional. Will be equal to Link Text if left blank');
28+
29+
$fields->addFieldToTab('Root.Main', $forceDownload = CheckboxField::create('ForceDownload',
30+
_t('Linkable.FORCEDOWNLOAD', 'Force user to download file')));
31+
$forceDownload->displayIf('Type')->isEqualTo("File")->end();
32+
33+
$fields->removeByName('OwningPageID');
34+
}
35+
36+
public function getDownloadAttribute() {
37+
/** @var File $component */
38+
if ($this->owner->Type === 'File' && $component = $this->owner->getComponent($this->owner->Type)) {
39+
if ($component->exists()) {
40+
return ' download="' . $component->Name . '" ';
41+
}
42+
43+
}
44+
45+
return null;
46+
}
47+
48+
public function getTitleAttribute() {
49+
return ' title="' . ($this->owner->LinkSeoText ?: $this->owner->Title) . '" ';
50+
}
51+
52+
public function getAttributes(){
53+
54+
return join(' ', [$this->owner->getTargetAttr(), $this->getDownloadAttribute(), $this->getTitleAttribute()]);
55+
}
56+
}

0 commit comments

Comments
 (0)