/
Breadcrumbs.php
251 lines (217 loc) · 6.71 KB
/
Breadcrumbs.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
<?php
declare(strict_types=1);
namespace Yiisoft\Yii\Bootstrap5;
use JsonException;
use RuntimeException;
use Yiisoft\Arrays\ArrayHelper;
use Yiisoft\Html\Html;
use function array_key_exists;
use function implode;
use function is_array;
use function strtr;
/**
* Button renders a bootstrap button.
*
* For example,
*
* ```php
* echo Breadcrumbs::widget()
* ->links(['label' => !empty($this->params['breadcrumbs']) ? $this->params['breadcrumbs'] : []]);
* ```
*/
final class Breadcrumbs extends Widget
{
private string $tag = 'ol';
private bool $encodeLabels = true;
private bool $encodeTags = false;
private array $homeLink = [];
private array $links = [];
private string $itemTemplate = "<li class=\"breadcrumb-item\">{link}</li>\n";
private string $activeItemTemplate = "<li class=\"breadcrumb-item active\" aria-current=\"page\">{link}</li>\n";
private array $navOptions = ['aria-label' => 'breadcrumb'];
private array $options = [];
protected function run(): string
{
if (empty($this->links)) {
return '';
}
if (!isset($this->options['id'])) {
$this->options['id'] = "{$this->getId()}-breadcrumb";
}
/** @psalm-suppress InvalidArgument */
Html::addCssClass($this->options, ['widget' => 'breadcrumb']);
$links = [];
if ($this->homeLink === []) {
$links[] = $this->renderItem([
'label' => 'Home',
'url' => '/',
], $this->itemTemplate);
} else {
$links[] = $this->renderItem($this->homeLink, $this->itemTemplate);
}
foreach ($this->links as $link) {
if (!is_array($link)) {
$link = ['label' => $link];
}
$links[] = $this->renderItem($link, isset($link['url']) ? $this->itemTemplate : $this->activeItemTemplate);
}
return Html::tag(
'nav',
Html::tag($this->tag, implode('', $links), $this->options)->encode($this->encodeTags)->render(),
$this->navOptions
)->encode($this->encodeTags)->render();
}
/**
* The template used to render each active item in the breadcrumbs. The token `{link}` will be replaced with the
* actual HTML link for each active item.
*
* @param string $value
*
* @return self
*/
public function activeItemTemplate(string $value): self
{
$new = clone $this;
$new->activeItemTemplate = $value;
return $new;
}
/**
* When tags Labels HTML should not be encoded.
*
* @return self
*/
public function withoutEncodeLabels(): self
{
$new = clone $this;
$new->encodeLabels = false;
return $new;
}
/**
* The first hyperlink in the breadcrumbs (called home link).
*
* Please refer to {@see links} on the format of the link.
*
* If this property is not set, it will default to a link pointing with the label 'Home'. If this property is false,
* the home link will not be rendered.
*
* @param array $value
*
* @return self
*/
public function homeLink(array $value): self
{
$new = clone $this;
$new->homeLink = $value;
return $new;
}
/**
* The template used to render each inactive item in the breadcrumbs. The token `{link}` will be replaced with the
* actual HTML link for each inactive item.
*
* @param string $value
*
* @return self
*/
public function itemTemplate(string $value): self
{
$new = clone $this;
$new->itemTemplate = $value;
return $new;
}
/**
* List of links to appear in the breadcrumbs. If this property is empty, the widget will not render anything. Each
* array element represents a single link in the breadcrumbs with the following structure:
*
* ```php
* [
* 'label' => 'label of the link', // required
* 'url' => 'url of the link', // optional, will be processed by Url::to()
* 'template' => 'own template of the item', // optional, if not set $this->itemTemplate will be used
* ]
* ```
*
* @param array $value
*
* @return self
*/
public function links(array $value): self
{
$new = clone $this;
$new->links = $value;
return $new;
}
/**
* The HTML attributes for the widgets nav container tag.
*
* {@see Html::renderTagAttributes()} for details on how attributes are being rendered.
*
* @param array $value
*
* @return self
*/
public function navOptions(array $value): self
{
$new = clone $this;
$new->navOptions = $value;
return $new;
}
/**
* The HTML attributes for the widget container tag. The following special options are recognized.
*
* {@see Html::renderTagAttributes()} for details on how attributes are being rendered.
*
* @param array $value
*
* @return self
*/
public function options(array $value): self
{
$new = clone $this;
$new->options = $value;
return $new;
}
/**
* The name of the breadcrumb container tag.
*
* @param string $value
*
* @return self
*/
public function tag(string $value): self
{
$new = clone $this;
$new->tag = $value;
return $new;
}
/**
* Renders a single breadcrumb item.
*
* @param array $link the link to be rendered. It must contain the "label" element. The "url" element is optional.
* @param string $template the template to be used to rendered the link. The token "{link}" will be replaced by the
* link.
*
* @throws JsonException|RuntimeException if `$link` does not have "label" element.
*
* @return string the rendering result
*/
private function renderItem(array $link, string $template): string
{
$encodeLabel = ArrayHelper::remove($link, 'encode', $this->encodeLabels);
if (array_key_exists('label', $link)) {
$label = $encodeLabel ? Html::encode($link['label']) : $link['label'];
} else {
throw new RuntimeException('The "label" element is required for each link.');
}
if (isset($link['template'])) {
$template = $link['template'];
}
if (isset($link['url'])) {
$options = $link;
unset($options['template'], $options['label'], $options['url']);
$linkHtml = Html::a($label, $link['url'], $options)->encode($this->encodeTags);
} else {
$linkHtml = $label;
}
return strtr($template, ['{link}' => $linkHtml]);
}
}