‪TYPO3CMS  ‪main
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 
22 use TYPO3\CMS\Core\Imaging\IconSize;
24 
29 {
35  protected ‪$defaultFieldInformation = [
36  'tcaDescription' => [
37  'renderType' => 'tcaDescription',
38  ],
39  ];
40 
46  protected ‪$defaultFieldWizard = [
47  'localizationStateSelector' => [
48  'renderType' => 'localizationStateSelector',
49  ],
50  'otherLanguageContent' => [
51  'renderType' => 'otherLanguageContent',
52  'after' => [
53  'localizationStateSelector',
54  ],
55  ],
56  'defaultLanguageDifferences' => [
57  'renderType' => 'defaultLanguageDifferences',
58  'after' => [
59  'otherLanguageContent',
60  ],
61  ],
62  ];
63 
64  public function ‪__construct(
65  private readonly ‪IconFactory $iconFactory,
66  private readonly ‪IconRegistry $iconRegistry,
67  ) {}
68 
74  public function ‪render(): array
75  {
76  $resultArray = $this->‪initializeResultArray();
77 
78  $elementHtml = '';
79  $disabled = false;
80  if ($this->data['parameterArray']['fieldConf']['config']['readOnly'] ?? false) {
81  $disabled = true;
82  }
83  // Traversing the array of items
84  $items = $this->data['parameterArray']['fieldConf']['config']['items'] ?? [];
85 
86  $numberOfItems = count($items);
87  if ($numberOfItems === 0) {
88  $items[] = ['label' => ''];
89  $numberOfItems = 1;
90  }
91  $formElementValue = (int)$this->data['parameterArray']['itemFormElValue'];
92  $cols = (int)($this->data['parameterArray']['fieldConf']['config']['cols'] ?? 0);
93  if ($cols > 1) {
94  [$colClass, $colClear] = $this->‪calculateColumnMarkup($cols);
95  $elementHtml .= '<div class="row">';
96  $counter = 0;
97  // $itemKey is important here, because items could have been removed via TSConfig
98  foreach ($items as $itemKey => $itemDefinition) {
99  $label = $itemDefinition['label'];
100  $elementHtml .=
101  '<div class="' . $colClass . '">'
102  . $this->‪renderSingleCheckboxElement($label, $itemKey, $formElementValue, $numberOfItems, $this->data['parameterArray'], $disabled) .
103  '</div>';
104  ++$counter;
105  if ($counter < $numberOfItems && !empty($colClear)) {
106  foreach ($colClear as $rowBreakAfter => $clearClass) {
107  if ($counter % $rowBreakAfter === 0) {
108  $elementHtml .= '<div class="clearfix ' . $clearClass . '"></div>';
109  }
110  }
111  }
112  }
113  $elementHtml .= '</div>';
114  } else {
115  foreach ($items as $itemKey => $itemDefinition) {
116  $label = $itemDefinition['label'];
117  $elementHtml .= $this->‪renderSingleCheckboxElement($label, $itemKey, $formElementValue, $numberOfItems, $this->data['parameterArray'], $disabled);
118  }
119  }
120  if (!$disabled) {
121  $elementHtml .= '<input type="hidden" name="' . htmlspecialchars($this->data['parameterArray']['itemFormElName']) . '" value="' . htmlspecialchars((string)$formElementValue) . '" />';
122  }
123 
124  $fieldInformationResult = $this->‪renderFieldInformation();
125  $fieldInformationHtml = $fieldInformationResult['html'];
126  $resultArray = $this->‪mergeChildReturnIntoExistingResult($resultArray, $fieldInformationResult, false);
127 
128  $fieldWizardResult = $this->‪renderFieldWizard();
129  $fieldWizardHtml = $fieldWizardResult['html'];
130  $resultArray = $this->‪mergeChildReturnIntoExistingResult($resultArray, $fieldWizardResult, false);
131 
132  $html = [];
133  $html[] = '<div class="formengine-field-item t3js-formengine-field-item">';
134  $html[] = $fieldInformationHtml;
135  $html[] = '<div class="form-wizards-wrap">';
136  $html[] = '<div class="form-wizards-element">';
137  $html[] = $elementHtml;
138  $html[] = '</div>';
139  if (!$disabled && !empty($fieldWizardHtml)) {
140  $html[] = '<div class="form-wizards-items-bottom">';
141  $html[] = $fieldWizardHtml;
142  $html[] = '</div>';
143  }
144  $html[] = '</div>';
145  $html[] = '</div>';
146 
147  $resultArray['html'] = $this->‪wrapWithFieldsetAndLegend(implode(LF, $html));
148  return $resultArray;
149  }
150 
162  protected function ‪renderSingleCheckboxElement($label, $itemCounter, $formElementValue, $numberOfItems, $additionalInformation, $disabled): string
163  {
164  $config = $additionalInformation['fieldConf']['config'];
165  $inline = !empty($config['cols']) && $config['cols'] === 'inline';
166  $invert = isset($config['items'][$itemCounter]['invertStateDisplay']) && $config['items'][$itemCounter]['invertStateDisplay'] === true;
167  $checkboxParameters = $this->‪checkBoxParams(
168  $additionalInformation['itemFormElName'],
169  $formElementValue,
170  $itemCounter,
171  $numberOfItems,
172  $additionalInformation['fieldChangeFunc'] ?? [],
173  $invert
174  );
175  $checkboxId = htmlspecialchars(‪StringUtility::getUniqueId('formengine-check-') . '-' . $itemCounter);
176 
177  $iconIdentifierChecked = !empty($config['items'][$itemCounter]['iconIdentifierChecked']) ? $config['items'][$itemCounter]['iconIdentifierChecked'] : 'actions-check';
178  if (!$this->iconRegistry->isRegistered($iconIdentifierChecked)) {
179  $iconIdentifierChecked = 'actions-check';
180  }
181  $iconIdentifierUnchecked = !empty($config['items'][$itemCounter]['iconIdentifierUnchecked']) ? $config['items'][$itemCounter]['iconIdentifierUnchecked'] : 'empty-empty';
182  if (!$this->iconRegistry->isRegistered($iconIdentifierUnchecked)) {
183  $iconIdentifierUnchecked = 'empty-empty';
184  }
185  $iconChecked = $this->iconFactory->getIcon($iconIdentifierChecked, IconSize::SMALL)->render('inline');
186  $iconUnchecked = $this->iconFactory->getIcon($iconIdentifierUnchecked, IconSize::SMALL)->render('inline');
187 
188  return '
189  <div class="form-check form-check-type-icon-toggle' . ($inline ? ' form-check-inline' : '') . (!$disabled ? '' : ' disabled') . '">
190  <input type="checkbox"
191  class="form-check-input"
192  value="1"
193  data-formengine-input-name="' . htmlspecialchars($additionalInformation['itemFormElName']) . '"
194  ' . $checkboxParameters . '
195  ' . ($disabled ? ' disabled="disabled"' : '') . '
196  id="' . $checkboxId . '" />
197  <label class="form-check-label" for="' . $checkboxId . '">
198  <span class="form-check-label-icon">
199  <span class="form-check-label-icon-checked">' . $iconChecked . '</span>
200  <span class="form-check-label-icon-unchecked">' . $iconUnchecked . '</span>
201  </span>
202  ' . $this->‪appendValueToLabelInDebugMode(($label ? htmlspecialchars($label) : ''), $formElementValue) . '
203  </label>
204  </div>';
205  }
206 }
‪TYPO3\CMS\Backend\Form\Element\AbstractFormElement\renderFieldInformation
‪array renderFieldInformation()
Definition: AbstractFormElement.php:73
‪TYPO3\CMS\Backend\Form\AbstractNode\mergeChildReturnIntoExistingResult
‪array mergeChildReturnIntoExistingResult(array $existing, array $childReturn, bool $mergeHtml=true)
Definition: AbstractNode.php:104
‪TYPO3\CMS\Backend\Form\Element\AbstractFormElement
Definition: AbstractFormElement.php:37
‪TYPO3\CMS\Core\Imaging\IconFactory
Definition: IconFactory.php:35
‪TYPO3\CMS\Backend\Form\Element
Definition: AbstractFormElement.php:16
‪TYPO3\CMS\Backend\Form\Element\CheckboxElement\__construct
‪__construct(private readonly IconFactory $iconFactory, private readonly IconRegistry $iconRegistry,)
Definition: CheckboxElement.php:62
‪TYPO3\CMS\Backend\Form\Element\AbstractFormElement\wrapWithFieldsetAndLegend
‪wrapWithFieldsetAndLegend(string $innerHTML)
Definition: AbstractFormElement.php:133
‪TYPO3\CMS\Backend\Form\Element\AbstractFormElement\calculateColumnMarkup
‪calculateColumnMarkup(int $cols)
Definition: AbstractFormElement.php:412
‪TYPO3\CMS\Backend\Form\Element\CheckboxElement
Definition: CheckboxElement.php:29
‪TYPO3\CMS\Backend\Form\Element\CheckboxElement\$defaultFieldInformation
‪array $defaultFieldInformation
Definition: CheckboxElement.php:34
‪TYPO3\CMS\Core\Imaging\IconRegistry
Definition: IconRegistry.php:32
‪TYPO3\CMS\Backend\Form\Element\CheckboxElement\$defaultFieldWizard
‪array $defaultFieldWizard
Definition: CheckboxElement.php:44
‪TYPO3\CMS\Backend\Form\Element\AbstractFormElement\appendValueToLabelInDebugMode
‪appendValueToLabelInDebugMode(string|int $label, string|int $value)
Definition: AbstractFormElement.php:447
‪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:383
‪TYPO3\CMS\Backend\Form\Element\CheckboxElement\renderSingleCheckboxElement
‪string renderSingleCheckboxElement($label, $itemCounter, $formElementValue, $numberOfItems, $additionalInformation, $disabled)
Definition: CheckboxElement.php:160
‪TYPO3\CMS\Backend\Form\Element\CheckboxElement\render
‪array render()
Definition: CheckboxElement.php:72
‪TYPO3\CMS\Core\Utility\StringUtility
Definition: StringUtility.php:24
‪TYPO3\CMS\Backend\Form\Element\AbstractFormElement\renderFieldWizard
‪array renderFieldWizard()
Definition: AbstractFormElement.php:105
‪TYPO3\CMS\Core\Utility\StringUtility\getUniqueId
‪static getUniqueId(string $prefix='')
Definition: StringUtility.php:57
‪TYPO3\CMS\Backend\Form\AbstractNode\initializeResultArray
‪initializeResultArray()
Definition: AbstractNode.php:77