-
Notifications
You must be signed in to change notification settings - Fork 28
/
Copy pathBreadcrumbs.php
218 lines (193 loc) · 7.74 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
<?php
/**
* @link https://github.com/MacGyer/yii2-materializecss
* @copyright Copyright (c) 2016 ... MacGyer for pluspunkt coding
* @license https://github.com/MacGyer/yii2-materializecss/blob/master/LICENSE
*/
namespace macgyer\yii2materializecss\widgets\navigation;
use macgyer\yii2materializecss\lib\Html;
use Yii;
use yii\base\InvalidConfigException;
use yii\helpers\ArrayHelper;
/**
* Breadcrumbs displays a list of links indicating the position of the current page in the whole site hierarchy.
*
* For example, breadcrumbs like "Home / Sample Post / Edit" means the user is viewing an edit page
* for the "Sample Post". He can click on "Sample Post" to view that page, or he can click on "Home"
* to return to the homepage.
*
* To use Breadcrumbs, you need to configure its `$links` property (inherited from
* [yii\widgets\Breadcrumbs](http://www.yiiframework.com/doc-2.0/yii-widgets-breadcrumbs.html)) , which specifies the links to be
* displayed.
*
* For example,
*
* ```php
* // $this is the view object currently being used
* echo Breadcrumbs::widget([
* 'itemTemplate' => "<i>{link}</i>\n", // template for all links
* 'links' => [
* [
* 'label' => 'Post Category',
* 'url' => ['post-category/view', 'id' => 10],
* 'template' => "<b>{link}</b>\n", // template for this link only
* ],
* ['label' => 'Sample Post', 'url' => ['post/edit', 'id' => 1]],
* 'Edit',
* ],
* ]);
* ```
*
* Because breadcrumbs usually appear in nearly every page of a website, you may consider placing it in a layout view.
* You can use a view parameter (e.g. `$this->params['breadcrumbs']`) to configure the links in different
* views. In the layout view, you assign this view parameter to the `$links` property like the following:
*
* ```php
* // $this is the view object currently being used
* echo Breadcrumbs::widget([
* 'links' => isset($this->params['breadcrumbs']) ? $this->params['breadcrumbs'] : [],
* ]);
* ```
*
* @author Christoph Erdmann <yii2-materializecss@pluspunkt-coding.de>
* @package widgets
* @subpackage navigation
* @see https://materializecss.com/breadcrumbs.html
*/
class Breadcrumbs extends \yii\widgets\Breadcrumbs
{
/**
* @var string the name of the wrapper tag for the breadcrumbs list.
* defaults to "div"
*/
public $tag = 'div';
/**
* @var array the HTML options for the surrounding "nav" tag.
*
* @see [yii\helpers\BaseHtml::renderTagAttributes()](http://www.yiiframework.com/doc-2.0/yii-helpers-basehtml.html#renderTagAttributes()-detail) for details on
* how attributes are being rendered.
*/
public $containerOptions = [];
/**
* @var array the HTML options for the wrapper tag.
*
* @see [yii\helpers\BaseHtml::renderTagAttributes()](http://www.yiiframework.com/doc-2.0/yii-helpers-basehtml.html#renderTagAttributes()-detail) for details on
* how attributes are being rendered.
*/
public $options = [];
/**
* @var array the HTML options for the inner container tag.
*
* Set this to 'false' if you do not want the inner container to be rendered.
* The following special options are recognized:
*
* - tag: string, defaults to "div", the name of the inner container tag.
*
* @see [yii\helpers\BaseHtml::renderTagAttributes()](http://www.yiiframework.com/doc-2.0/yii-helpers-basehtml.html#renderTagAttributes()-detail) for details on
* how attributes are being rendered.
* @see https://github.com/MacGyer/yii2-materializecss/pull/5
*/
public $innerContainerOptions = [];
/**
* @var string 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.
*/
public $itemTemplate = "{link}\n";
/**
* @var string 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.
*/
public $activeItemTemplate = "<span class=\"breadcrumb active\">{link}</span>\n";
/**
* Initialize the widget.
*/
public function init()
{
if (!isset($this->containerOptions['class'])) {
Html::addCssClass($this->containerOptions, ['breadcrumbsContainer' => 'breadcrumbs']);
}
if (!isset($this->options['class'])) {
Html::addCssClass($this->options, ['wrapper' => 'nav-wrapper']);
}
if ($this->innerContainerOptions !== false && !isset($this->innerContainerOptions['class'])) {
Html::addCssClass($this->innerContainerOptions, ['innerContainer' => 'col s12']);
}
}
/**
* Render the widget.
* @return string the result of widget execution to be outputted.
* @throws InvalidConfigException
*/
public function run()
{
if (empty($this->links)) {
return '';
}
$html[] = Html::beginTag('nav', $this->containerOptions);
$html[] = Html::beginTag($this->tag, $this->options);
if ($this->innerContainerOptions !== false) {
$innerContainerTag = ArrayHelper::remove($this->innerContainerOptions, 'tag', 'div');
$html[] = Html::beginTag($innerContainerTag, $this->innerContainerOptions);
}
$html[] = implode('', $this->prepareLinks());
if ($this->innerContainerOptions !== false) {
$html[] = Html::endTag($innerContainerTag);
}
$html[] = Html::endTag($this->tag);
$html[] = Html::endTag('nav');
return implode("\n", $html);
}
/**
* 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.
* @return string the rendering result
* @throws InvalidConfigException if `$link` does not have "label" element.
*/
protected function renderItem($link, $template)
{
$encodeLabel = ArrayHelper::remove($link, 'encode', $this->encodeLabels);
if (array_key_exists('label', $link)) {
$label = $encodeLabel ? Html::encode($link['label']) : $link['label'];
} else {
throw new InvalidConfigException('The "label" element is required for each link.');
}
if (isset($link['template'])) {
$template = $link['template'];
}
if (isset($link['url'])) {
$options = $link;
Html::addCssClass($options, ['link' => 'breadcrumb']);
unset($options['template'], $options['label'], $options['url']);
$link = Html::a($label, $link['url'], $options);
} else {
$link = $label;
}
return strtr($template, ['{link}' => $link]);
}
/**
* Generates all breadcrumb links and sets active states.
* @return array all processed items
* @uses [[renderItem]]
* @throws InvalidConfigException
*/
protected function prepareLinks()
{
$links = [];
if ($this->homeLink === null) {
$links[] = $this->renderItem([
'label' => Yii::t('yii', 'Home'),
'url' => Yii::$app->homeUrl,
], $this->itemTemplate);
} elseif ($this->homeLink !== false) {
$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 $links;
}
}