/
CMSMenuItem.php
122 lines (104 loc) · 2.72 KB
/
CMSMenuItem.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
<?php
namespace SilverStripe\Admin;
use SilverStripe\Core\Convert;
use SilverStripe\Core\Injector\Injectable;
use SilverStripe\ORM\FieldType\DBField;
use SilverStripe\ORM\FieldType\DBHTMLText;
/**
* A simple CMS menu item.
*
* Items can be added to the menu through custom {@link LeftAndMainExtension}
* classes and {@link CMSMenu}.
*
* @see CMSMenu
*/
class CMSMenuItem
{
use Injectable;
/**
* The (translated) menu title
* @var string $title
*/
public $title;
/**
* Relative URL
* @var string $url
*/
public $url;
/**
* Parent controller class name
* @var string $controller
*/
public $controller;
/**
* Menu priority (sort order)
* @var integer $priority
*/
public $priority;
/**
* Attributes for the link. For instance, custom data attributes or standard
* HTML anchor properties.
*
* @var string
*/
protected $attributes = [];
/**
* @var string
*/
public $iconClass;
/**
* Create a new CMS Menu Item
*
* @param string $title
* @param string $url
* @param string $controller Controller class name
* @param integer $priority The sort priority of the item
* @param string $iconClass
*/
public function __construct($title, $url, $controller = null, $priority = -1, $iconClass = null)
{
$this->title = $title;
$this->url = $url;
$this->controller = $controller;
$this->priority = $priority;
$this->iconClass = $iconClass;
}
/**
* @param array $attributes
*/
public function setAttributes($attributes)
{
$this->attributes = $attributes;
}
/**
* @param array $attrs
* @return DBHTMLText
*/
public function getAttributesHTML($attrs = null)
{
$excludeKeys = (is_string($attrs)) ? func_get_args() : null;
if (!$attrs || is_string($attrs)) {
$attrs = $this->attributes;
}
// Remove empty or excluded values
foreach ($attrs as $key => $value) {
if (($excludeKeys && in_array($key, $excludeKeys ?? []))
|| (!$value && $value !== 0 && $value !== '0')
) {
unset($attrs[$key]);
continue;
}
}
// Create markkup
$parts = [];
foreach ($attrs as $name => $value) {
if ($value === true) {
$value = $name;
}
$parts[] = sprintf('%s="%s"', Convert::raw2att($name), Convert::raw2att($value));
}
/** @var DBHTMLText $fragment */
$fragment = DBField::create_field('HTMLFragment', implode(' ', $parts));
return $fragment;
}
}