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