|
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
|
|
|
private string $tag = 'ul'; |
|
53
|
|
|
private array $options = ['class' => 'breadcrumb']; |
|
54
|
|
|
private bool $encodeLabels = true; |
|
55
|
|
|
private bool $homeLink = true; |
|
56
|
|
|
private array $homeUrlLink = []; |
|
57
|
|
|
private array $links = []; |
|
58
|
|
|
private string $itemTemplate = "<li>{link}</li>\n"; |
|
59
|
|
|
private string $activeItemTemplate = "<li class=\"active\">{link}</li>\n"; |
|
60
|
|
|
|
|
61
|
|
|
/** |
|
62
|
|
|
* Renders the widget. |
|
63
|
|
|
* |
|
64
|
|
|
* @throws JsonException |
|
65
|
|
|
* |
|
66
|
|
|
* @return string the result of widget execution to be outputted. |
|
67
|
|
|
*/ |
|
68
|
8 |
|
public function run(): string |
|
69
|
|
|
{ |
|
70
|
8 |
|
if (empty($this->links)) { |
|
71
|
1 |
|
return ''; |
|
72
|
|
|
} |
|
73
|
|
|
|
|
74
|
7 |
|
$links = []; |
|
75
|
|
|
|
|
76
|
7 |
|
if ($this->homeLink === true) { |
|
77
|
2 |
|
$links[] = $this->renderItem([ |
|
78
|
2 |
|
'label' => 'Home', |
|
79
|
|
|
'url' => '/', |
|
80
|
2 |
|
], $this->itemTemplate); |
|
81
|
5 |
|
} elseif (!empty($this->homeUrlLink)) { |
|
82
|
1 |
|
$links[] = $this->renderItem($this->homeUrlLink, $this->itemTemplate); |
|
83
|
|
|
} |
|
84
|
|
|
|
|
85
|
7 |
|
foreach ($this->links as $link) { |
|
86
|
7 |
|
if (!is_array($link)) { |
|
87
|
7 |
|
$link = ['label' => $link]; |
|
88
|
|
|
} |
|
89
|
|
|
|
|
90
|
7 |
|
if (!empty($link)) { |
|
91
|
7 |
|
$links[] = $this->renderItem( |
|
92
|
7 |
|
$link, |
|
93
|
7 |
|
isset($link['url']) ? $this->itemTemplate : $this->activeItemTemplate |
|
94
|
|
|
); |
|
95
|
|
|
} |
|
96
|
|
|
} |
|
97
|
|
|
|
|
98
|
7 |
|
return Html::tag(!empty($this->tag) ? $this->tag : false, implode('', $links), $this->options); |
|
99
|
|
|
} |
|
100
|
|
|
|
|
101
|
|
|
/** |
|
102
|
|
|
* Renders a single breadcrumb item. |
|
103
|
|
|
* |
|
104
|
|
|
* @param array $link the link to be rendered. It must contain the "label" element. The "url" element is optional. |
|
105
|
|
|
* @param string $template the template to be used to rendered the link. The token "{link}" will be replaced by the |
|
106
|
|
|
* link. |
|
107
|
|
|
* |
|
108
|
|
|
* @throws JsonException|InvalidArgumentException if `$link` does not have "label" element. |
|
109
|
|
|
* |
|
110
|
|
|
* @return string the rendering result |
|
111
|
|
|
*/ |
|
112
|
7 |
|
protected function renderItem(array $link, string $template): string |
|
113
|
|
|
{ |
|
114
|
7 |
|
$encodeLabel = ArrayHelper::remove($link, 'encode', $this->encodeLabels); |
|
115
|
7 |
|
$label = $encodeLabel ? Html::encode($link['label']) : $link['label']; |
|
116
|
|
|
|
|
117
|
7 |
|
if (isset($link['template'])) { |
|
118
|
|
|
$template = $link['template']; |
|
119
|
|
|
} |
|
120
|
|
|
|
|
121
|
7 |
|
if (isset($link['url'])) { |
|
122
|
2 |
|
$options = $link; |
|
123
|
2 |
|
unset($options['template'], $options['label'], $options['url']); |
|
124
|
2 |
|
$link = Html::a($label, $link['url'], $options); |
|
125
|
|
|
} else { |
|
126
|
7 |
|
$link = $label; |
|
127
|
|
|
} |
|
128
|
|
|
|
|
129
|
7 |
|
return strtr($template, ['{link}' => $link]); |
|
130
|
|
|
} |
|
131
|
|
|
|
|
132
|
3 |
|
public function tag(string $value): self |
|
133
|
|
|
{ |
|
134
|
3 |
|
$this->tag = $value; |
|
135
|
|
|
|
|
136
|
3 |
|
return $this; |
|
137
|
|
|
} |
|
138
|
|
|
|
|
139
|
|
|
/** |
|
140
|
|
|
* @param array $value the HTML attributes for the menu's container tag. The following special options are |
|
141
|
|
|
* recognized: |
|
142
|
|
|
* |
|
143
|
|
|
* - tag: string, defaults to "ul", the tag name of the item container tags. Set to false to disable container tag. |
|
144
|
|
|
* See also {@see \Yiisoft\Html\Html::tag()}. |
|
145
|
|
|
* |
|
146
|
|
|
* @return $this |
|
147
|
|
|
* |
|
148
|
|
|
* {@see \Yiisoft\Html\Html::renderTagAttributes()} for details on how attributes are being rendered. |
|
149
|
|
|
*/ |
|
150
|
4 |
|
public function options(array $value): self |
|
151
|
|
|
{ |
|
152
|
4 |
|
$this->options = $value; |
|
153
|
|
|
|
|
154
|
4 |
|
return $this; |
|
155
|
|
|
} |
|
156
|
|
|
|
|
157
|
|
|
/** |
|
158
|
|
|
* @param bool $value whether the labels for menu items should be HTML-encoded. |
|
159
|
|
|
* |
|
160
|
|
|
* @return $this |
|
161
|
|
|
*/ |
|
162
|
1 |
|
public function encodeLabels(bool $value): self |
|
163
|
|
|
{ |
|
164
|
1 |
|
$this->encodeLabels = $value; |
|
165
|
|
|
|
|
166
|
1 |
|
return $this; |
|
167
|
|
|
} |
|
168
|
|
|
|
|
169
|
|
|
/** |
|
170
|
|
|
* @param bool $value the first hyperlink in the breadcrumbs (called home link). If this property is true, it will |
|
171
|
|
|
* default to a link pointing to HomeUrl '\' with the label 'Home'. If this property is false, the home link will |
|
172
|
|
|
* not be rendered. |
|
173
|
|
|
* |
|
174
|
|
|
* @return $this |
|
175
|
|
|
*/ |
|
176
|
7 |
|
public function homeLink(bool $value): self |
|
177
|
|
|
{ |
|
178
|
7 |
|
$this->homeLink = $value; |
|
179
|
|
|
|
|
180
|
7 |
|
return $this; |
|
181
|
|
|
} |
|
182
|
|
|
|
|
183
|
1 |
|
public function homeUrlLink(array $value): self |
|
184
|
|
|
{ |
|
185
|
1 |
|
$this->homeUrlLink = $value; |
|
186
|
|
|
|
|
187
|
1 |
|
return $this; |
|
188
|
|
|
} |
|
189
|
|
|
|
|
190
|
|
|
/** |
|
191
|
|
|
* @param array $value list of links to appear in the breadcrumbs. If this property is empty, the widget will not |
|
192
|
|
|
* render anything. Each array element represents a single link in the breadcrumbs with the following structure: |
|
193
|
|
|
* |
|
194
|
|
|
* ```php |
|
195
|
|
|
* [ |
|
196
|
|
|
* 'label' => 'label of the link', // required |
|
197
|
|
|
* 'url' => 'url of the link', // optional |
|
198
|
|
|
* 'template' => 'own template of the item', // optional, if not set $this->itemTemplate will be used |
|
199
|
|
|
* ] |
|
200
|
|
|
* ``` |
|
201
|
|
|
* |
|
202
|
|
|
* If a link is active, you only need to specify its "label", and instead of writing `['label' => $label]`, you may |
|
203
|
|
|
* simply use `$label`. |
|
204
|
|
|
* |
|
205
|
|
|
* Additional array elements for each link will be treated as the HTML attributes for the hyperlink tag. |
|
206
|
|
|
* For example, the following link specification will generate a hyperlink with CSS class `external`: |
|
207
|
|
|
* |
|
208
|
|
|
* ```php |
|
209
|
|
|
* [ |
|
210
|
|
|
* 'label' => 'demo', |
|
211
|
|
|
* 'url' => 'http://example.com', |
|
212
|
|
|
* 'class' => 'external', |
|
213
|
|
|
* ] |
|
214
|
|
|
* ``` |
|
215
|
|
|
* |
|
216
|
|
|
* Each individual link can override global {@see encodeLabels} param like the following: |
|
217
|
|
|
* |
|
218
|
|
|
* ```php |
|
219
|
|
|
* [ |
|
220
|
|
|
* 'label' => '<strong>Hello!</strong>', |
|
221
|
|
|
* 'encode' => false, |
|
222
|
|
|
* ] |
|
223
|
|
|
* ``` |
|
224
|
|
|
* |
|
225
|
|
|
* @return $this |
|
226
|
|
|
*/ |
|
227
|
8 |
|
public function links(array $value): self |
|
228
|
|
|
{ |
|
229
|
8 |
|
if (!array_key_exists('label', $value)) { |
|
230
|
1 |
|
throw new InvalidArgumentException('The "label" element is required for each link.'); |
|
231
|
|
|
} |
|
232
|
|
|
|
|
233
|
7 |
|
$this->links = $value; |
|
234
|
|
|
|
|
235
|
7 |
|
return $this; |
|
236
|
|
|
} |
|
237
|
|
|
|
|
238
|
|
|
/** |
|
239
|
|
|
* @param string $value the template used to render each inactive item in the breadcrumbs. The token `{link}` will |
|
240
|
|
|
* be replaced with the actual HTML link for each inactive item. |
|
241
|
|
|
* |
|
242
|
|
|
* @return $this |
|
243
|
|
|
*/ |
|
244
|
1 |
|
public function itemTemplate(string $value): self |
|
245
|
|
|
{ |
|
246
|
1 |
|
$this->itemTemplate = $value; |
|
247
|
|
|
|
|
248
|
1 |
|
return $this; |
|
249
|
|
|
} |
|
250
|
|
|
|
|
251
|
|
|
/** |
|
252
|
|
|
* @param string $value the template used to render each active item in the breadcrumbs. The token `{link}` will be |
|
253
|
|
|
* replaced with the actual HTML link for each active item. |
|
254
|
|
|
* |
|
255
|
|
|
* @return $this |
|
256
|
|
|
*/ |
|
257
|
3 |
|
public function activeItemTemplate(string $value): self |
|
258
|
|
|
{ |
|
259
|
3 |
|
$this->activeItemTemplate = $value; |
|
260
|
|
|
|
|
261
|
3 |
|
return $this; |
|
262
|
|
|
} |
|
263
|
|
|
} |
|
264
|
|
|
|