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