1 <?php
2
3 4 5 6 7
8 class extends DataObject {
9
10 static $db = array(
11 'Title' => 'Varchar(255)',
12 'OtherLink' => 'Text',
13 'MenuLinkID' => 'Int',
14 'CSS' => 'Varchar(255)',
15 );
16 static $has_one = array(
17 'Parent' => 'CustomMenuHolder',
18 'PageLink' => 'SiteTree',
19 'Icon' => 'Image',
20 'ParentItem' => 'CustomMenuItem',
21 );
22
23 static $has_many = array(
24 'SubPages' => 'CustomMenuItem'
25 );
26
27 static $summary_fields = array(
28 'Title', 'Link'
29 );
30
31 static $default_sort = 'SortOrder, ID';
32
33 function getCMSFields() {
34 $fields = parent::getCMSFields();
35 $fields->replaceField('OtherLink', new TextField('OtherLink', _t('CustomMenuItem.db_OtherLink', 'Other Link')));
36
37 $pageIDField = new TreeDropdownField('PageLinkID', _t('CustomMenuItem.has_one_PageLink', 'PageLink'), 'SiteTree');
38 if (class_exists('Subsite')) {
39 $currentSubsite = 0;
40 if ($this->ParentID && $this->Parent()->SubsiteID) {
41 $currentSubsite = $this->Parent()->SubsiteID;
42 }
43 Subsite::$force_subsite = $currentSubsite;
44 }
45 $fields->addFieldToTab('Root.Main', $pageIDField, 'OtherLink');
46
47 $fields->removeByName('SortOrder');
48 $fields->removeByName('MenuLinkID');
49 $fields->removeByName('ParentItemID');
50 $fields->removeByName('SubPages');
51
52 $fields->findOrMakeTab('Root.Design', _t('CustomMenuItem.tab_Design', 'Design'));
53 $fields->addFieldToTab('Root.Design', $fields->dataFieldByName('CSS'));
54 $i = $fields->dataFieldByName('Icon');
55 $i->setFolderName('CustomMenu');
56 $fields->addFieldToTab('Root.Design', $i);
57
58 return $fields;
59 }
60
61 function onBeforeDelete() {
62 parent::onBeforeDelete();
63 if ($this->SubPages() && $this->SubPages()->Count()) {
64 foreach($this->SubPages() as $item) {
65 $item->delete();
66 }
67 }
68 }
69
70 function () {
71 if ($this->MenuLinkID != 0) {
72 return TRUE;
73 }
74 return FALSE;
75 }
76
77 function () {
78 return $this->Title;
79 }
80
81 function Link() {
82 if ($this->PageLinkID == 0 && $this->MenuLinkID == 0) {
83 return $this->OtherLink;
84 }
85 if ($this->MenuLinkID != 0) {
86 return 'CustomMenu';
87 }
88 return $this->PageLink()->Link();
89 }
90
91 function Items() {
92 if ($this->MenuLinkID != 0) {
93 $menu = DataObject::get_by_id('CustomMenuHolder', $this->MenuLinkID);
94 return ($menu) ? $menu->Pages() : null;
95 }
96 return null;
97 }
98
99 function LinkOrSection() {
100 return ($this->PageLinkID) ? $this->PageLink()->LinkOrSection() : 'link';
101 }
102
103 function LinkOrCurrent() {
104 return ($this->PageLinkID) ? $this->PageLink()->LinkOrCurrent() : 'link';
105 }
106
107 function LinkingMode() {
108 return ($this->PageLinkID) ? $this->PageLink()->LinkingMode() : 'link';
109 }
110
111 }
112
[Raise a SilverStripe Framework issue/bug](https://github.com/silverstripe/silverstripe-framework/issues/new)
- [Raise a SilverStripe CMS issue/bug](https://github.com/silverstripe/silverstripe-cms/issues/new)
- Please use the
Silverstripe Forums to ask development related questions.
-