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
<?php
/**
* Backend container block
*
* Copyright © Magento, Inc. All rights reserved.
* See COPYING.txt for license details.
*/
namespace Magento\Backend\Block\Widget;
use Magento\Framework\View\Element\Template;
/**
* @api
* @since 100.0.2
*/
class Container extends \Magento\Backend\Block\Template implements ContainerInterface
{
/**#@+
* Initialization parameters in pseudo-constructor
*/
const PARAM_CONTROLLER = 'controller';
const PARAM_HEADER_TEXT = 'header_text';
/**#@-*/
/**#@-*/
protected $_controller = 'empty';
/**
* Header text
*
* @var string
*/
protected $_headerText = 'Container Widget Header';
/**
* @var \Magento\Backend\Block\Widget\Button\ButtonList
*/
protected $buttonList;
/**
* @var Button\ToolbarInterface
*/
protected $toolbar;
/**
* @param Context $context
* @param array $data
*/
public function __construct(\Magento\Backend\Block\Widget\Context $context, array $data = [])
{
$this->buttonList = $context->getButtonList();
$this->toolbar = $context->getButtonToolbar();
parent::__construct($context, $data);
}
/**
* Initialize "controller" and "header text"
*
* @return void
*/
protected function _construct()
{
parent::_construct();
if ($this->hasData(self::PARAM_CONTROLLER)) {
$this->_controller = $this->_getData(self::PARAM_CONTROLLER);
}
if ($this->hasData(self::PARAM_HEADER_TEXT)) {
$this->_headerText = $this->_getData(self::PARAM_HEADER_TEXT);
}
}
/**
* Public wrapper for the button list
*
* @param string $buttonId
* @param array $data
* @param integer $level
* @param integer $sortOrder
* @param string|null $region That button should be displayed in ('toolbar', 'header', 'footer', null)
* @return $this
*/
public function addButton($buttonId, $data, $level = 0, $sortOrder = 0, $region = 'toolbar')
{
$this->buttonList->add($buttonId, $data, $level, $sortOrder, $region);
return $this;
}
/**
* Public wrapper for the button list
*
* @param string $buttonId
* @return $this
*/
public function removeButton($buttonId)
{
$this->buttonList->remove($buttonId);
return $this;
}
/**
* Public wrapper for protected _updateButton method
*
* @param string $buttonId
* @param string|null $key
* @param string $data
* @return $this
*/
public function updateButton($buttonId, $key, $data)
{
$this->buttonList->update($buttonId, $key, $data);
return $this;
}
/**
* Preparing child blocks for each added button
*
* @return $this
*/
protected function _prepareLayout()
{
$this->toolbar->pushButtons($this, $this->buttonList);
return parent::_prepareLayout();
}
/**
* Produce buttons HTML
*
* @param string $region
* @return string
*/
public function getButtonsHtml($region = null)
{
$out = '';
foreach ($this->buttonList->getItems() as $buttons) {
/** @var \Magento\Backend\Block\Widget\Button\Item $item */
foreach ($buttons as $item) {
if ($region && $region != $item->getRegion()) {
continue;
}
$out .= $this->getChildHtml($item->getButtonKey());
}
}
return $out;
}
/**
* Get header text
*
* @return string
*/
public function getHeaderText()
{
return $this->_headerText;
}
/**
* Get header CSS class
*
* @return string
*/
public function getHeaderCssClass()
{
return 'head-' . strtr($this->_controller, '_', '-');
}
/**
* Get header HTML
*
* @return string
*/
public function getHeaderHtml()
{
return '<h3 class="' . $this->getHeaderCssClass() . '">' . $this->getHeaderText() . '</h3>';
}
/**
* Check if there's anything to display in footer
*
* @return boolean
*/
public function hasFooterButtons()
{
foreach ($this->buttonList->getItems() as $buttons) {
foreach ($buttons as $data) {
if (isset($data['region']) && 'footer' == $data['region']) {
return true;
}
}
}
return false;
}
/**
* Check whether button rendering is allowed in current context
*
* @param \Magento\Backend\Block\Widget\Button\Item $item
* @return bool
*/
public function canRender(Button\Item $item)
{
return !$item->isDeleted();
}
}