|
1
|
|
|
<?php |
|
2
|
|
|
/** |
|
3
|
|
|
* @link https://github.com/DMGPage/yii2-materialize |
|
4
|
|
|
* @copyright Copyright (c) 2018 Dmitrijs Reinmanis |
|
5
|
|
|
* @license https://github.com/DMGPage/yii2-materialize/blob/master/LICENSE |
|
6
|
|
|
*/ |
|
7
|
|
|
|
|
8
|
|
|
namespace dmgpage\yii2materialize\widgets; |
|
9
|
|
|
|
|
10
|
|
|
use yii\base\InvalidConfigException; |
|
11
|
|
|
use yii\helpers\ArrayHelper; |
|
12
|
|
|
use dmgpage\yii2materialize\helpers\Html; |
|
13
|
|
|
use dmgpage\yii2materialize\assets\MaterializeExtraAsset; |
|
14
|
|
|
|
|
15
|
|
|
/** |
|
16
|
|
|
* Breadcrumbs displays a list of links indicating the position of the current page in the whole site hierarchy. |
|
17
|
|
|
* |
|
18
|
|
|
* For example, breadcrumbs like "Home / Sample Post / Edit" means the user is viewing an edit page |
|
19
|
|
|
* for the "Sample Post". He can click on "Sample Post" to view that page, or he can click on "Home" |
|
20
|
|
|
* to return to the homepage. |
|
21
|
|
|
* |
|
22
|
|
|
* To use Breadcrumbs, you need to configure its [[links]] property, which specifies the links to be displayed. For example, |
|
23
|
|
|
* |
|
24
|
|
|
* ```php |
|
25
|
|
|
* echo Breadcrumbs::widget([ |
|
26
|
|
|
* 'type' => BreadcrumbType::BASE, |
|
27
|
|
|
* 'homeLink' => [ |
|
28
|
|
|
* 'label' => 'Home', |
|
29
|
|
|
* 'url' => '/', |
|
30
|
|
|
* 'icon' => 'home' |
|
31
|
|
|
* ], |
|
32
|
|
|
* 'links' => [ |
|
33
|
|
|
* [ |
|
34
|
|
|
* 'label' => 'Post Category', |
|
35
|
|
|
* 'url' => ['post-category/view', 'id' => 10], |
|
36
|
|
|
* 'target' => '_blank', |
|
37
|
|
|
* 'icon' => 'create' |
|
38
|
|
|
* ], |
|
39
|
|
|
* [ |
|
40
|
|
|
* 'label' => 'Sample Post', |
|
41
|
|
|
* 'url' => ['post/edit', 'id' => 1], |
|
42
|
|
|
* 'icon' => 'reorder' |
|
43
|
|
|
* ], |
|
44
|
|
|
* 'Edit', |
|
45
|
|
|
* ], |
|
46
|
|
|
* ]); |
|
47
|
|
|
* ``` |
|
48
|
|
|
* |
|
49
|
|
|
* Because breadcrumbs usually appears in nearly every page of a website, you may consider placing it in a layout view. |
|
50
|
|
|
* You can use a view parameter (e.g. `$this->params['breadcrumbs']`) to configure the links in different |
|
51
|
|
|
* views. In the layout view, you assign this view parameter to the [[links]] property like the following: |
|
52
|
|
|
* |
|
53
|
|
|
* ```php |
|
54
|
|
|
* // $this is the view object currently being used |
|
55
|
|
|
* echo Breadcrumbs::widget([ |
|
56
|
|
|
* 'links' => isset($this->params['breadcrumbs']) ? $this->params['breadcrumbs'] : [], |
|
57
|
|
|
* ]); |
|
58
|
|
|
* ``` |
|
59
|
|
|
* @see https://materializecss.com/breadcrumbs.html |
|
60
|
|
|
* @package widgets |
|
61
|
|
|
*/ |
|
62
|
|
|
class Breadcrumbs extends Widget |
|
63
|
|
|
{ |
|
64
|
|
|
/** |
|
65
|
|
|
* @var array the HTML attributes for the breadcrumb container tag. |
|
66
|
|
|
* @see \yii\helpers\Html::renderTagAttributes() for details on how attributes are being rendered. |
|
67
|
|
|
*/ |
|
68
|
|
|
public $options = []; |
|
69
|
|
|
|
|
70
|
|
|
/** |
|
71
|
|
|
* @var array the HTML attributes for the breadcrumb wrapper tag. |
|
72
|
|
|
* @see \yii\helpers\Html::renderTagAttributes() for details on how attributes are being rendered. |
|
73
|
|
|
*/ |
|
74
|
|
|
public $wrapperOptions = []; |
|
75
|
|
|
|
|
76
|
|
|
/** |
|
77
|
|
|
* @var array the HTML attributes for the breadcrumb column tag. |
|
78
|
|
|
* @see \yii\helpers\Html::renderTagAttributes() for details on how attributes are being rendered. |
|
79
|
|
|
*/ |
|
80
|
|
|
public $columnOptions = []; |
|
81
|
|
|
|
|
82
|
|
|
/** |
|
83
|
|
|
* @var string the type of breadcrumb to be rendered |
|
84
|
|
|
* @see \dmgpage\yii2materialize\helpers\BreadcrumbType |
|
85
|
|
|
*/ |
|
86
|
|
|
public $type; |
|
87
|
|
|
|
|
88
|
|
|
/** |
|
89
|
|
|
* @var bool whether to HTML-encode the link labels. |
|
90
|
|
|
*/ |
|
91
|
|
|
public $encodeLabels = true; |
|
92
|
|
|
|
|
93
|
|
|
/** |
|
94
|
|
|
* @var array the first hyperlink in the breadcrumbs (called home link). |
|
95
|
|
|
* Please refer to [[links]] on the format of the link. |
|
96
|
|
|
* If this property is not set, it will default to a link pointing to [[\yii\web\Application::homeUrl]] |
|
97
|
|
|
* with the label 'Home'. If this property contains `['render' => false]`, the home link will not be rendered. |
|
98
|
|
|
*/ |
|
99
|
|
|
public $homeLink; |
|
100
|
|
|
|
|
101
|
|
|
/** |
|
102
|
|
|
* @var array list of links to appear in the breadcrumbs. If this property is empty, |
|
103
|
|
|
* the widget will not render anything. Each array element represents a single link in the breadcrumbs |
|
104
|
|
|
* with the following structure: |
|
105
|
|
|
* |
|
106
|
|
|
* ```php |
|
107
|
|
|
* [ |
|
108
|
|
|
* 'label' => 'label of the link', // required |
|
109
|
|
|
* 'url' => 'url of the link', // optional, will be processed by Url::to() |
|
110
|
|
|
* 'icon' => [ // optional, will generate icon with given name and options |
|
111
|
|
|
* 'name' => 'home', |
|
112
|
|
|
* 'options' => ['class' => 'red'] |
|
113
|
|
|
* ] |
|
114
|
|
|
* ] |
|
115
|
|
|
* ``` |
|
116
|
|
|
* |
|
117
|
|
|
* If a link is active, you only need to specify its "label", and instead of writing `['label' => $label]`, |
|
118
|
|
|
* you may simply use `$label`. |
|
119
|
|
|
*/ |
|
120
|
|
|
public $links = []; |
|
121
|
|
|
|
|
122
|
|
|
/** |
|
123
|
|
|
* Initialize the widget. |
|
124
|
|
|
*/ |
|
125
|
|
|
public function init() |
|
126
|
|
|
{ |
|
127
|
|
|
parent::init(); |
|
128
|
|
|
|
|
129
|
|
|
if (!isset($this->wrapperOptions['class'])) { |
|
130
|
|
|
Html::addCssClass($this->wrapperOptions, 'nav-wrapper'); |
|
131
|
|
|
} |
|
132
|
|
|
|
|
133
|
|
|
if (!isset($this->columnOptions['class'])) { |
|
134
|
|
|
Html::addCssClass($this->columnOptions, 'col s12'); |
|
135
|
|
|
} |
|
136
|
|
|
} |
|
137
|
|
|
|
|
138
|
|
|
/** |
|
139
|
|
|
* Renders the widget. |
|
140
|
|
|
*/ |
|
141
|
|
|
public function run() |
|
142
|
|
|
{ |
|
143
|
|
|
$this->registerPlugin('breadcrumb'); |
|
144
|
|
|
|
|
145
|
|
|
if (!empty($this->links)) { |
|
146
|
|
|
$links = []; |
|
147
|
|
|
|
|
148
|
|
|
if (empty($this->homeLink)) { |
|
149
|
|
|
$links[] = $this->renderItem( |
|
150
|
|
|
[ |
|
151
|
|
|
'label' => \Yii::t('yii', 'Home'), |
|
152
|
|
|
'url' => \Yii::$app->homeUrl |
|
153
|
|
|
] |
|
154
|
|
|
); |
|
155
|
|
|
} elseif (!isset($this->homeLink['render']) || $this->homeLink['render'] === true) { |
|
156
|
|
|
$links[] = $this->renderItem($this->homeLink); |
|
157
|
|
|
} |
|
158
|
|
|
|
|
159
|
|
|
foreach ($this->links as $link) { |
|
160
|
|
|
if (!is_array($link)) { |
|
161
|
|
|
$link = ['label' => $link]; |
|
162
|
|
|
} |
|
163
|
|
|
|
|
164
|
|
|
$links[] = $this->renderItem($link); |
|
165
|
|
|
} |
|
166
|
|
|
|
|
167
|
|
|
$column = Html::tag('div', implode('', $links), $this->columnOptions); |
|
168
|
|
|
$wraper = Html::tag('div', $column, $this->wrapperOptions); |
|
169
|
|
|
|
|
170
|
|
|
if (!empty($this->type)) { |
|
171
|
|
|
Html::addCssClass($this->options, $this->type); |
|
172
|
|
|
$view = $this->getView(); |
|
173
|
|
|
MaterializeExtraAsset::register($view); |
|
174
|
|
|
} |
|
175
|
|
|
|
|
176
|
|
|
return Html::tag('nav', $wraper, $this->options); |
|
177
|
|
|
} |
|
178
|
|
|
} |
|
179
|
|
|
|
|
180
|
|
|
/** |
|
181
|
|
|
* Renders a single breadcrumb item. |
|
182
|
|
|
* |
|
183
|
|
|
* @param array $link the link to be rendered. It must contain the "label" element. The "url" and "icon" element is optional. |
|
184
|
|
|
* @return string the rendering result |
|
185
|
|
|
* @throws InvalidConfigException if `$link` does not have "label" element. |
|
186
|
|
|
*/ |
|
187
|
|
|
protected function renderItem($link) |
|
188
|
|
|
{ |
|
189
|
|
|
$encodeLabel = ArrayHelper::remove($link, 'encode', $this->encodeLabels); |
|
190
|
|
|
|
|
191
|
|
|
if (array_key_exists('label', $link)) { |
|
192
|
|
|
$label = $encodeLabel ? Html::encode($link['label']) : $link['label']; |
|
193
|
|
|
} else { |
|
194
|
|
|
throw new InvalidConfigException('The "label" element is required for each link.'); |
|
195
|
|
|
} |
|
196
|
|
|
|
|
197
|
|
|
// Add icon to label text |
|
198
|
|
|
if (isset($link['icon'])) { |
|
199
|
|
|
// Has issues on positioning: https://github.com/Dogfalo/materialize/issues/6224 |
|
200
|
|
|
$label = $this->renderIcon($link['icon']) . $label; |
|
201
|
|
|
} |
|
202
|
|
|
|
|
203
|
|
|
$options = $link; |
|
204
|
|
|
unset($options['label'], $options['url'], $options['icon']); |
|
205
|
|
|
|
|
206
|
|
|
if (!isset($options['class'])) { |
|
207
|
|
|
Html::addCssClass($options, 'breadcrumb'); |
|
208
|
|
|
} |
|
209
|
|
|
|
|
210
|
|
|
if (isset($link['url'])) { |
|
211
|
|
|
return Html::a($label, $link['url'], $options); |
|
212
|
|
|
} else { |
|
213
|
|
|
return Html::tag('span', $label, $options) ; |
|
214
|
|
|
} |
|
215
|
|
|
} |
|
216
|
|
|
} |
|
217
|
|
|
|
|
218
|
|
|
|