‪TYPO3CMS  9.5
CheckboxElement.php
Go to the documentation of this file.
1 <?php
2 declare(strict_types = 1);
4 
5 /*
6  * This file is part of the TYPO3 CMS project.
7  *
8  * It is free software; you can redistribute it and/or modify it under
9  * the terms of the GNU General Public License, either version 2
10  * of the License, or any later version.
11  *
12  * For the full copyright and license information, please read the
13  * LICENSE.txt file that was distributed with this source code.
14  *
15  * The TYPO3 project - inspiring people to share!
16  */
17 
23 
28 {
32  private ‪$iconRegistry;
33 
39  protected ‪$defaultFieldInformation = [
40  'tcaDescription' => [
41  'renderType' => 'tcaDescription',
42  ],
43  ];
44 
50  protected ‪$defaultFieldWizard = [
51  'localizationStateSelector' => [
52  'renderType' => 'localizationStateSelector',
53  ],
54  'otherLanguageContent' => [
55  'renderType' => 'otherLanguageContent',
56  'after' => [
57  'localizationStateSelector'
58  ],
59  ],
60  'defaultLanguageDifferences' => [
61  'renderType' => 'defaultLanguageDifferences',
62  'after' => [
63  'otherLanguageContent',
64  ],
65  ],
66  ];
67 
73  {
74  parent::__construct(‪$nodeFactory, ‪$data);
75  $this->iconRegistry = GeneralUtility::makeInstance(IconRegistry::class);
76  }
77 
83  public function ‪render(): array
84  {
85  $resultArray = $this->‪initializeResultArray();
86 
87  $elementHtml = '';
88  $disabled = false;
89  if ($this->data['parameterArray']['fieldConf']['config']['readOnly']) {
90  $disabled = true;
91  }
92  // Traversing the array of items
93  $items = $this->data['parameterArray']['fieldConf']['config']['items'];
94 
95  $numberOfItems = \count($items);
96  if ($numberOfItems === 0) {
97  $items[] = ['', ''];
98  $numberOfItems = 1;
99  }
100  $formElementValue = (int)$this->data['parameterArray']['itemFormElValue'];
101  $cols = (int)$this->data['parameterArray']['fieldConf']['config']['cols'];
102  if ($cols > 1) {
103  [$colClass, $colClear] = $this->‪calculateColumnMarkup($cols);
104  $elementHtml .= '<div class="checkbox-row row">';
105  $counter = 0;
106  // $itemKey is important here, because items could have been removed via TSConfig
107  foreach ($items as $itemKey => $itemDefinition) {
108  $label = $itemDefinition[0];
109  $elementHtml .=
110  '<div class="checkbox-column ' . $colClass . '">'
111  . $this->‪renderSingleCheckboxElement($label, $itemKey, $formElementValue, $numberOfItems, $this->data['parameterArray'], $disabled) .
112  '</div>';
113  ++$counter;
114  if ($counter < $numberOfItems && !empty($colClear)) {
115  foreach ($colClear as $rowBreakAfter => $clearClass) {
116  if ($counter % $rowBreakAfter === 0) {
117  $elementHtml .= '<div class="clearfix ' . $clearClass . '"></div>';
118  }
119  }
120  }
121  }
122  $elementHtml .= '</div>';
123  } else {
124  $counter = 0;
125  foreach ($items as $itemKey => $itemDefinition) {
126  $label = $itemDefinition[0];
127  $elementHtml .= $this->‪renderSingleCheckboxElement($label, $counter, $formElementValue, $numberOfItems, $this->data['parameterArray'], $disabled);
128  ++$counter;
129  }
130  }
131  if (!$disabled) {
132  $elementHtml .= '<input type="hidden" name="' . htmlspecialchars($this->data['parameterArray']['itemFormElName']) . '" value="' . htmlspecialchars((string)$formElementValue) . '" />';
133  }
134 
135  $fieldInformationResult = $this->‪renderFieldInformation();
136  $fieldInformationHtml = $fieldInformationResult['html'];
137  $resultArray = $this->‪mergeChildReturnIntoExistingResult($resultArray, $fieldInformationResult, false);
138 
139  $fieldWizardResult = $this->‪renderFieldWizard();
140  $fieldWizardHtml = $fieldWizardResult['html'];
141  $resultArray = $this->‪mergeChildReturnIntoExistingResult($resultArray, $fieldWizardResult, false);
142 
143  $html = [];
144  $html[] = '<div class="formengine-field-item t3js-formengine-field-item">';
145  $html[] = $fieldInformationHtml;
146  $html[] = '<div class="form-wizards-wrap">';
147  $html[] = '<div class="form-wizards-element">';
148  $html[] = $elementHtml;
149  $html[] = '</div>';
150  if (!$disabled && !empty($fieldWizardHtml)) {
151  $html[] = '<div class="form-wizards-items-bottom">';
152  $html[] = $fieldWizardHtml;
153  $html[] = '</div>';
154  }
155  $html[] = '</div>';
156  $html[] = '</div>';
157 
158  $resultArray['html'] = implode(LF, $html);
159  return $resultArray;
160  }
161 
173  protected function ‪renderSingleCheckboxElement($label, $itemCounter, $formElementValue, $numberOfItems, $additionalInformation, $disabled): string
174  {
175  $config = $additionalInformation['fieldConf']['config'];
176  $inline = !empty($config['cols']) && $config['cols'] === 'inline';
177  $invert = isset($config['items'][$itemCounter]['invertStateDisplay']) && $config['items'][$itemCounter]['invertStateDisplay'] === true;
178  $checkboxParameters = $this->‪checkBoxParams(
179  $additionalInformation['itemFormElName'],
180  $formElementValue,
181  $itemCounter,
182  $numberOfItems,
183  implode('', $additionalInformation['fieldChangeFunc'])
184  );
185  $uniqueId = ‪StringUtility::getUniqueId('_');
186  $checkboxId = $additionalInformation['itemFormElID'] . '_' . $itemCounter . $uniqueId;
187 
188  $iconIdentifierChecked = !empty($config['items'][$itemCounter]['iconIdentifierChecked']) ? $config['items'][$itemCounter]['iconIdentifierChecked'] : 'actions-check';
189  if (!$this->iconRegistry->isRegistered($iconIdentifierChecked)) {
190  $iconIdentifierChecked = 'actions-check';
191  }
192  $iconIdentifierUnchecked = !empty($config['items'][$itemCounter]['iconIdentifierUnchecked']) ? $config['items'][$itemCounter]['iconIdentifierUnchecked'] : 'empty-empty';
193  if (!$this->iconRegistry->isRegistered($iconIdentifierUnchecked)) {
194  $iconIdentifierUnchecked = 'empty-empty';
195  }
196  $iconChecked = $this->iconFactory->getIcon($iconIdentifierChecked, ‪Icon::SIZE_SMALL)->render('inline');
197  $iconUnchecked = $this->iconFactory->getIcon($iconIdentifierUnchecked, ‪Icon::SIZE_SMALL)->render('inline');
198 
199  return '
200  <div class="checkbox checkbox-type-icon-toggle' . ($invert ? ' checkbox-invert' : '') . ($inline ? ' checkbox-inline' : '') . (!$disabled ? '' : ' disabled') . '">
201  <input type="checkbox"
202  class="checkbox-input"
203  value="1"
204  data-formengine-input-name="' . htmlspecialchars($additionalInformation['itemFormElName']) . '"
205  ' . $checkboxParameters . '
206  ' . ($disabled ? ' disabled="disabled"' : '') . '
207  id="' . $checkboxId . '" />
208  <label class="checkbox-label" for="' . $checkboxId . '">
209  <span class="checkbox-label-icon">
210  <span class="checkbox-label-icon-checked">' . ($invert ? $iconUnchecked : $iconChecked) . '</span>
211  <span class="checkbox-label-icon-unchecked">' . ($invert ? $iconChecked : $iconUnchecked) . '</span>
212  </span>
213  <span class="checkbox-label-text">' . $this->‪appendValueToLabelInDebugMode(($label ? htmlspecialchars($label) : '&nbsp;'), $formElementValue) . '</span>
214  </label>
215  </div>';
216  }
217 }
‪TYPO3\CMS\Core\Imaging\Icon\SIZE_SMALL
‪const SIZE_SMALL
Definition: Icon.php:29
‪TYPO3\CMS\Backend\Form\Element\AbstractFormElement\renderFieldInformation
‪array renderFieldInformation()
Definition: AbstractFormElement.php:71
‪TYPO3\CMS\Backend\Form\AbstractNode\mergeChildReturnIntoExistingResult
‪array mergeChildReturnIntoExistingResult(array $existing, array $childReturn, bool $mergeHtml=true)
Definition: AbstractNode.php:115
‪TYPO3\CMS\Backend\Form\AbstractNode\initializeResultArray
‪array initializeResultArray()
Definition: AbstractNode.php:88
‪TYPO3\CMS\Core\Imaging\Icon
Definition: Icon.php:25
‪TYPO3\CMS\Backend\Form\Element\AbstractFormElement\appendValueToLabelInDebugMode
‪string int appendValueToLabelInDebugMode($label, $value)
Definition: AbstractFormElement.php:377
‪TYPO3\CMS\Backend\Form\AbstractNode\$nodeFactory
‪NodeFactory $nodeFactory
Definition: AbstractNode.php:34
‪TYPO3\CMS\Backend\Form\Element\AbstractFormElement
Definition: AbstractFormElement.php:31
‪TYPO3\CMS\Backend\Form\Element
Definition: AbstractFormElement.php:2
‪TYPO3\CMS\Backend\Form\Element\CheckboxElement
Definition: CheckboxElement.php:28
‪TYPO3\CMS\Backend\Form\Element\CheckboxElement\$defaultFieldInformation
‪array $defaultFieldInformation
Definition: CheckboxElement.php:37
‪TYPO3\CMS\Backend\Form\AbstractNode\$data
‪array $data
Definition: AbstractNode.php:40
‪TYPO3\CMS\Core\Imaging\IconRegistry
Definition: IconRegistry.php:34
‪TYPO3\CMS\Backend\Form\Element\CheckboxElement\$defaultFieldWizard
‪array $defaultFieldWizard
Definition: CheckboxElement.php:47
‪TYPO3\CMS\Backend\Form\Element\CheckboxElement\$iconRegistry
‪IconRegistry $iconRegistry
Definition: CheckboxElement.php:31
‪TYPO3\CMS\Backend\Form\NodeFactory
Definition: NodeFactory.php:36
‪TYPO3\CMS\Backend\Form\Element\CheckboxElement\__construct
‪__construct(NodeFactory $nodeFactory, array $data)
Definition: CheckboxElement.php:69
‪TYPO3\CMS\Core\Utility\StringUtility\getUniqueId
‪static string getUniqueId($prefix='')
Definition: StringUtility.php:91
‪TYPO3\CMS\Backend\Form\Element\CheckboxElement\renderSingleCheckboxElement
‪string renderSingleCheckboxElement($label, $itemCounter, $formElementValue, $numberOfItems, $additionalInformation, $disabled)
Definition: CheckboxElement.php:170
‪TYPO3\CMS\Backend\Form\Element\AbstractFormElement\calculateColumnMarkup
‪array calculateColumnMarkup(int $cols)
Definition: AbstractFormElement.php:338
‪TYPO3\CMS\Backend\Form\Element\CheckboxElement\render
‪array render()
Definition: CheckboxElement.php:80
‪TYPO3\CMS\Core\Utility\GeneralUtility
Definition: GeneralUtility.php:45
‪TYPO3\CMS\Core\Utility\StringUtility
Definition: StringUtility.php:21
‪TYPO3\CMS\Backend\Form\Element\AbstractFormElement\renderFieldWizard
‪array renderFieldWizard()
Definition: AbstractFormElement.php:103
‪TYPO3\CMS\Backend\Form\Element\AbstractFormElement\checkBoxParams
‪string checkBoxParams($itemName, $formElementValue, $checkbox, $checkboxesCount, $additionalJavaScript='')
Definition: AbstractFormElement.php:321