Magento 2 Documentation  2.3
Documentation for Magento 2 CMS v2.3 (December 2018)
Checkboxes.php
Go to the documentation of this file.
1 <?php
7 
9 
16 {
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('checkbox');
31  $this->setExtType('checkboxes');
32  }
33 
39  public function getHtmlAttributes()
40  {
41  return [
42  'type',
43  'name',
44  'class',
45  'style',
46  'checked',
47  'onclick',
48  'onchange',
49  'disabled',
50  'data-role',
51  'data-action'
52  ];
53  }
54 
60  protected function _prepareValues()
61  {
62  $options = [];
63  $values = [];
64 
65  if ($this->getValues()) {
66  if (!is_array($this->getValues())) {
67  $options = [$this->getValues()];
68  } else {
69  $options = $this->getValues();
70  }
71  } elseif ($this->getOptions() && is_array($this->getOptions())) {
72  $options = $this->getOptions();
73  }
74  foreach ($options as $k => $v) {
75  if (is_array($v)) {
76  if (isset($v['value'])) {
77  if (!isset($v['label'])) {
78  $v['label'] = $v['value'];
79  }
80  $values[] = ['label' => $v['label'], 'value' => $v['value']];
81  }
82  } else {
83  $values[] = ['label' => $v, 'value' => $k];
84  }
85  }
86 
87  return $values;
88  }
89 
95  public function getElementHtml()
96  {
97  $values = $this->_prepareValues();
98 
99  if (!$values) {
100  return '';
101  }
102 
103  $html = '<div class=nested>';
104  foreach ($values as $value) {
105  $html .= $this->_optionToHtml($value);
106  }
107  $html .= '</div>' . $this->getAfterElementHtml();
108 
109  return $html;
110  }
111 
116  public function getChecked($value)
117  {
118  if ($checked = $this->getValue()) {
119  } elseif ($checked = $this->getData('checked')) {
120  } else {
121  return;
122  }
123  if (!is_array($checked)) {
124  $checked = [(string)$checked];
125  } else {
126  foreach ($checked as $k => $v) {
127  $checked[$k] = (string)$v;
128  }
129  }
130  if (in_array((string)$value, $checked)) {
131  return 'checked';
132  }
133  return;
134  }
135 
140  public function getDisabled($value)
141  {
142  if ($disabled = $this->getData('disabled')) {
143  if (!is_array($disabled)) {
144  $disabled = [(string)$disabled];
145  } else {
146  foreach ($disabled as $k => $v) {
147  $disabled[$k] = (string)$v;
148  }
149  }
150  if (in_array((string)$value, $disabled)) {
151  return 'disabled';
152  }
153  }
154  return;
155  }
156 
161  public function getOnclick($value)
162  {
163  if ($onclick = $this->getData('onclick')) {
164  return str_replace('$value', $value, $onclick);
165  }
166  return;
167  }
168 
173  public function getOnchange($value)
174  {
175  if ($onchange = $this->getData('onchange')) {
176  return str_replace('$value', $value, $onchange);
177  }
178  return;
179  }
180 
185  protected function _optionToHtml($option)
186  {
187  $id = $this->getHtmlId() . '_' . $this->_escape($option['value']);
188 
189  $html = '<div class="field choice admin__field admin__field-option"><input id="' . $id . '"';
190  foreach ($this->getHtmlAttributes() as $attribute) {
191  if ($value = $this->getDataUsingMethod($attribute, $option['value'])) {
192  $html .= ' ' . $attribute . '="' . $value . '" class="admin__control-checkbox"';
193  }
194  }
195  $html .= ' value="' .
196  $option['value'] .
197  '" />' .
198  ' <label for="' .
199  $id .
200  '" class="admin__field-label"><span>' .
201  $option['label'] .
202  '</span></label></div>' .
203  "\n";
204  return $html;
205  }
206 }
__construct(Factory $factoryElement, CollectionFactory $factoryCollection, Escaper $escaper, $data=[])
Definition: Checkboxes.php:23
getData($key='', $index=null)
Definition: DataObject.php:119
elseif(isset( $params[ 'redirect_parent']))
Definition: iframe.phtml:17
getDataUsingMethod($key, $args=null)
Definition: DataObject.php:218
$id
Definition: fieldset.phtml:14
$values
Definition: options.phtml:88
$value
Definition: gender.phtml:16
$checked
Definition: billing.phtml:77