Checkboxes.php 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206
  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. * @author Magento Core Team <core@magentocommerce.com>
  12. */
  13. class Checkboxes extends AbstractElement
  14. {
  15. /**
  16. * @param Factory $factoryElement
  17. * @param CollectionFactory $factoryCollection
  18. * @param Escaper $escaper
  19. * @param array $data
  20. */
  21. public function __construct(
  22. Factory $factoryElement,
  23. CollectionFactory $factoryCollection,
  24. Escaper $escaper,
  25. $data = []
  26. ) {
  27. parent::__construct($factoryElement, $factoryCollection, $escaper, $data);
  28. $this->setType('checkbox');
  29. $this->setExtType('checkboxes');
  30. }
  31. /**
  32. * Retrieve allow attributes
  33. *
  34. * @return string[]
  35. */
  36. public function getHtmlAttributes()
  37. {
  38. return [
  39. 'type',
  40. 'name',
  41. 'class',
  42. 'style',
  43. 'checked',
  44. 'onclick',
  45. 'onchange',
  46. 'disabled',
  47. 'data-role',
  48. 'data-action'
  49. ];
  50. }
  51. /**
  52. * Prepare value list
  53. *
  54. * @return array
  55. */
  56. protected function _prepareValues()
  57. {
  58. $options = [];
  59. $values = [];
  60. if ($this->getValues()) {
  61. if (!is_array($this->getValues())) {
  62. $options = [$this->getValues()];
  63. } else {
  64. $options = $this->getValues();
  65. }
  66. } elseif ($this->getOptions() && is_array($this->getOptions())) {
  67. $options = $this->getOptions();
  68. }
  69. foreach ($options as $k => $v) {
  70. if (is_array($v)) {
  71. if (isset($v['value'])) {
  72. if (!isset($v['label'])) {
  73. $v['label'] = $v['value'];
  74. }
  75. $values[] = ['label' => $v['label'], 'value' => $v['value']];
  76. }
  77. } else {
  78. $values[] = ['label' => $v, 'value' => $k];
  79. }
  80. }
  81. return $values;
  82. }
  83. /**
  84. * Retrieve HTML
  85. *
  86. * @return string
  87. */
  88. public function getElementHtml()
  89. {
  90. $values = $this->_prepareValues();
  91. if (!$values) {
  92. return '';
  93. }
  94. $html = '<div class=nested>';
  95. foreach ($values as $value) {
  96. $html .= $this->_optionToHtml($value);
  97. }
  98. $html .= '</div>' . $this->getAfterElementHtml();
  99. return $html;
  100. }
  101. /**
  102. * @param mixed $value
  103. * @return string|void
  104. */
  105. public function getChecked($value)
  106. {
  107. if ($checked = $this->getValue()) {
  108. } elseif ($checked = $this->getData('checked')) {
  109. } else {
  110. return;
  111. }
  112. if (!is_array($checked)) {
  113. $checked = [(string)$checked];
  114. } else {
  115. foreach ($checked as $k => $v) {
  116. $checked[$k] = (string)$v;
  117. }
  118. }
  119. if (in_array((string)$value, $checked)) {
  120. return 'checked';
  121. }
  122. return;
  123. }
  124. /**
  125. * @param mixed $value
  126. * @return string
  127. */
  128. public function getDisabled($value)
  129. {
  130. if ($disabled = $this->getData('disabled')) {
  131. if (!is_array($disabled)) {
  132. $disabled = [(string)$disabled];
  133. } else {
  134. foreach ($disabled as $k => $v) {
  135. $disabled[$k] = (string)$v;
  136. }
  137. }
  138. if (in_array((string)$value, $disabled)) {
  139. return 'disabled';
  140. }
  141. }
  142. return;
  143. }
  144. /**
  145. * @param mixed $value
  146. * @return mixed
  147. */
  148. public function getOnclick($value)
  149. {
  150. if ($onclick = $this->getData('onclick')) {
  151. return str_replace('$value', $value, $onclick);
  152. }
  153. return;
  154. }
  155. /**
  156. * @param mixed $value
  157. * @return mixed
  158. */
  159. public function getOnchange($value)
  160. {
  161. if ($onchange = $this->getData('onchange')) {
  162. return str_replace('$value', $value, $onchange);
  163. }
  164. return;
  165. }
  166. /**
  167. * @param array $option
  168. * @return string
  169. */
  170. protected function _optionToHtml($option)
  171. {
  172. $id = $this->getHtmlId() . '_' . $this->_escape($option['value']);
  173. $html = '<div class="field choice admin__field admin__field-option"><input id="' . $id . '"';
  174. foreach ($this->getHtmlAttributes() as $attribute) {
  175. if ($value = $this->getDataUsingMethod($attribute, $option['value'])) {
  176. $html .= ' ' . $attribute . '="' . $value . '" class="admin__control-checkbox"';
  177. }
  178. }
  179. $html .= ' value="' .
  180. $option['value'] .
  181. '" />' .
  182. ' <label for="' .
  183. $id .
  184. '" class="admin__field-label"><span>' .
  185. $option['label'] .
  186. '</span></label></div>' .
  187. "\n";
  188. return $html;
  189. }
  190. }