|
1 |
| -<?php |
2 |
| - |
3 |
| -namespace macgyer\yii2materializecss\widgets; |
4 |
| - |
5 |
| -use macgyer\yii2materializecss\lib\Html; |
6 |
| -use Yii; |
7 |
| -use yii\base\InvalidConfigException; |
8 |
| -use yii\helpers\ArrayHelper; |
9 |
| - |
10 |
| -/** |
11 |
| - * Class Breadcrumbs |
12 |
| - * @package macgyer\yii2materializecss\widgets |
13 |
| - */ |
14 |
| -class Breadcrumbs extends \yii\widgets\Breadcrumbs |
15 |
| -{ |
16 |
| - /** |
17 |
| - * @var string the wrapper for the breadcrumbs list |
18 |
| - * defaults to "div" |
19 |
| - */ |
20 |
| - public $tag = 'div'; |
21 |
| - |
22 |
| - /** |
23 |
| - * @var array the HTML options for the wrapper tag |
24 |
| - * |
25 |
| - * @see \yii\helpers\Html::renderTagAttributes() for details on how attributes are being rendered. |
26 |
| - */ |
27 |
| - public $options; |
28 |
| - |
29 |
| - /** |
30 |
| - * @var array the HTML options for the surrounding "nav" tag |
31 |
| - * |
32 |
| - * @see \yii\helpers\Html::renderTagAttributes() for details on how attributes are being rendered. |
33 |
| - */ |
34 |
| - public $containerOptions = []; |
35 |
| - |
36 |
| - /** |
37 |
| - * @var boolean whether to HTML-encode the link labels. |
38 |
| - */ |
39 |
| - public $encodeLabels = true; |
40 |
| - |
41 |
| - /** |
42 |
| - * @var array the first hyperlink in the breadcrumbs (called home link). |
43 |
| - * Please refer to [[links]] on the format of the link. |
44 |
| - * If this property is not set, it will default to a link pointing to [[\yii\web\Application::homeUrl]] |
45 |
| - * with the label 'Home'. If this property is false, the home link will not be rendered. |
46 |
| - */ |
47 |
| - public $homeLink; |
48 |
| - |
49 |
| - /** |
50 |
| - * @var array list of links to appear in the breadcrumbs. If this property is empty, |
51 |
| - * the widget will not render anything. Each array element represents a single link in the breadcrumbs |
52 |
| - * with the following structure: |
53 |
| - * |
54 |
| - * ```php |
55 |
| - * [ |
56 |
| - * 'label' => 'label of the link', // required |
57 |
| - * 'url' => 'url of the link', // optional, will be processed by Url::to() |
58 |
| - * 'template' => 'own template of the item', // optional, if not set $this->itemTemplate will be used |
59 |
| - * ] |
60 |
| - * ``` |
61 |
| - * |
62 |
| - * If a link is active, you only need to specify its "label", and instead of writing `['label' => $label]`, |
63 |
| - * you may simply use `$label`. |
64 |
| - * |
65 |
| - * Since version 2.0.1, any additional array elements for each link will be treated as the HTML attributes |
66 |
| - * for the hyperlink tag. For example, the following link specification will generate a hyperlink |
67 |
| - * with CSS class `external`: |
68 |
| - * |
69 |
| - * ```php |
70 |
| - * [ |
71 |
| - * 'label' => 'demo', |
72 |
| - * 'url' => 'http://example.com', |
73 |
| - * 'class' => 'external', |
74 |
| - * ] |
75 |
| - * ``` |
76 |
| - * |
77 |
| - * Since version 2.0.3 each individual link can override global [[encodeLabels]] param like the following: |
78 |
| - * |
79 |
| - * ```php |
80 |
| - * [ |
81 |
| - * 'label' => '<strong>Hello!</strong>', |
82 |
| - * 'encode' => false, |
83 |
| - * ] |
84 |
| - * ``` |
85 |
| - * |
86 |
| - */ |
87 |
| - public $links = []; |
88 |
| - |
89 |
| - /** |
90 |
| - * @var string the template used to render each inactive item in the breadcrumbs. The token `{link}` |
91 |
| - * will be replaced with the actual HTML link for each inactive item. |
92 |
| - */ |
93 |
| - public $itemTemplate = "{link}\n"; |
94 |
| - |
95 |
| - /** |
96 |
| - * @var string the template used to render each active item in the breadcrumbs. The token `{link}` |
97 |
| - * will be replaced with the actual HTML link for each active item. |
98 |
| - */ |
99 |
| - public $activeItemTemplate = "<span class=\"breadcrumb active\">{link}</span>\n"; |
100 |
| - |
101 |
| - /** |
102 |
| - * @inheritdoc |
103 |
| - */ |
104 |
| - public function run() |
105 |
| - { |
106 |
| - if (empty($this->links)) { |
107 |
| - return; |
108 |
| - } |
109 |
| - |
110 |
| - if (!isset($this->containerOptions['class'])) { |
111 |
| - Html::addCssClass($this->containerOptions, ['breadcrumbsContainer' => 'breadcrumbs']); |
112 |
| - } |
113 |
| - echo Html::beginTag('nav', $this->containerOptions); |
114 |
| - |
115 |
| - $links = []; |
116 |
| - if ($this->homeLink === null) { |
117 |
| - $links[] = $this->renderItem([ |
118 |
| - 'label' => Yii::t('yii', 'Home'), |
119 |
| - 'url' => Yii::$app->homeUrl, |
120 |
| - ], $this->itemTemplate); |
121 |
| - } elseif ($this->homeLink !== false) { |
122 |
| - $links[] = $this->renderItem($this->homeLink, $this->itemTemplate); |
123 |
| - } |
124 |
| - |
125 |
| - foreach ($this->links as $link) { |
126 |
| - if (!is_array($link)) { |
127 |
| - $link = ['label' => $link]; |
128 |
| - } |
129 |
| - $links[] = $this->renderItem($link, isset($link['url']) ? $this->itemTemplate : $this->activeItemTemplate); |
130 |
| - } |
131 |
| - |
132 |
| - if (!isset($this->options['class'])) { |
133 |
| - Html::addCssClass($this->options, ['wrapper' => 'nav-wrapper']); |
134 |
| - } |
135 |
| - echo Html::tag($this->tag, implode('', $links), $this->options); |
136 |
| - |
137 |
| - echo Html::endTag('nav'); |
138 |
| - } |
139 |
| - |
140 |
| - /** |
141 |
| - * Renders a single breadcrumb item. |
142 |
| - * @param array $link the link to be rendered. It must contain the "label" element. The "url" element is optional. |
143 |
| - * @param string $template the template to be used to rendered the link. The token "{link}" will be replaced by the link. |
144 |
| - * @return string the rendering result |
145 |
| - * @throws InvalidConfigException if `$link` does not have "label" element. |
146 |
| - */ |
147 |
| - protected function renderItem($link, $template) |
148 |
| - { |
149 |
| - $encodeLabel = ArrayHelper::remove($link, 'encode', $this->encodeLabels); |
150 |
| - if (array_key_exists('label', $link)) { |
151 |
| - $label = $encodeLabel ? Html::encode($link['label']) : $link['label']; |
152 |
| - } else { |
153 |
| - throw new InvalidConfigException('The "label" element is required for each link.'); |
154 |
| - } |
155 |
| - |
156 |
| - if (isset($link['template'])) { |
157 |
| - $template = $link['template']; |
158 |
| - } |
159 |
| - |
160 |
| - if (isset($link['url'])) { |
161 |
| - $options = $link; |
162 |
| - Html::addCssClass($options, ['link' => 'breadcrumb']); |
163 |
| - |
164 |
| - unset($options['template'], $options['label'], $options['url']); |
165 |
| - $link = Html::a($label, $link['url'], $options); |
166 |
| - } else { |
167 |
| - $link = $label; |
168 |
| - } |
169 |
| - |
170 |
| - return strtr($template, ['{link}' => $link]); |
171 |
| - } |
172 |
| -} |
| 1 | +<?php |
| 2 | + |
| 3 | +namespace macgyer\yii2materializecss\widgets; |
| 4 | + |
| 5 | +use macgyer\yii2materializecss\lib\Html; |
| 6 | +use Yii; |
| 7 | +use yii\base\InvalidConfigException; |
| 8 | +use yii\helpers\ArrayHelper; |
| 9 | + |
| 10 | +/** |
| 11 | + * Class Breadcrumbs |
| 12 | + * @package macgyer\yii2materializecss\widgets |
| 13 | + */ |
| 14 | +class Breadcrumbs extends \yii\widgets\Breadcrumbs |
| 15 | +{ |
| 16 | + /** |
| 17 | + * @var string the wrapper for the breadcrumbs list |
| 18 | + * defaults to "div" |
| 19 | + */ |
| 20 | + public $tag = 'div'; |
| 21 | + |
| 22 | + /** |
| 23 | + * @var array the HTML options for the surrounding "nav" tag |
| 24 | + * |
| 25 | + * @see \yii\helpers\Html::renderTagAttributes() for details on how attributes are being rendered. |
| 26 | + */ |
| 27 | + public $containerOptions = []; |
| 28 | + |
| 29 | + /** |
| 30 | + * @var array the HTML options for the wrapper tag |
| 31 | + * |
| 32 | + * @see \yii\helpers\Html::renderTagAttributes() for details on how attributes are being rendered. |
| 33 | + */ |
| 34 | + public $options = []; |
| 35 | + |
| 36 | + /** |
| 37 | + * @var array the HTML options for the inner container tag. |
| 38 | + * |
| 39 | + * Set this to 'false' if you do not want the inner container to be rendered. |
| 40 | + * The following special options are recognized: |
| 41 | + * |
| 42 | + * - tag: string, defaults to "div", the name of the inner container tag. |
| 43 | + * |
| 44 | + * @see \yii\helpers\Html::renderTagAttributes() for details on how attributes are being rendered. |
| 45 | + * @see https://github.com/MacGyer/yii2-materializecss/pull/5 |
| 46 | + */ |
| 47 | + public $innerContainerOptions = []; |
| 48 | + |
| 49 | + /** |
| 50 | + * @var string the template used to render each inactive item in the breadcrumbs. The token `{link}` |
| 51 | + * will be replaced with the actual HTML link for each inactive item. |
| 52 | + */ |
| 53 | + public $itemTemplate = "{link}\n"; |
| 54 | + |
| 55 | + /** |
| 56 | + * @var string the template used to render each active item in the breadcrumbs. The token `{link}` |
| 57 | + * will be replaced with the actual HTML link for each active item. |
| 58 | + */ |
| 59 | + public $activeItemTemplate = "<span class=\"breadcrumb active\">{link}</span>\n"; |
| 60 | + |
| 61 | + /** |
| 62 | + * Initialize the widget. |
| 63 | + */ |
| 64 | + public function init() |
| 65 | + { |
| 66 | + if (!isset($this->containerOptions['class'])) { |
| 67 | + Html::addCssClass($this->containerOptions, ['breadcrumbsContainer' => 'breadcrumbs']); |
| 68 | + } |
| 69 | + |
| 70 | + if (!isset($this->options['class'])) { |
| 71 | + Html::addCssClass($this->options, ['wrapper' => 'nav-wrapper']); |
| 72 | + } |
| 73 | + |
| 74 | + if ($this->innerContainerOptions !== false && !isset($this->innerContainerOptions['class'])) { |
| 75 | + Html::addCssClass($this->innerContainerOptions, ['innerContainer' => 'col s12']); |
| 76 | + } |
| 77 | + } |
| 78 | + |
| 79 | + /** |
| 80 | + * Renders the widget. |
| 81 | + */ |
| 82 | + public function run() |
| 83 | + { |
| 84 | + if (empty($this->links)) { |
| 85 | + return; |
| 86 | + } |
| 87 | + |
| 88 | + echo Html::beginTag('nav', $this->containerOptions); |
| 89 | + echo Html::beginTag($this->tag, $this->options); |
| 90 | + |
| 91 | + if ($this->innerContainerOptions !== false) { |
| 92 | + $innerContainerTag = ArrayHelper::remove($this->innerContainerOptions, 'tag', 'div'); |
| 93 | + echo Html::beginTag($innerContainerTag, $this->innerContainerOptions); |
| 94 | + } |
| 95 | + echo implode('', $this->prepareLinks()); |
| 96 | + |
| 97 | + if ($this->innerContainerOptions !== false) { |
| 98 | + echo Html::endTag($innerContainerTag); |
| 99 | + } |
| 100 | + |
| 101 | + echo Html::endTag($this->tag); |
| 102 | + echo Html::endTag('nav'); |
| 103 | + } |
| 104 | + |
| 105 | + /** |
| 106 | + * Renders a single breadcrumb item. |
| 107 | + * @param array $link the link to be rendered. It must contain the "label" element. The "url" element is optional. |
| 108 | + * @param string $template the template to be used to rendered the link. The token "{link}" will be replaced by the link. |
| 109 | + * @return string the rendering result |
| 110 | + * @throws InvalidConfigException if `$link` does not have "label" element. |
| 111 | + */ |
| 112 | + protected function renderItem($link, $template) |
| 113 | + { |
| 114 | + $encodeLabel = ArrayHelper::remove($link, 'encode', $this->encodeLabels); |
| 115 | + if (array_key_exists('label', $link)) { |
| 116 | + $label = $encodeLabel ? Html::encode($link['label']) : $link['label']; |
| 117 | + } else { |
| 118 | + throw new InvalidConfigException('The "label" element is required for each link.'); |
| 119 | + } |
| 120 | + |
| 121 | + if (isset($link['template'])) { |
| 122 | + $template = $link['template']; |
| 123 | + } |
| 124 | + |
| 125 | + if (isset($link['url'])) { |
| 126 | + $options = $link; |
| 127 | + Html::addCssClass($options, ['link' => 'breadcrumb']); |
| 128 | + |
| 129 | + unset($options['template'], $options['label'], $options['url']); |
| 130 | + $link = Html::a($label, $link['url'], $options); |
| 131 | + } else { |
| 132 | + $link = $label; |
| 133 | + } |
| 134 | + |
| 135 | + return strtr($template, ['{link}' => $link]); |
| 136 | + } |
| 137 | + |
| 138 | + /** |
| 139 | + * @return array |
| 140 | + * @throws InvalidConfigException |
| 141 | + */ |
| 142 | + private function prepareLinks() |
| 143 | + { |
| 144 | + $links = []; |
| 145 | + if ($this->homeLink === null) { |
| 146 | + $links[] = $this->renderItem([ |
| 147 | + 'label' => Yii::t('yii', 'Home'), |
| 148 | + 'url' => Yii::$app->homeUrl, |
| 149 | + ], $this->itemTemplate); |
| 150 | + } elseif ($this->homeLink !== false) { |
| 151 | + $links[] = $this->renderItem($this->homeLink, $this->itemTemplate); |
| 152 | + } |
| 153 | + |
| 154 | + foreach ($this->links as $link) { |
| 155 | + if (!is_array($link)) { |
| 156 | + $link = ['label' => $link]; |
| 157 | + } |
| 158 | + $links[] = $this->renderItem($link, isset($link['url']) ? $this->itemTemplate : $this->activeItemTemplate); |
| 159 | + } |
| 160 | + |
| 161 | + return $links; |
| 162 | + } |
| 163 | +} |
0 commit comments