123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206 |
- <?php
- /**
- * Copyright © Magento, Inc. All rights reserved.
- * See COPYING.txt for license details.
- */
- namespace Magento\Framework\Data\Form\Element;
- use Magento\Framework\Escaper;
- /**
- * Form select element
- *
- * @author Magento Core Team <core@magentocommerce.com>
- */
- class Checkboxes 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('checkbox');
- $this->setExtType('checkboxes');
- }
- /**
- * Retrieve allow attributes
- *
- * @return string[]
- */
- public function getHtmlAttributes()
- {
- return [
- 'type',
- 'name',
- 'class',
- 'style',
- 'checked',
- 'onclick',
- 'onchange',
- 'disabled',
- 'data-role',
- 'data-action'
- ];
- }
- /**
- * Prepare value list
- *
- * @return array
- */
- protected function _prepareValues()
- {
- $options = [];
- $values = [];
- if ($this->getValues()) {
- if (!is_array($this->getValues())) {
- $options = [$this->getValues()];
- } else {
- $options = $this->getValues();
- }
- } elseif ($this->getOptions() && is_array($this->getOptions())) {
- $options = $this->getOptions();
- }
- foreach ($options as $k => $v) {
- if (is_array($v)) {
- if (isset($v['value'])) {
- if (!isset($v['label'])) {
- $v['label'] = $v['value'];
- }
- $values[] = ['label' => $v['label'], 'value' => $v['value']];
- }
- } else {
- $values[] = ['label' => $v, 'value' => $k];
- }
- }
- return $values;
- }
- /**
- * Retrieve HTML
- *
- * @return string
- */
- public function getElementHtml()
- {
- $values = $this->_prepareValues();
- if (!$values) {
- return '';
- }
- $html = '<div class=nested>';
- foreach ($values as $value) {
- $html .= $this->_optionToHtml($value);
- }
- $html .= '</div>' . $this->getAfterElementHtml();
- return $html;
- }
- /**
- * @param mixed $value
- * @return string|void
- */
- public function getChecked($value)
- {
- if ($checked = $this->getValue()) {
- } elseif ($checked = $this->getData('checked')) {
- } else {
- return;
- }
- if (!is_array($checked)) {
- $checked = [(string)$checked];
- } else {
- foreach ($checked as $k => $v) {
- $checked[$k] = (string)$v;
- }
- }
- if (in_array((string)$value, $checked)) {
- return 'checked';
- }
- return;
- }
- /**
- * @param mixed $value
- * @return string
- */
- public function getDisabled($value)
- {
- if ($disabled = $this->getData('disabled')) {
- if (!is_array($disabled)) {
- $disabled = [(string)$disabled];
- } else {
- foreach ($disabled as $k => $v) {
- $disabled[$k] = (string)$v;
- }
- }
- if (in_array((string)$value, $disabled)) {
- return 'disabled';
- }
- }
- return;
- }
- /**
- * @param mixed $value
- * @return mixed
- */
- public function getOnclick($value)
- {
- if ($onclick = $this->getData('onclick')) {
- return str_replace('$value', $value, $onclick);
- }
- return;
- }
- /**
- * @param mixed $value
- * @return mixed
- */
- public function getOnchange($value)
- {
- if ($onchange = $this->getData('onchange')) {
- return str_replace('$value', $value, $onchange);
- }
- return;
- }
- /**
- * @param array $option
- * @return string
- */
- protected function _optionToHtml($option)
- {
- $id = $this->getHtmlId() . '_' . $this->_escape($option['value']);
- $html = '<div class="field choice admin__field admin__field-option"><input id="' . $id . '"';
- foreach ($this->getHtmlAttributes() as $attribute) {
- if ($value = $this->getDataUsingMethod($attribute, $option['value'])) {
- $html .= ' ' . $attribute . '="' . $value . '" class="admin__control-checkbox"';
- }
- }
- $html .= ' value="' .
- $option['value'] .
- '" />' .
- ' <label for="' .
- $id .
- '" class="admin__field-label"><span>' .
- $option['label'] .
- '</span></label></div>' .
- "\n";
- return $html;
- }
- }
|