Select.php 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162
  1. <?php
  2. /**
  3. * Copyright © Magento, Inc. All rights reserved.
  4. * See COPYING.txt for license details.
  5. */
  6. namespace Magento\Framework\Data\Form\Element;
  7. use Magento\Framework\Escaper;
  8. /**
  9. * Form select element
  10. *
  11. * @api
  12. * @author Magento Core Team <core@magentocommerce.com>
  13. * @since 100.0.2
  14. */
  15. class Select extends AbstractElement
  16. {
  17. /**
  18. * @param Factory $factoryElement
  19. * @param CollectionFactory $factoryCollection
  20. * @param Escaper $escaper
  21. * @param array $data
  22. */
  23. public function __construct(
  24. Factory $factoryElement,
  25. CollectionFactory $factoryCollection,
  26. Escaper $escaper,
  27. $data = []
  28. ) {
  29. parent::__construct($factoryElement, $factoryCollection, $escaper, $data);
  30. $this->setType('select');
  31. $this->setExtType('combobox');
  32. $this->_prepareOptions();
  33. }
  34. /**
  35. * Get the element Html.
  36. *
  37. * @return string
  38. */
  39. public function getElementHtml()
  40. {
  41. $this->addClass('select admin__control-select');
  42. $html = '';
  43. if ($this->getBeforeElementHtml()) {
  44. $html .= '<label class="addbefore" for="' .
  45. $this->getHtmlId() .
  46. '">' .
  47. $this->getBeforeElementHtml() .
  48. '</label>';
  49. }
  50. $html .= '<select id="' . $this->getHtmlId() . '" name="' . $this->getName() . '" ' . $this->serialize(
  51. $this->getHtmlAttributes()
  52. ) . $this->_getUiId() . '>' . "\n";
  53. $value = $this->getValue();
  54. if (!is_array($value)) {
  55. $value = [$value];
  56. }
  57. if ($values = $this->getValues()) {
  58. foreach ($values as $key => $option) {
  59. if (!is_array($option)) {
  60. $html .= $this->_optionToHtml(['value' => $key, 'label' => $option], $value);
  61. } elseif (is_array($option['value'])) {
  62. $html .= '<optgroup label="' . $option['label'] . '">' . "\n";
  63. foreach ($option['value'] as $groupItem) {
  64. $html .= $this->_optionToHtml($groupItem, $value);
  65. }
  66. $html .= '</optgroup>' . "\n";
  67. } else {
  68. $html .= $this->_optionToHtml($option, $value);
  69. }
  70. }
  71. }
  72. $html .= '</select>' . "\n";
  73. if ($this->getAfterElementHtml()) {
  74. $html .= '<label class="addafter" for="' .
  75. $this->getHtmlId() .
  76. '">' .
  77. "\n{$this->getAfterElementHtml()}\n" .
  78. '</label>' .
  79. "\n";
  80. }
  81. return $html;
  82. }
  83. /**
  84. * Format an option as Html
  85. *
  86. * @param array $option
  87. * @param array $selected
  88. * @return string
  89. */
  90. protected function _optionToHtml($option, $selected)
  91. {
  92. if (is_array($option['value'])) {
  93. $html = '<optgroup label="' . $option['label'] . '">' . "\n";
  94. foreach ($option['value'] as $groupItem) {
  95. $html .= $this->_optionToHtml($groupItem, $selected);
  96. }
  97. $html .= '</optgroup>' . "\n";
  98. } else {
  99. $html = '<option value="' . $this->_escape($option['value']) . '"';
  100. $html .= isset($option['title']) ? 'title="' . $this->_escape($option['title']) . '"' : '';
  101. $html .= isset($option['style']) ? 'style="' . $option['style'] . '"' : '';
  102. if (in_array($option['value'], $selected)) {
  103. $html .= ' selected="selected"';
  104. }
  105. $html .= '>' . $this->_escape($option['label']) . '</option>' . "\n";
  106. }
  107. return $html;
  108. }
  109. /**
  110. * Prepare options.
  111. *
  112. * @return void
  113. */
  114. protected function _prepareOptions()
  115. {
  116. $values = $this->getValues();
  117. if (empty($values)) {
  118. $options = $this->getOptions();
  119. if (is_array($options)) {
  120. $values = [];
  121. foreach ($options as $value => $label) {
  122. $values[] = ['value' => $value, 'label' => $label];
  123. }
  124. } elseif (is_string($options)) {
  125. $values = [['value' => $options, 'label' => $options]];
  126. }
  127. $this->setValues($values);
  128. }
  129. }
  130. /**
  131. * Get the Html attributes.
  132. *
  133. * @return string[]
  134. */
  135. public function getHtmlAttributes()
  136. {
  137. return [
  138. 'title',
  139. 'class',
  140. 'style',
  141. 'onclick',
  142. 'onchange',
  143. 'disabled',
  144. 'readonly',
  145. 'tabindex',
  146. 'data-form-part',
  147. 'data-role',
  148. 'data-action'
  149. ];
  150. }
  151. }