Select.php 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245
  1. <?php
  2. /**
  3. * Copyright © Magento, Inc. All rights reserved.
  4. * See COPYING.txt for license details.
  5. */
  6. namespace Magento\Framework\View\Element\Html;
  7. /**
  8. * HTML select element block
  9. */
  10. class Select extends \Magento\Framework\View\Element\AbstractBlock
  11. {
  12. /**
  13. * Options
  14. *
  15. * @var array
  16. */
  17. protected $_options = [];
  18. /**
  19. * Get options of the element
  20. *
  21. * @return array
  22. */
  23. public function getOptions()
  24. {
  25. return $this->_options;
  26. }
  27. /**
  28. * Set options for the HTML select
  29. *
  30. * @param array $options
  31. * @return $this
  32. */
  33. public function setOptions($options)
  34. {
  35. $this->_options = $options;
  36. return $this;
  37. }
  38. /**
  39. * Add an option to HTML select
  40. *
  41. * @param string $value HTML value
  42. * @param string $label HTML label
  43. * @param array $params HTML attributes
  44. * @return $this
  45. */
  46. public function addOption($value, $label, $params = [])
  47. {
  48. $this->_options[] = ['value' => $value, 'label' => $label, 'params' => $params];
  49. return $this;
  50. }
  51. /**
  52. * Set element's HTML ID
  53. *
  54. * @param string $elementId ID
  55. * @return $this
  56. */
  57. public function setId($elementId)
  58. {
  59. $this->setData('id', $elementId);
  60. return $this;
  61. }
  62. /**
  63. * Set element's CSS class
  64. *
  65. * @param string $class Class
  66. * @return $this
  67. */
  68. public function setClass($class)
  69. {
  70. $this->setData('class', $class);
  71. return $this;
  72. }
  73. /**
  74. * Set element's HTML title
  75. *
  76. * @param string $title Title
  77. * @return $this
  78. */
  79. public function setTitle($title)
  80. {
  81. $this->setData('title', $title);
  82. return $this;
  83. }
  84. /**
  85. * HTML ID of the element
  86. *
  87. * @return string
  88. */
  89. public function getId()
  90. {
  91. return $this->getData('id');
  92. }
  93. /**
  94. * CSS class of the element
  95. *
  96. * @return string
  97. */
  98. public function getClass()
  99. {
  100. return $this->getData('class');
  101. }
  102. /**
  103. * Returns HTML title of the element
  104. *
  105. * @return string
  106. */
  107. public function getTitle()
  108. {
  109. return $this->getData('title');
  110. }
  111. /**
  112. * Render HTML
  113. *
  114. * @return string
  115. *
  116. * @SuppressWarnings(PHPMD.CyclomaticComplexity)
  117. * @SuppressWarnings(PHPMD.NPathComplexity)
  118. */
  119. protected function _toHtml()
  120. {
  121. if (!$this->_beforeToHtml()) {
  122. return '';
  123. }
  124. $html = '<select name="' .
  125. $this->getName() .
  126. '" id="' .
  127. $this->getId() .
  128. '" class="' .
  129. $this->getClass() .
  130. '" title="' .
  131. $this->escapeHtml($this->getTitle()) .
  132. '" ' .
  133. $this->getExtraParams() .
  134. '>';
  135. $values = $this->getValue();
  136. if (!is_array($values)) {
  137. $values = (array)$values;
  138. }
  139. $isArrayOption = true;
  140. foreach ($this->getOptions() as $key => $option) {
  141. $optgroupName = '';
  142. if ($isArrayOption && is_array($option)) {
  143. $value = $option['value'];
  144. $label = (string)$option['label'];
  145. $optgroupName = isset($option['optgroup-name']) ? $option['optgroup-name'] : $label;
  146. $params = !empty($option['params']) ? $option['params'] : [];
  147. } else {
  148. $value = (string)$key;
  149. $label = (string)$option;
  150. $isArrayOption = false;
  151. $params = [];
  152. }
  153. if (is_array($value)) {
  154. $html .= '<optgroup label="' . $this->escapeHtml($label)
  155. . '" data-optgroup-name="' . $this->escapeHtml($optgroupName) . '">';
  156. foreach ($value as $keyGroup => $optionGroup) {
  157. if (!is_array($optionGroup)) {
  158. $optionGroup = ['value' => $keyGroup, 'label' => $optionGroup];
  159. }
  160. $html .= $this->_optionToHtml($optionGroup, in_array($optionGroup['value'], $values));
  161. }
  162. $html .= '</optgroup>';
  163. } else {
  164. $html .= $this->_optionToHtml(
  165. ['value' => $value, 'label' => $label, 'params' => $params],
  166. in_array($value, $values)
  167. );
  168. }
  169. }
  170. $html .= '</select>';
  171. return $html;
  172. }
  173. /**
  174. * Return option HTML node
  175. *
  176. * @param array $option
  177. * @param boolean $selected
  178. * @return string
  179. */
  180. protected function _optionToHtml($option, $selected = false)
  181. {
  182. $selectedHtml = $selected ? ' selected="selected"' : '';
  183. if ($this->getIsRenderToJsTemplate() === true) {
  184. $selectedHtml .= ' <%= option_extra_attrs.option_' . self::calcOptionHash($option['value']) . ' %>';
  185. }
  186. $params = '';
  187. if (!empty($option['params']) && is_array($option['params'])) {
  188. foreach ($option['params'] as $key => $value) {
  189. if (is_array($value)) {
  190. foreach ($value as $keyMulti => $valueMulti) {
  191. $params .= sprintf(' %s="%s" ', $keyMulti, $this->escapeHtml($valueMulti));
  192. }
  193. } else {
  194. $params .= sprintf(' %s="%s" ', $key, $this->escapeHtml($value));
  195. }
  196. }
  197. }
  198. return sprintf(
  199. '<option value="%s"%s %s>%s</option>',
  200. $this->escapeHtml($option['value']),
  201. $selectedHtml,
  202. $params,
  203. $this->escapeHtml($option['label'])
  204. );
  205. }
  206. /**
  207. * Alias for toHtml()
  208. *
  209. * @return string
  210. */
  211. public function getHtml()
  212. {
  213. return $this->toHtml();
  214. }
  215. /**
  216. * Calculate CRC32 hash for option value
  217. *
  218. * @param string $optionValue Value of the option
  219. * @return string
  220. */
  221. public function calcOptionHash($optionValue)
  222. {
  223. return sprintf('%u', crc32($this->getName() . $this->getId() . $optionValue));
  224. }
  225. }