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
<?php
/**
* Copyright © Magento, Inc. All rights reserved.
* See COPYING.txt for license details.
*/
namespace Magento\Framework\Data\Form\Element;
use Magento\Framework\Escaper;
/**
* Form select element
*
* @api
* @author Magento Core Team <core@magentocommerce.com>
* @since 100.0.2
*/
class Select extends AbstractElement
{
/**
* @param Factory $factoryElement
* @param CollectionFactory $factoryCollection
* @param Escaper $escaper
* @param array $data
*/
public function __construct(
Factory $factoryElement,
CollectionFactory $factoryCollection,
Escaper $escaper,
$data = []
) {
parent::__construct($factoryElement, $factoryCollection, $escaper, $data);
$this->setType('select');
$this->setExtType('combobox');
$this->_prepareOptions();
}
/**
* Get the element Html.
*
* @return string
*/
public function getElementHtml()
{
$this->addClass('select admin__control-select');
$html = '';
if ($this->getBeforeElementHtml()) {
$html .= '<label class="addbefore" for="' .
$this->getHtmlId() .
'">' .
$this->getBeforeElementHtml() .
'</label>';
}
$html .= '<select id="' . $this->getHtmlId() . '" name="' . $this->getName() . '" ' . $this->serialize(
$this->getHtmlAttributes()
) . $this->_getUiId() . '>' . "\n";
$value = $this->getValue();
if (!is_array($value)) {
$value = [$value];
}
if ($values = $this->getValues()) {
foreach ($values as $key => $option) {
if (!is_array($option)) {
$html .= $this->_optionToHtml(['value' => $key, 'label' => $option], $value);
} elseif (is_array($option['value'])) {
$html .= '<optgroup label="' . $option['label'] . '">' . "\n";
foreach ($option['value'] as $groupItem) {
$html .= $this->_optionToHtml($groupItem, $value);
}
$html .= '</optgroup>' . "\n";
} else {
$html .= $this->_optionToHtml($option, $value);
}
}
}
$html .= '</select>' . "\n";
if ($this->getAfterElementHtml()) {
$html .= '<label class="addafter" for="' .
$this->getHtmlId() .
'">' .
"\n{$this->getAfterElementHtml()}\n" .
'</label>' .
"\n";
}
return $html;
}
/**
* Format an option as Html
*
* @param array $option
* @param array $selected
* @return string
*/
protected function _optionToHtml($option, $selected)
{
if (is_array($option['value'])) {
$html = '<optgroup label="' . $option['label'] . '">' . "\n";
foreach ($option['value'] as $groupItem) {
$html .= $this->_optionToHtml($groupItem, $selected);
}
$html .= '</optgroup>' . "\n";
} else {
$html = '<option value="' . $this->_escape($option['value']) . '"';
$html .= isset($option['title']) ? 'title="' . $this->_escape($option['title']) . '"' : '';
$html .= isset($option['style']) ? 'style="' . $option['style'] . '"' : '';
if (in_array($option['value'], $selected)) {
$html .= ' selected="selected"';
}
$html .= '>' . $this->_escape($option['label']) . '</option>' . "\n";
}
return $html;
}
/**
* Prepare options.
*
* @return void
*/
protected function _prepareOptions()
{
$values = $this->getValues();
if (empty($values)) {
$options = $this->getOptions();
if (is_array($options)) {
$values = [];
foreach ($options as $value => $label) {
$values[] = ['value' => $value, 'label' => $label];
}
} elseif (is_string($options)) {
$values = [['value' => $options, 'label' => $options]];
}
$this->setValues($values);
}
}
/**
* Get the Html attributes.
*
* @return string[]
*/
public function getHtmlAttributes()
{
return [
'title',
'class',
'style',
'onclick',
'onchange',
'disabled',
'readonly',
'tabindex',
'data-form-part',
'data-role',
'data-action'
];
}
}