Repository navigation
Expand file tree
/
Copy pathListWidget.php
More file actions
92 lines (73 loc) · 2.23 KB
/
Copy pathListWidget.php
File metadata and controls
92 lines (73 loc) · 2.23 KB
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
<?php
namespace soluto\plugin;
use yii\base\InvalidConfigException;
use yii\helpers\Html;
class ListWidget extends Widget
{
/**
* @var string the name of the breadcrumb container tag.
*/
public $tag = 'ul';
/**
* @var string the template used to render each inactive . The token `{link}`
* will be replaced with the actual HTML link for each inactive item.
*/
public $itemTemplate = "<li>{content}</li>\n";
/**
* @var array $items the option data items.
*/
public $items = [];
/**
* @inheritdoc
*/
public function run()
{
$id = $this->getId();
echo $this->renderList($this->items, $id);
$this->registerPlugin($id);
}
/**
* Renders the list elements
* @param array $items
* @param string $id
* @return string
*/
protected function renderList($items, $id = null)
{
$list = [];
foreach ($items as $item) {
if (!is_array($item)) {
$item = ['label' => $item];
}
if (isset($item['items'])) {
$item['content'] = $this->renderList($item['items']);
}
$list[] = $this->renderItem($item);
}
$options = ($id !== null) ? ['id' => $id] : [];
return Html::tag($this->tag, implode('', $list), $options);
}
/**
* Renders a single item.
* @param array $item the item to be rendered
* @param string $template the template to be used to rendered the item. The token "{content}" will be replaced by the content.
* @return string the rendering result
* @throws InvalidConfigException if `$item` does not have "label" element.
*/
protected function renderItem($item)
{
$template = $this->itemTemplate;
if (array_key_exists('label', $item)) {
$content = $item['label'];
} else {
throw new InvalidConfigException('The "label" element is required.');
}
if (isset($item['content'])) {
$content .= $item['content'];
}
if (isset($content['template'])) {
$template = $content['template'];
}
return strtr($template, ['{content}' => $content]);
}
}