Magento 2 Documentation  2.3
Documentation for Magento 2 CMS v2.3 (December 2018)
Button.php
Go to the documentation of this file.
1 <?php
7 
17 {
23  protected function _construct()
24  {
25  $this->setTemplate('Magento_Backend::widget/button.phtml');
26  parent::_construct();
27  }
28 
34  public function getType()
35  {
36  if (in_array($this->getData('type'), ['reset', 'submit'])) {
37  return $this->getData('type');
38  }
39  return 'button';
40  }
41 
47  public function getOnClick()
48  {
49  return $this->getData('on_click') ?: $this->getData('onclick');
50  }
51 
57  public function getAttributesHtml()
58  {
59  $disabled = $this->getDisabled() ? 'disabled' : '';
60  $title = $this->getTitle();
61  if (!$title) {
62  $title = $this->getLabel();
63  }
64  $classes = [];
65  $classes[] = 'action-default';
66  $classes[] = 'scalable';
67  if ($this->getClass()) {
68  $classes[] = $this->getClass();
69  }
70  if ($disabled) {
71  $classes[] = $disabled;
72  }
73 
74  return $this->_attributesToHtml($this->_prepareAttributes($title, $classes, $disabled));
75  }
76 
85  protected function _prepareAttributes($title, $classes, $disabled)
86  {
87  $attributes = [
88  'id' => $this->getId(),
89  'name' => $this->getElementName(),
90  'title' => $title,
91  'type' => $this->getType(),
92  'class' => join(' ', $classes),
93  'onclick' => $this->getOnClick(),
94  'style' => $this->getStyle(),
95  'value' => $this->getValue(),
96  'disabled' => $disabled,
97  ];
98  if ($this->getDataAttribute()) {
99  foreach ($this->getDataAttribute() as $key => $attr) {
100  $attributes['data-' . $key] = is_scalar($attr) ? $attr : json_encode($attr);
101  }
102  }
103  return $attributes;
104  }
105 
112  protected function _attributesToHtml($attributes)
113  {
114  $html = '';
115  foreach ($attributes as $attributeKey => $attributeValue) {
116  if ($attributeValue === null || $attributeValue == '') {
117  continue;
118  }
119  $html .= $attributeKey . '="' . $this->escapeHtmlAttr($attributeValue, false) . '" ';
120  }
121 
122  return $html;
123  }
124 }
$title
Definition: default.phtml:14
getData($key='', $index=null)
Definition: DataObject.php:119
$attr
Definition: text.phtml:8
_prepareAttributes($title, $classes, $disabled)
Definition: Button.php:85
$attributes
Definition: matrix.phtml:13
escapeHtmlAttr($string, $escapeSingleQuote=true)