Magento 2 Documentation  2.3
Documentation for Magento 2 CMS v2.3 (December 2018)
Select.php
Go to the documentation of this file.
1 <?php
7 
9 
17 class Select extends AbstractElement
18 {
25  public function __construct(
26  Factory $factoryElement,
27  CollectionFactory $factoryCollection,
28  Escaper $escaper,
29  $data = []
30  ) {
31  parent::__construct($factoryElement, $factoryCollection, $escaper, $data);
32  $this->setType('select');
33  $this->setExtType('combobox');
34  $this->_prepareOptions();
35  }
36 
42  public function getElementHtml()
43  {
44  $this->addClass('select admin__control-select');
45 
46  $html = '';
47  if ($this->getBeforeElementHtml()) {
48  $html .= '<label class="addbefore" for="' .
49  $this->getHtmlId() .
50  '">' .
51  $this->getBeforeElementHtml() .
52  '</label>';
53  }
54 
55  $html .= '<select id="' . $this->getHtmlId() . '" name="' . $this->getName() . '" ' . $this->serialize(
56  $this->getHtmlAttributes()
57  ) . $this->_getUiId() . '>' . "\n";
58 
59  $value = $this->getValue();
60  if (!is_array($value)) {
61  $value = [$value];
62  }
63 
64  if ($values = $this->getValues()) {
65  foreach ($values as $key => $option) {
66  if (!is_array($option)) {
67  $html .= $this->_optionToHtml(['value' => $key, 'label' => $option], $value);
68  } elseif (is_array($option['value'])) {
69  $html .= '<optgroup label="' . $option['label'] . '">' . "\n";
70  foreach ($option['value'] as $groupItem) {
71  $html .= $this->_optionToHtml($groupItem, $value);
72  }
73  $html .= '</optgroup>' . "\n";
74  } else {
75  $html .= $this->_optionToHtml($option, $value);
76  }
77  }
78  }
79 
80  $html .= '</select>' . "\n";
81  if ($this->getAfterElementHtml()) {
82  $html .= '<label class="addafter" for="' .
83  $this->getHtmlId() .
84  '">' .
85  "\n{$this->getAfterElementHtml()}\n" .
86  '</label>' .
87  "\n";
88  }
89  return $html;
90  }
91 
99  protected function _optionToHtml($option, $selected)
100  {
101  if (is_array($option['value'])) {
102  $html = '<optgroup label="' . $option['label'] . '">' . "\n";
103  foreach ($option['value'] as $groupItem) {
104  $html .= $this->_optionToHtml($groupItem, $selected);
105  }
106  $html .= '</optgroup>' . "\n";
107  } else {
108  $html = '<option value="' . $this->_escape($option['value']) . '"';
109  $html .= isset($option['title']) ? 'title="' . $this->_escape($option['title']) . '"' : '';
110  $html .= isset($option['style']) ? 'style="' . $option['style'] . '"' : '';
111  if (in_array($option['value'], $selected)) {
112  $html .= ' selected="selected"';
113  }
114  $html .= '>' . $this->_escape($option['label']) . '</option>' . "\n";
115  }
116  return $html;
117  }
118 
124  protected function _prepareOptions()
125  {
126  $values = $this->getValues();
127  if (empty($values)) {
128  $options = $this->getOptions();
129  if (is_array($options)) {
130  $values = [];
131  foreach ($options as $value => $label) {
132  $values[] = ['value' => $value, 'label' => $label];
133  }
134  } elseif (is_string($options)) {
135  $values = [['value' => $options, 'label' => $options]];
136  }
137  $this->setValues($values);
138  }
139  }
140 
146  public function getHtmlAttributes()
147  {
148  return [
149  'title',
150  'class',
151  'style',
152  'onclick',
153  'onchange',
154  'disabled',
155  'readonly',
156  'tabindex',
157  'data-form-part',
158  'data-role',
159  'data-action'
160  ];
161  }
162 }
elseif(isset( $params[ 'redirect_parent']))
Definition: iframe.phtml:17
$values
Definition: options.phtml:88
__construct(Factory $factoryElement, CollectionFactory $factoryCollection, Escaper $escaper, $data=[])
Definition: Select.php:25
$label
Definition: details.phtml:21
$value
Definition: gender.phtml:16
serialize($attributes=[], $valueSeparator='=', $fieldSeparator=' ', $quote='"')