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
<?php
/**
* Copyright © Magento, Inc. All rights reserved.
* See COPYING.txt for license details.
*/
namespace Magento\Backend\Block\Widget;
/**
* Button widget
*
* @api
* @author Magento Core Team <core@magentocommerce.com>
* @api
* @since 100.0.2
*/
class Button extends \Magento\Backend\Block\Widget
{
/**
* Define block template
*
* @return void
*/
protected function _construct()
{
$this->setTemplate('Magento_Backend::widget/button.phtml');
parent::_construct();
}
/**
* Retrieve button type
*
* @return string
*/
public function getType()
{
if (in_array($this->getData('type'), ['reset', 'submit'])) {
return $this->getData('type');
}
return 'button';
}
/**
* Retrieve onclick handler
*
* @return null|string
*/
public function getOnClick()
{
return $this->getData('on_click') ?: $this->getData('onclick');
}
/**
* Retrieve attributes html
*
* @return string
*/
public function getAttributesHtml()
{
$disabled = $this->getDisabled() ? 'disabled' : '';
$title = $this->getTitle();
if (!$title) {
$title = $this->getLabel();
}
$classes = [];
$classes[] = 'action-default';
$classes[] = 'scalable';
if ($this->getClass()) {
$classes[] = $this->getClass();
}
if ($disabled) {
$classes[] = $disabled;
}
return $this->_attributesToHtml($this->_prepareAttributes($title, $classes, $disabled));
}
/**
* Prepare attributes
*
* @param string $title
* @param array $classes
* @param string $disabled
* @return array
*/
protected function _prepareAttributes($title, $classes, $disabled)
{
$attributes = [
'id' => $this->getId(),
'name' => $this->getElementName(),
'title' => $title,
'type' => $this->getType(),
'class' => join(' ', $classes),
'onclick' => $this->getOnClick(),
'style' => $this->getStyle(),
'value' => $this->getValue(),
'disabled' => $disabled,
];
if ($this->getDataAttribute()) {
foreach ($this->getDataAttribute() as $key => $attr) {
$attributes['data-' . $key] = is_scalar($attr) ? $attr : json_encode($attr);
}
}
return $attributes;
}
/**
* Attributes list to html
*
* @param array $attributes
* @return string
*/
protected function _attributesToHtml($attributes)
{
$html = '';
foreach ($attributes as $attributeKey => $attributeValue) {
if ($attributeValue === null || $attributeValue == '') {
continue;
}
$html .= $attributeKey . '="' . $this->escapeHtmlAttr($attributeValue, false) . '" ';
}
return $html;
}
}