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