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 
22 class SplitButton extends Button
23 {
27  protected function getTemplatePath()
28  {
29  return 'Magento_Ui::control/button/split.phtml';
30  }
31 
37  public function getAttributesHtml()
38  {
39  $classes = [];
40 
41  if (!($title = $this->getTitle())) {
42  $title = $this->getLabel();
43  }
44 
45  if ($this->hasSplit()) {
46  $classes[] = 'actions-split';
47  }
48 
49  if ($this->getClass()) {
50  $classes[] = $this->getClass();
51  }
52 
53  return $this->attributesToHtml(['title' => $title, 'class' => join(' ', $classes)]);
54  }
55 
61  public function getButtonAttributesHtml()
62  {
63  $disabled = $this->getDisabled() ? 'disabled' : '';
64  $classes = ['action-default', 'primary'];
65 
66  if (!($title = $this->getTitle())) {
67  $title = $this->getLabel();
68  }
69 
70  if ($this->getButtonClass()) {
71  $classes[] = $this->getButtonClass();
72  }
73 
74  if ($disabled) {
75  $classes[] = $disabled;
76  }
77 
78  $attributes = [
79  'id' => $this->getId() . '-button',
80  'title' => $title,
81  'class' => join(' ', $classes),
82  'disabled' => $disabled,
83  'style' => $this->getStyle(),
84  ];
85 
86  if (($idHard = $this->getIdHard())) {
87  $attributes['id'] = $idHard;
88  }
89 
90  //TODO perhaps we need to skip data-mage-init when disabled="disabled"
91  if (($dataAttribute = $this->getDataAttribute())) {
92  $this->getDataAttributes($dataAttribute, $attributes);
93  }
94 
95  $html = $this->attributesToHtml($attributes);
96  $html .= $this->getUiId();
97 
98  return $html;
99  }
100 
106  public function getToggleAttributesHtml()
107  {
108  $disabled = $this->getDisabled() ? 'disabled' : '';
109  $classes = ['action-toggle', 'primary'];
110 
111  if (!($title = $this->getTitle())) {
112  $title = $this->getLabel();
113  }
114 
115  if (($currentClass = $this->getClass())) {
116  $classes[] = $currentClass;
117  }
118 
119  if ($disabled) {
120  $classes[] = $disabled;
121  }
122 
123  $attributes = ['title' => $title, 'class' => join(' ', $classes), 'disabled' => $disabled];
124  $this->getDataAttributes(['mage-init' => '{"dropdown": {}}', 'toggle' => 'dropdown'], $attributes);
125 
126  $html = $this->attributesToHtml($attributes);
127  $html .= $this->getUiId('dropdown');
128 
129  return $html;
130  }
131 
140  public function getOptionAttributesHtml($key, $option)
141  {
142  $disabled = !empty($option['disabled']) ? 'disabled' : '';
143  $title = isset($option['title']) ? $option['title'] : $option['label'];
144  $classes = ['item'];
145 
146  if (!empty($option['default'])) {
147  $classes[] = 'item-default';
148  }
149 
150  if ($disabled) {
151  $classes[] = $disabled;
152  }
153 
154  $attributes = $this->prepareOptionAttributes($option, $title, $classes, $disabled);
155  $html = $this->attributesToHtml($attributes);
156  $html .= $this->getUiId(isset($option['id']) ? $option['id'] : 'item' . '-' . $key);
157 
158  return $html;
159  }
160 
171  protected function prepareOptionAttributes($option, $title, $classes, $disabled)
172  {
173  $attributes = [
174  'id' => isset($option['id']) ? $this->getId() . '-' . $option['id'] : '',
175  'title' => $title,
176  'class' => join(' ', $classes),
177  'onclick' => isset($option['onclick']) ? $option['onclick'] : '',
178  'style' => isset($option['style']) ? $option['style'] : '',
179  'disabled' => $disabled,
180  ];
181 
182  if (!empty($option['id_hard'])) {
183  $attributes['id'] = $option['id_hard'];
184  }
185 
186  if (isset($option['data_attribute'])) {
187  $this->getDataAttributes($option['data_attribute'], $attributes);
188  }
189 
190  return $attributes;
191  }
192 
200  public function hasSplit()
201  {
202  return $this->hasData('has_split') ? (bool)$this->getData('has_split') : true;
203  }
204 
212  protected function getDataAttributes($data, &$attributes)
213  {
214  foreach ($data as $key => $attr) {
215  $attributes['data-' . $key] = is_scalar($attr) ? $attr : json_encode($attr);
216  }
217  }
218 }
$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
prepareOptionAttributes($option, $title, $classes, $disabled)