1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* @link https://github.com/DMGPage/yii2-materialize |
4
|
|
|
* @copyright Copyright (c) 2018 Dmitrijs Reinmanis |
5
|
|
|
* @license https://github.com/DMGPage/yii2-materialize/blob/master/LICENSE |
6
|
|
|
*/ |
7
|
|
|
|
8
|
|
|
namespace dmgpage\yii2materialize\widgets; |
9
|
|
|
|
10
|
|
|
/** |
11
|
|
|
* Breadcrumbs displays a list of links indicating the position of the current page in the whole site hierarchy. |
12
|
|
|
* |
13
|
|
|
* For example, breadcrumbs like "Home / Sample Post / Edit" means the user is viewing an edit page |
14
|
|
|
* for the "Sample Post". He can click on "Sample Post" to view that page, or he can click on "Home" |
15
|
|
|
* to return to the homepage. |
16
|
|
|
* |
17
|
|
|
* To use Breadcrumbs, you need to configure its [[links]] property, which specifies the links to be displayed. For example, |
18
|
|
|
* |
19
|
|
|
* ```php |
20
|
|
|
* // $this is the view object currently being used |
21
|
|
|
* echo Breadcrumbs::widget([ |
22
|
|
|
* 'itemTemplate' => "<li><i>{link}</i></li>\n", // template for all links |
23
|
|
|
* 'links' => [ |
24
|
|
|
* [ |
25
|
|
|
* 'label' => 'Post Category', |
26
|
|
|
* 'url' => ['post-category/view', 'id' => 10], |
27
|
|
|
* 'template' => "<li><b>{link}</b></li>\n", // template for this link only |
28
|
|
|
* ], |
29
|
|
|
* ['label' => 'Sample Post', 'url' => ['post/edit', 'id' => 1]], |
30
|
|
|
* 'Edit', |
31
|
|
|
* ], |
32
|
|
|
* ]); |
33
|
|
|
* ``` |
34
|
|
|
* |
35
|
|
|
* Because breadcrumbs usually appears in nearly every page of a website, you may consider placing it in a layout view. |
36
|
|
|
* You can use a view parameter (e.g. `$this->params['breadcrumbs']`) to configure the links in different |
37
|
|
|
* views. In the layout view, you assign this view parameter to the [[links]] property like the following: |
38
|
|
|
* |
39
|
|
|
* ```php |
40
|
|
|
* // $this is the view object currently being used |
41
|
|
|
* echo Breadcrumbs::widget([ |
42
|
|
|
* 'links' => isset($this->params['breadcrumbs']) ? $this->params['breadcrumbs'] : [], |
43
|
|
|
* ]); |
44
|
|
|
* ``` |
45
|
|
|
* |
46
|
|
|
* @author Qiang Xue <[email protected]> |
47
|
|
|
* @since 2.0 |
48
|
|
|
*/ |
49
|
|
|
class Breadcrumbs extends Widget |
50
|
|
|
{ |
51
|
|
|
/** |
52
|
|
|
* @var string the name of the breadcrumb container tag. |
53
|
|
|
*/ |
54
|
|
|
public $tag = 'ul'; |
55
|
|
|
|
56
|
|
|
/** |
57
|
|
|
* @var array the HTML attributes for the breadcrumb container tag. |
58
|
|
|
* @see \yii\helpers\Html::renderTagAttributes() for details on how attributes are being rendered. |
59
|
|
|
*/ |
60
|
|
|
public $options = ['class' => 'breadcrumb']; |
61
|
|
|
|
62
|
|
|
/** |
63
|
|
|
* @var bool whether to HTML-encode the link labels. |
64
|
|
|
*/ |
65
|
|
|
public $encodeLabels = true; |
66
|
|
|
|
67
|
|
|
/** |
68
|
|
|
* @var array the first hyperlink in the breadcrumbs (called home link). |
69
|
|
|
* Please refer to [[links]] on the format of the link. |
70
|
|
|
* If this property is not set, it will default to a link pointing to [[\yii\web\Application::homeUrl]] |
71
|
|
|
* with the label 'Home'. If this property is false, the home link will not be rendered. |
72
|
|
|
*/ |
73
|
|
|
public $homeLink; |
74
|
|
|
|
75
|
|
|
/** |
76
|
|
|
* @var array list of links to appear in the breadcrumbs. If this property is empty, |
77
|
|
|
* the widget will not render anything. Each array element represents a single link in the breadcrumbs |
78
|
|
|
* with the following structure: |
79
|
|
|
* |
80
|
|
|
* ```php |
81
|
|
|
* [ |
82
|
|
|
* 'label' => 'label of the link', // required |
83
|
|
|
* 'url' => 'url of the link', // optional, will be processed by Url::to() |
84
|
|
|
* 'template' => 'own template of the item', // optional, if not set $this->itemTemplate will be used |
85
|
|
|
* ] |
86
|
|
|
* ``` |
87
|
|
|
* |
88
|
|
|
* If a link is active, you only need to specify its "label", and instead of writing `['label' => $label]`, |
89
|
|
|
* you may simply use `$label`. |
90
|
|
|
* |
91
|
|
|
* Since version 2.0.1, any additional array elements for each link will be treated as the HTML attributes |
92
|
|
|
* for the hyperlink tag. For example, the following link specification will generate a hyperlink |
93
|
|
|
* with CSS class `external`: |
94
|
|
|
* |
95
|
|
|
* ```php |
96
|
|
|
* [ |
97
|
|
|
* 'label' => 'demo', |
98
|
|
|
* 'url' => 'http://example.com', |
99
|
|
|
* 'class' => 'external', |
100
|
|
|
* ] |
101
|
|
|
* ``` |
102
|
|
|
* |
103
|
|
|
* Since version 2.0.3 each individual link can override global [[encodeLabels]] param like the following: |
104
|
|
|
* |
105
|
|
|
* ```php |
106
|
|
|
* [ |
107
|
|
|
* 'label' => '<strong>Hello!</strong>', |
108
|
|
|
* 'encode' => false, |
109
|
|
|
* ] |
110
|
|
|
* ``` |
111
|
|
|
*/ |
112
|
|
|
public $links = []; |
113
|
|
|
|
114
|
|
|
/** |
115
|
|
|
* @var string the template used to render each inactive item in the breadcrumbs. The token `{link}` |
116
|
|
|
* will be replaced with the actual HTML link for each inactive item. |
117
|
|
|
*/ |
118
|
|
|
public $itemTemplate = "<li>{link}</li>\n"; |
119
|
|
|
|
120
|
|
|
/** |
121
|
|
|
* @var string the template used to render each active item in the breadcrumbs. The token `{link}` |
122
|
|
|
* will be replaced with the actual HTML link for each active item. |
123
|
|
|
*/ |
124
|
|
|
public $activeItemTemplate = "<li class=\"active\">{link}</li>\n"; |
125
|
|
|
|
126
|
|
|
/** |
127
|
|
|
* Renders the widget. |
128
|
|
|
*/ |
129
|
|
|
public function run() |
130
|
|
|
{ |
131
|
|
|
if (empty($this->links)) { |
132
|
|
|
return; |
133
|
|
|
} |
134
|
|
|
$links = []; |
135
|
|
|
if ($this->homeLink === null) { |
136
|
|
|
$links[] = $this->renderItem([ |
137
|
|
|
'label' => Yii::t('yii', 'Home'), |
|
|
|
|
138
|
|
|
'url' => Yii::$app->homeUrl, |
139
|
|
|
], $this->itemTemplate); |
140
|
|
|
} elseif ($this->homeLink !== false) { |
|
|
|
|
141
|
|
|
$links[] = $this->renderItem($this->homeLink, $this->itemTemplate); |
142
|
|
|
} |
143
|
|
|
foreach ($this->links as $link) { |
144
|
|
|
if (!is_array($link)) { |
145
|
|
|
$link = ['label' => $link]; |
146
|
|
|
} |
147
|
|
|
$links[] = $this->renderItem($link, isset($link['url']) ? $this->itemTemplate : $this->activeItemTemplate); |
148
|
|
|
} |
149
|
|
|
echo Html::tag($this->tag, implode('', $links), $this->options); |
|
|
|
|
150
|
|
|
} |
151
|
|
|
|
152
|
|
|
/** |
153
|
|
|
* Renders a single breadcrumb item. |
154
|
|
|
* @param array $link the link to be rendered. It must contain the "label" element. The "url" element is optional. |
155
|
|
|
* @param string $template the template to be used to rendered the link. The token "{link}" will be replaced by the link. |
156
|
|
|
* @return string the rendering result |
157
|
|
|
* @throws InvalidConfigException if `$link` does not have "label" element. |
158
|
|
|
*/ |
159
|
|
|
protected function renderItem($link, $template) |
160
|
|
|
{ |
161
|
|
|
$encodeLabel = ArrayHelper::remove($link, 'encode', $this->encodeLabels); |
|
|
|
|
162
|
|
|
if (array_key_exists('label', $link)) { |
163
|
|
|
$label = $encodeLabel ? Html::encode($link['label']) : $link['label']; |
164
|
|
|
} else { |
165
|
|
|
throw new InvalidConfigException('The "label" element is required for each link.'); |
|
|
|
|
166
|
|
|
} |
167
|
|
|
if (isset($link['template'])) { |
168
|
|
|
$template = $link['template']; |
169
|
|
|
} |
170
|
|
|
if (isset($link['url'])) { |
171
|
|
|
$options = $link; |
172
|
|
|
unset($options['template'], $options['label'], $options['url']); |
173
|
|
|
$link = Html::a($label, $link['url'], $options); |
174
|
|
|
} else { |
175
|
|
|
$link = $label; |
176
|
|
|
} |
177
|
|
|
return strtr($template, ['{link}' => $link]); |
178
|
|
|
} |
179
|
|
|
} |
180
|
|
|
|