1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
declare(strict_types=1); |
4
|
|
|
|
5
|
|
|
namespace Yiisoft\Yii\Widgets; |
6
|
|
|
|
7
|
|
|
use Yiisoft\Arrays\ArrayHelper; |
8
|
|
|
use Yiisoft\Html\Html; |
9
|
|
|
use Yiisoft\Widget\Widget; |
10
|
|
|
|
11
|
|
|
/** |
12
|
|
|
* Breadcrumbs displays a list of links indicating the position of the current page in the whole site hierarchy. |
13
|
|
|
* |
14
|
|
|
* For example, breadcrumbs like "Home / Sample Post / Edit" means the user is viewing an edit page for the |
15
|
|
|
* "Sample Post". He can click on "Sample Post" to view that page, or he can click on "Home" to return to the homepage. |
16
|
|
|
* |
17
|
|
|
* To use Breadcrumbs, you need to configure its {@see links} property, which specifies the links to be displayed. For |
18
|
|
|
* example, |
19
|
|
|
* |
20
|
|
|
* ```php |
21
|
|
|
* // $this is the view object currently being used |
22
|
|
|
* echo Breadcrumbs::widget() |
23
|
|
|
* -> itemTemplate() => "<li><i>{link}</i></li>\n", // template for all links |
24
|
|
|
* -> links() => [ |
25
|
|
|
* [ |
26
|
|
|
* 'label' => 'Post Category', |
27
|
|
|
* 'url' => 'post-category/view?id=10', |
28
|
|
|
* 'template' => "<li><b>{link}</b></li>\n", // template for this link only |
29
|
|
|
* ], |
30
|
|
|
* ['label' => 'Sample Post', 'url' => 'post/edit?id=1', |
31
|
|
|
* 'Edit', |
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 views. In the |
37
|
|
|
* layout view, you assign this view parameter to the {@see 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
|
|
|
class Breadcrumbs extends Widget |
46
|
|
|
{ |
47
|
|
|
/** |
48
|
|
|
* @var string the name of the breadcrumb container tag. |
49
|
|
|
*/ |
50
|
|
|
private string $tag = 'ul'; |
51
|
|
|
|
52
|
|
|
/** |
53
|
|
|
* @var array the HTML attributes for the breadcrumb container tag. |
54
|
|
|
* |
55
|
|
|
* {@see \Yiisoft\Html\Html::renderTagAttributes()} for details on how attributes are being rendered. |
56
|
|
|
*/ |
57
|
|
|
private array $options = ['class' => 'breadcrumb']; |
58
|
|
|
|
59
|
|
|
/** |
60
|
|
|
* @var bool whether to HTML-encode the link labels. |
61
|
|
|
*/ |
62
|
|
|
private bool $encodeLabels = true; |
63
|
|
|
|
64
|
|
|
/** |
65
|
|
|
* @var bool the first hyperlink in the breadcrumbs (called home link). If this property is true, it will default |
66
|
|
|
* to a link pointing to HomeUrl '\' with the label 'Home'. If this property is false, the home link will |
67
|
|
|
* not be rendered. |
68
|
|
|
*/ |
69
|
|
|
private bool $homeLink = true; |
70
|
|
|
|
71
|
|
|
/** |
72
|
|
|
* @var array $homeUrlLink |
73
|
|
|
*/ |
74
|
|
|
private array $homeUrlLink = []; |
75
|
|
|
|
76
|
|
|
/** |
77
|
|
|
* @var array list of links to appear in the breadcrumbs. If this property is empty, the widget will not render |
78
|
|
|
* anything. Each array element represents a single link in the breadcrumbs with the following structure: |
79
|
|
|
* |
80
|
|
|
* ```php |
81
|
|
|
* [ |
82
|
|
|
* 'label' => 'label of the link', // required |
83
|
|
|
* 'url' => 'url of the link', // optional |
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]`, you may |
89
|
|
|
* simply use `$label`. |
90
|
|
|
* |
91
|
|
|
* Additional array elements for each link will be treated as the HTML attributes for the hyperlink tag. |
92
|
|
|
* For example, the following link specification will generate a hyperlink with CSS class `external`: |
93
|
|
|
* |
94
|
|
|
* ```php |
95
|
|
|
* [ |
96
|
|
|
* 'label' => 'demo', |
97
|
|
|
* 'url' => 'http://example.com', |
98
|
|
|
* 'class' => 'external', |
99
|
|
|
* ] |
100
|
|
|
* ``` |
101
|
|
|
* |
102
|
|
|
* Each individual link can override global {@see encodeLabels} param like the following: |
103
|
|
|
* |
104
|
|
|
* ```php |
105
|
|
|
* [ |
106
|
|
|
* 'label' => '<strong>Hello!</strong>', |
107
|
|
|
* 'encode' => false, |
108
|
|
|
* ] |
109
|
|
|
* ``` |
110
|
|
|
*/ |
111
|
|
|
private array $links = []; |
112
|
|
|
|
113
|
|
|
/** |
114
|
|
|
* @var string the template used to render each inactive item in the breadcrumbs. The token `{link}` will be |
115
|
|
|
* replaced with the actual HTML link for each inactive item. |
116
|
|
|
*/ |
117
|
|
|
private string $itemTemplate = "<li>{link}</li>\n"; |
118
|
|
|
|
119
|
|
|
/** |
120
|
|
|
* @var string the template used to render each active item in the breadcrumbs. The token `{link}` will be replaced |
121
|
|
|
* with the actual HTML link for each active item. |
122
|
|
|
*/ |
123
|
|
|
private string $activeItemTemplate = "<li class=\"active\">{link}</li>\n"; |
124
|
|
|
|
125
|
|
|
/** |
126
|
|
|
* Renders the widget. |
127
|
|
|
* |
128
|
|
|
* @return string the result of widget execution to be outputted. |
129
|
|
|
*/ |
130
|
8 |
|
public function run(): string |
131
|
|
|
{ |
132
|
8 |
|
if (empty($this->links)) { |
133
|
1 |
|
return ''; |
134
|
|
|
} |
135
|
|
|
|
136
|
7 |
|
$links = []; |
137
|
|
|
|
138
|
7 |
|
if ($this->homeLink === true) { |
139
|
2 |
|
$links[] = $this->renderItem([ |
140
|
2 |
|
'label' => 'Home', |
141
|
|
|
'url' => '/', |
142
|
2 |
|
], $this->itemTemplate); |
143
|
5 |
|
} elseif (!empty($this->homeUrlLink)) { |
144
|
1 |
|
$links[] = $this->renderItem($this->homeUrlLink, $this->itemTemplate); |
145
|
|
|
} |
146
|
|
|
|
147
|
7 |
|
foreach ($this->links as $link) { |
148
|
7 |
|
if (!\is_array($link)) { |
149
|
7 |
|
$link = ['label' => $link]; |
150
|
|
|
} |
151
|
|
|
|
152
|
7 |
|
if (!empty($link)) { |
153
|
7 |
|
$links[] = $this->renderItem( |
154
|
7 |
|
$link, |
155
|
7 |
|
isset($link['url']) ? $this->itemTemplate : $this->activeItemTemplate |
156
|
|
|
); |
157
|
|
|
} |
158
|
|
|
} |
159
|
|
|
|
160
|
7 |
|
return Html::tag(!empty($this->tag) ? $this->tag : false, implode('', $links), $this->options); |
161
|
|
|
} |
162
|
|
|
|
163
|
|
|
/** |
164
|
|
|
* Renders a single breadcrumb item. |
165
|
|
|
* |
166
|
|
|
* @param array $link the link to be rendered. It must contain the "label" element. The "url" element is optional. |
167
|
|
|
* @param string $template the template to be used to rendered the link. The token "{link}" will be replaced by the |
168
|
|
|
* link. |
169
|
|
|
* |
170
|
|
|
* @throws \InvalidArgumentException if `$link` does not have "label" element. |
171
|
|
|
* |
172
|
|
|
* @return string the rendering result |
173
|
|
|
*/ |
174
|
7 |
|
protected function renderItem(array $link, string $template): string |
175
|
|
|
{ |
176
|
7 |
|
$encodeLabel = ArrayHelper::remove($link, 'encode', $this->encodeLabels); |
177
|
7 |
|
$label = $encodeLabel ? Html::encode($link['label']) : $link['label']; |
178
|
|
|
|
179
|
7 |
|
if (isset($link['template'])) { |
180
|
|
|
$template = $link['template']; |
181
|
|
|
} |
182
|
|
|
|
183
|
7 |
|
if (isset($link['url'])) { |
184
|
2 |
|
$options = $link; |
185
|
2 |
|
unset($options['template'], $options['label'], $options['url']); |
186
|
2 |
|
$link = Html::a($label, $link['url'], $options); |
187
|
|
|
} else { |
188
|
7 |
|
$link = $label; |
189
|
|
|
} |
190
|
|
|
|
191
|
7 |
|
return strtr($template, ['{link}' => $link]); |
192
|
|
|
} |
193
|
|
|
|
194
|
|
|
/** |
195
|
|
|
* {@see $tag} |
196
|
|
|
* |
197
|
|
|
* @param string $value |
198
|
|
|
* |
199
|
|
|
* @return Breadcrumbs |
200
|
|
|
*/ |
201
|
3 |
|
public function tag(string $value): Breadcrumbs |
202
|
|
|
{ |
203
|
3 |
|
$this->tag = $value; |
204
|
|
|
|
205
|
3 |
|
return $this; |
206
|
|
|
} |
207
|
|
|
|
208
|
|
|
/** |
209
|
|
|
* {@see $options} |
210
|
|
|
* |
211
|
|
|
* @param array $value |
212
|
|
|
* |
213
|
|
|
* @return Breadcrumbs |
214
|
|
|
*/ |
215
|
4 |
|
public function options(array $value): Breadcrumbs |
216
|
|
|
{ |
217
|
4 |
|
$this->options = $value; |
218
|
|
|
|
219
|
4 |
|
return $this; |
220
|
|
|
} |
221
|
|
|
|
222
|
|
|
/** |
223
|
|
|
* {@see $encodeLabel} |
224
|
|
|
* |
225
|
|
|
* @param bool $value |
226
|
|
|
* |
227
|
|
|
* @return Breadcrumbs |
228
|
|
|
*/ |
229
|
1 |
|
public function encodeLabels(bool $value): Breadcrumbs |
230
|
|
|
{ |
231
|
1 |
|
$this->encodeLabels = $value; |
232
|
|
|
|
233
|
1 |
|
return $this; |
234
|
|
|
} |
235
|
|
|
|
236
|
|
|
/** |
237
|
|
|
* {@see $homeLink} |
238
|
|
|
* |
239
|
|
|
* @param bool $value |
240
|
|
|
* |
241
|
|
|
* @return Breadcrumbs |
242
|
|
|
*/ |
243
|
7 |
|
public function homeLink(bool $value): Breadcrumbs |
244
|
|
|
{ |
245
|
7 |
|
$this->homeLink = $value; |
246
|
|
|
|
247
|
7 |
|
return $this; |
248
|
|
|
} |
249
|
|
|
|
250
|
|
|
/** |
251
|
|
|
* {@see $homeUrlLink} |
252
|
|
|
* |
253
|
|
|
* @param array $value |
254
|
|
|
* |
255
|
|
|
* @return Breadcrumbs |
256
|
|
|
*/ |
257
|
1 |
|
public function homeUrlLink(array $value): Breadcrumbs |
258
|
|
|
{ |
259
|
1 |
|
$this->homeUrlLink = $value; |
260
|
|
|
|
261
|
1 |
|
return $this; |
262
|
|
|
} |
263
|
|
|
|
264
|
|
|
/** |
265
|
|
|
* {@see $links} |
266
|
|
|
* |
267
|
|
|
* @param array $value |
268
|
|
|
* |
269
|
|
|
* @return Breadcrumbs |
270
|
|
|
*/ |
271
|
8 |
|
public function links(array $value): Breadcrumbs |
272
|
|
|
{ |
273
|
8 |
|
if (!\array_key_exists('label', $value)) { |
274
|
1 |
|
throw new \InvalidArgumentException('The "label" element is required for each link.'); |
275
|
|
|
} |
276
|
|
|
|
277
|
7 |
|
$this->links = $value; |
278
|
|
|
|
279
|
7 |
|
return $this; |
280
|
|
|
} |
281
|
|
|
|
282
|
|
|
/** |
283
|
|
|
* {@see $itemTemplate} |
284
|
|
|
* |
285
|
|
|
* @param string $value |
286
|
|
|
* |
287
|
|
|
* @return Breadcrumbs |
288
|
|
|
*/ |
289
|
1 |
|
public function itemTemplate(string $value): Breadcrumbs |
290
|
|
|
{ |
291
|
1 |
|
$this->itemTemplate = $value; |
292
|
|
|
|
293
|
1 |
|
return $this; |
294
|
|
|
} |
295
|
|
|
|
296
|
|
|
/** |
297
|
|
|
* {@see $activeItemTemplate} |
298
|
|
|
* |
299
|
|
|
* @param string $value |
300
|
|
|
* |
301
|
|
|
* @return Breadcrumbs |
302
|
|
|
*/ |
303
|
3 |
|
public function activeItemTemplate(string $value): Breadcrumbs |
304
|
|
|
{ |
305
|
3 |
|
$this->activeItemTemplate = $value; |
306
|
|
|
|
307
|
3 |
|
return $this; |
308
|
|
|
} |
309
|
|
|
} |
310
|
|
|
|