Magento 2 Documentation  2.3
Documentation for Magento 2 CMS v2.3 (December 2018)
SplitButton.php
Go to the documentation of this file.
1 <?php
7 
23 {
29  protected function _construct()
30  {
31  if (!$this->hasTemplate()) {
32  $this->setTemplate('Magento_Backend::widget/button/split.phtml');
33  }
34  parent::_construct();
35  }
36 
42  public function getAttributesHtml()
43  {
44  $title = $this->getTitle();
45  if (!$title) {
46  $title = $this->getLabel();
47  }
48  $classes = [];
49  if ($this->hasSplit()) {
50  $classes[] = 'actions-split';
51  }
52  //@TODO Perhaps use $this->getClass() instead
53  if ($this->getButtonClass()) {
54  $classes[] = $this->getButtonClass();
55  }
56 
57  $attributes = ['id' => $this->getId(), 'title' => $title, 'class' => join(' ', $classes)];
58 
59  $html = $this->_getAttributesString($attributes);
60 
61  return $html;
62  }
63 
69  public function getButtonAttributesHtml()
70  {
71  $disabled = $this->getDisabled() ? 'disabled' : '';
72  $title = $this->getTitle();
73  if (!$title) {
74  $title = $this->getLabel();
75  }
76  $classes = [];
77  $classes[] = 'action-default';
78  $classes[] = 'primary';
79  // @TODO Perhaps use $this->getButtonClass() instead
80  if ($this->getClass()) {
81  $classes[] = $this->getClass();
82  }
83  if ($disabled) {
84  $classes[] = $disabled;
85  }
86  $attributes = [
87  'id' => $this->getId() . '-button',
88  'title' => $title,
89  'class' => join(' ', $classes),
90  'disabled' => $disabled,
91  'style' => $this->getStyle(),
92  ];
93 
94  //TODO perhaps we need to skip data-mage-init when disabled="disabled"
95  if ($this->getDataAttribute()) {
96  $this->_getDataAttributes($this->getDataAttribute(), $attributes);
97  }
98 
99  $html = $this->_getAttributesString($attributes);
100  $html .= $this->getUiId();
101 
102  return $html;
103  }
104 
110  public function getToggleAttributesHtml()
111  {
112  $disabled = $this->getDisabled() ? 'disabled' : '';
113  $title = $this->getTitle();
114  if (!$title) {
115  $title = $this->getLabel();
116  }
117  $classes = [];
118  $classes[] = 'action-toggle';
119  $classes[] = 'primary';
120  if ($this->getClass()) {
121  $classes[] = $this->getClass();
122  }
123  if ($disabled) {
124  $classes[] = $disabled;
125  }
126 
127  $attributes = ['title' => $title, 'class' => join(' ', $classes), 'disabled' => $disabled];
128  $this->_getDataAttributes(['mage-init' => '{"dropdown": {}}', 'toggle' => 'dropdown'], $attributes);
129 
130  $html = $this->_getAttributesString($attributes);
131  $html .= $this->getUiId('dropdown');
132 
133  return $html;
134  }
135 
144  public function getOptionAttributesHtml($key, $option)
145  {
146  $disabled = isset($option['disabled']) && $option['disabled'] ? 'disabled' : '';
147  if (isset($option['title'])) {
148  $title = $option['title'];
149  } else {
150  $title = $option['label'];
151  }
152  $classes = [];
153  $classes[] = 'item';
154  if (!empty($option['default'])) {
155  $classes[] = 'item-default';
156  }
157  if ($disabled) {
158  $classes[] = $disabled;
159  }
160  $attributes = $this->_prepareOptionAttributes($option, $title, $classes, $disabled);
161  $html = $this->_getAttributesString($attributes);
162  $html .= $this->getUiId(isset($option['id']) ? $option['id'] : 'item' . '-' . $key);
163 
164  return $html;
165  }
166 
174  public function hasSplit()
175  {
176  return $this->hasData('has_split') ? (bool)$this->getData('has_split') : true;
177  }
178 
186  protected function _getDataAttributes($data, &$attributes)
187  {
188  foreach ($data as $key => $attr) {
189  $attributes['data-' . $key] = is_scalar($attr) ? $attr : json_encode($attr);
190  }
191  }
192 
203  protected function _prepareOptionAttributes($option, $title, $classes, $disabled)
204  {
205  $attributes = [
206  'id' => isset($option['id']) ? $this->getId() . '-' . $option['id'] : '',
207  'title' => $title,
208  'class' => join(' ', $classes),
209  'onclick' => isset($option['onclick']) ? $option['onclick'] : '',
210  'style' => isset($option['style']) ? $option['style'] : '',
211  'disabled' => $disabled,
212  ];
213 
214  if (isset($option['data_attribute'])) {
215  $this->_getDataAttributes($option['data_attribute'], $attributes);
216  }
217 
218  return $attributes;
219  }
220 
227  protected function _getAttributesString($attributes)
228  {
229  $html = [];
230  foreach ($attributes as $attributeKey => $attributeValue) {
231  if ($attributeValue === null || $attributeValue == '') {
232  continue;
233  }
234  $html[] = $attributeKey . '="' . $this->escapeHtmlAttr($attributeValue, false) . '"';
235  }
236  return join(' ', $html);
237  }
238 }
_prepareOptionAttributes($option, $title, $classes, $disabled)
$title
Definition: default.phtml:14
getData($key='', $index=null)
Definition: DataObject.php:119
getUiId($arg1=null, $arg2=null, $arg3=null, $arg4=null, $arg5=null)
$attr
Definition: text.phtml:8
$attributes
Definition: matrix.phtml:13
escapeHtmlAttr($string, $escapeSingleQuote=true)