‪TYPO3CMS  11.5
CheckboxElement.php
Go to the documentation of this file.
1 <?php
2 
3 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 
19 
25 
30 {
34  private ‪$iconRegistry;
35 
41  protected ‪$defaultFieldInformation = [
42  'tcaDescription' => [
43  'renderType' => 'tcaDescription',
44  ],
45  ];
46 
52  protected ‪$defaultFieldWizard = [
53  'localizationStateSelector' => [
54  'renderType' => 'localizationStateSelector',
55  ],
56  'otherLanguageContent' => [
57  'renderType' => 'otherLanguageContent',
58  'after' => [
59  'localizationStateSelector',
60  ],
61  ],
62  'defaultLanguageDifferences' => [
63  'renderType' => 'defaultLanguageDifferences',
64  'after' => [
65  'otherLanguageContent',
66  ],
67  ],
68  ];
69 
75  {
76  parent::__construct(‪$nodeFactory, ‪$data);
77  $this->iconRegistry = GeneralUtility::makeInstance(IconRegistry::class);
78  }
79 
85  public function ‪render(): array
86  {
87  $resultArray = $this->‪initializeResultArray();
88 
89  $elementHtml = '';
90  $disabled = false;
91  if ($this->data['parameterArray']['fieldConf']['config']['readOnly'] ?? false) {
92  $disabled = true;
93  }
94  // Traversing the array of items
95  $items = $this->data['parameterArray']['fieldConf']['config']['items'] ?? [];
96 
97  $numberOfItems = \count($items);
98  if ($numberOfItems === 0) {
99  $items[] = ['', ''];
100  $numberOfItems = 1;
101  }
102  $formElementValue = (int)$this->data['parameterArray']['itemFormElValue'];
103  $cols = (int)($this->data['parameterArray']['fieldConf']['config']['cols'] ?? 0);
104  if ($cols > 1) {
105  [$colClass, $colClear] = $this->‪calculateColumnMarkup($cols);
106  $elementHtml .= '<div class="checkbox-row row">';
107  $counter = 0;
108  // $itemKey is important here, because items could have been removed via TSConfig
109  foreach ($items as $itemKey => $itemDefinition) {
110  $label = $itemDefinition[0];
111  $elementHtml .=
112  '<div class="checkbox-column ' . $colClass . '">'
113  . $this->‪renderSingleCheckboxElement($label, $itemKey, $formElementValue, $numberOfItems, $this->data['parameterArray'], $disabled) .
114  '</div>';
115  ++$counter;
116  if ($counter < $numberOfItems && !empty($colClear)) {
117  foreach ($colClear as $rowBreakAfter => $clearClass) {
118  if ($counter % $rowBreakAfter === 0) {
119  $elementHtml .= '<div class="clearfix ' . $clearClass . '"></div>';
120  }
121  }
122  }
123  }
124  $elementHtml .= '</div>';
125  } else {
126  foreach ($items as $itemKey => $itemDefinition) {
127  $label = $itemDefinition[0];
128  $elementHtml .= $this->‪renderSingleCheckboxElement($label, $itemKey, $formElementValue, $numberOfItems, $this->data['parameterArray'], $disabled);
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  $additionalInformation['fieldChangeFunc'] ?? [],
184  $invert
185  );
186  $uniqueId = ‪StringUtility::getUniqueId('_');
187  $checkboxId = $additionalInformation['itemFormElID'] . '_' . $itemCounter . $uniqueId;
188 
189  $iconIdentifierChecked = !empty($config['items'][$itemCounter]['iconIdentifierChecked']) ? $config['items'][$itemCounter]['iconIdentifierChecked'] : 'actions-check';
190  if (!$this->iconRegistry->isRegistered($iconIdentifierChecked)) {
191  $iconIdentifierChecked = 'actions-check';
192  }
193  $iconIdentifierUnchecked = !empty($config['items'][$itemCounter]['iconIdentifierUnchecked']) ? $config['items'][$itemCounter]['iconIdentifierUnchecked'] : 'empty-empty';
194  if (!$this->iconRegistry->isRegistered($iconIdentifierUnchecked)) {
195  $iconIdentifierUnchecked = 'empty-empty';
196  }
197  $iconChecked = $this->iconFactory->getIcon($iconIdentifierChecked, ‪Icon::SIZE_SMALL)->render('inline');
198  $iconUnchecked = $this->iconFactory->getIcon($iconIdentifierUnchecked, ‪Icon::SIZE_SMALL)->render('inline');
199 
200  return '
201  <div class="form-check form-check-type-icon-toggle' . ($inline ? ' form-check-inline' : '') . (!$disabled ? '' : ' disabled') . '">
202  <input type="checkbox"
203  class="form-check-input"
204  value="1"
205  data-formengine-input-name="' . htmlspecialchars($additionalInformation['itemFormElName']) . '"
206  ' . $checkboxParameters . '
207  ' . ($disabled ? ' disabled="disabled"' : '') . '
208  id="' . $checkboxId . '" />
209  <label class="form-check-label" for="' . $checkboxId . '">
210  <span class="form-check-label-icon">
211  <span class="form-check-label-icon-checked">' . $iconChecked . '</span>
212  <span class="form-check-label-icon-unchecked">' . $iconUnchecked . '</span>
213  </span>
214  <span class="form-check-label-text">' . $this->‪appendValueToLabelInDebugMode(($label ? htmlspecialchars($label) : '&nbsp;'), $formElementValue) . '</span>
215  </label>
216  </div>';
217  }
218 }
‪TYPO3\CMS\Core\Imaging\Icon\SIZE_SMALL
‪const SIZE_SMALL
Definition: Icon.php:30
‪TYPO3\CMS\Backend\Form\Element\AbstractFormElement\renderFieldInformation
‪array renderFieldInformation()
Definition: AbstractFormElement.php:76
‪TYPO3\CMS\Backend\Form\AbstractNode\mergeChildReturnIntoExistingResult
‪array mergeChildReturnIntoExistingResult(array $existing, array $childReturn, bool $mergeHtml=true)
Definition: AbstractNode.php:120
‪TYPO3\CMS\Backend\Form\AbstractNode\initializeResultArray
‪array initializeResultArray()
Definition: AbstractNode.php:91
‪TYPO3\CMS\Core\Imaging\Icon
Definition: Icon.php:26
‪TYPO3\CMS\Backend\Form\AbstractNode\$nodeFactory
‪NodeFactory $nodeFactory
Definition: AbstractNode.php:36
‪TYPO3\CMS\Backend\Form\Element\AbstractFormElement
Definition: AbstractFormElement.php:35
‪TYPO3\CMS\Backend\Form\Element
Definition: AbstractFormElement.php:16
‪TYPO3\CMS\Backend\Form\Element\CheckboxElement
Definition: CheckboxElement.php:30
‪TYPO3\CMS\Backend\Form\Element\CheckboxElement\$defaultFieldInformation
‪array $defaultFieldInformation
Definition: CheckboxElement.php:39
‪TYPO3\CMS\Backend\Form\AbstractNode\$data
‪array $data
Definition: AbstractNode.php:43
‪TYPO3\CMS\Core\Imaging\IconRegistry
Definition: IconRegistry.php:32
‪TYPO3\CMS\Backend\Form\Element\CheckboxElement\$defaultFieldWizard
‪array $defaultFieldWizard
Definition: CheckboxElement.php:49
‪TYPO3\CMS\Backend\Form\Element\AbstractFormElement\checkBoxParams
‪string checkBoxParams(string $itemName, int $formElementValue, int $checkbox, int $checkboxesCount, array $fieldChangeFuncs=[], bool $invert=false)
Definition: AbstractFormElement.php:366
‪TYPO3\CMS\Backend\Form\Element\CheckboxElement\$iconRegistry
‪IconRegistry $iconRegistry
Definition: CheckboxElement.php:33
‪TYPO3\CMS\Backend\Form\NodeFactory
Definition: NodeFactory.php:37
‪TYPO3\CMS\Backend\Form\Element\AbstractFormElement\appendValueToLabelInDebugMode
‪string appendValueToLabelInDebugMode($label, $value)
Definition: AbstractFormElement.php:436
‪TYPO3\CMS\Backend\Form\Element\CheckboxElement\__construct
‪__construct(NodeFactory $nodeFactory, array $data)
Definition: CheckboxElement.php:71
‪TYPO3\CMS\Core\Utility\StringUtility\getUniqueId
‪static string getUniqueId($prefix='')
Definition: StringUtility.php:128
‪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:397
‪TYPO3\CMS\Backend\Form\Element\CheckboxElement\render
‪array render()
Definition: CheckboxElement.php:82
‪TYPO3\CMS\Core\Utility\GeneralUtility
Definition: GeneralUtility.php:50
‪TYPO3\CMS\Core\Utility\StringUtility
Definition: StringUtility.php:22
‪TYPO3\CMS\Backend\Form\Element\AbstractFormElement\renderFieldWizard
‪array renderFieldWizard()
Definition: AbstractFormElement.php:108