*/
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 .= '';
}
$html .= '' . "\n";
if ($this->getAfterElementHtml()) {
$html .= '' .
"\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 = '' . "\n";
} else {
$html = '' . "\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'
];
}
}