‪TYPO3CMS  ‪main
CheckboxToggleElement.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 
21 
26 {
32  protected ‪$defaultFieldInformation = [
33  'tcaDescription' => [
34  'renderType' => 'tcaDescription',
35  ],
36  ];
37 
43  protected ‪$defaultFieldWizard = [
44  'localizationStateSelector' => [
45  'renderType' => 'localizationStateSelector',
46  ],
47  'otherLanguageContent' => [
48  'renderType' => 'otherLanguageContent',
49  'after' => [
50  'localizationStateSelector',
51  ],
52  ],
53  'defaultLanguageDifferences' => [
54  'renderType' => 'defaultLanguageDifferences',
55  'after' => [
56  'otherLanguageContent',
57  ],
58  ],
59  ];
60 
66  public function ‪render(): array
67  {
68  $resultArray = $this->‪initializeResultArray();
69 
70  $elementHtml = '';
71  $disabled = false;
72  if ($this->data['parameterArray']['fieldConf']['config']['readOnly'] ?? false) {
73  $disabled = true;
74  }
75  // Traversing the array of items
76  $items = $this->data['parameterArray']['fieldConf']['config']['items'];
77 
78  $numberOfItems = count($items);
79  if ($numberOfItems === 0) {
80  $items[] = ['label' => ''];
81  $numberOfItems = 1;
82  }
83  // The values in the array may be numeric strings, but we need real ints.
84  $formElementValue = (int)($this->data['parameterArray']['itemFormElValue'] ?? 0);
85  $cols = (int)($this->data['parameterArray']['fieldConf']['config']['cols'] ?? 0);
86  if ($cols > 1) {
87  [$colClass, $colClear] = $this->‪calculateColumnMarkup($cols);
88  $elementHtml .= '<div class="row">';
89  $counter = 0;
90  // $itemKey is important here, because items could have been removed via TSConfig
91  foreach ($items as $itemKey => $itemDefinition) {
92  $label = $itemDefinition['label'];
93  $elementHtml .=
94  '<div class="' . $colClass . '">'
95  . $this->‪renderSingleCheckboxElement($label, $itemKey, $formElementValue, $numberOfItems, $this->data['parameterArray'], $disabled) .
96  '</div>';
97  ++$counter;
98  if ($counter < $numberOfItems && !empty($colClear)) {
99  foreach ($colClear as $rowBreakAfter => $clearClass) {
100  if ($counter % $rowBreakAfter === 0) {
101  $elementHtml .= '<div class="clearfix ' . $clearClass . '"></div>';
102  }
103  }
104  }
105  }
106  $elementHtml .= '</div>';
107  } else {
108  $counter = 0;
109  foreach ($items as $itemDefinition) {
110  $label = $itemDefinition['label'];
111  $elementHtml .= $this->‪renderSingleCheckboxElement($label, $counter, $formElementValue, $numberOfItems, $this->data['parameterArray'], $disabled);
112  ++$counter;
113  }
114  }
115  if (!$disabled) {
116  $elementHtml .= '<input type="hidden" name="' . htmlspecialchars($this->data['parameterArray']['itemFormElName']) . '" value="' . htmlspecialchars((string)$formElementValue) . '" />';
117  }
118 
119  $fieldInformationResult = $this->‪renderFieldInformation();
120  $fieldInformationHtml = $fieldInformationResult['html'];
121  $resultArray = $this->‪mergeChildReturnIntoExistingResult($resultArray, $fieldInformationResult, false);
122 
123  $fieldWizardResult = $this->‪renderFieldWizard();
124  $fieldWizardHtml = $fieldWizardResult['html'];
125  $resultArray = $this->‪mergeChildReturnIntoExistingResult($resultArray, $fieldWizardResult, false);
126 
127  $html = [];
128  $html[] = '<div class="formengine-field-item t3js-formengine-field-item">';
129  $html[] = $fieldInformationHtml;
130  $html[] = '<div class="form-wizards-wrap">';
131  $html[] = '<div class="form-wizards-element">';
132  $html[] = $elementHtml;
133  $html[] = '</div>';
134  if (!$disabled && !empty($fieldWizardHtml)) {
135  $html[] = '<div class="form-wizards-items-bottom">';
136  $html[] = $fieldWizardHtml;
137  $html[] = '</div>';
138  }
139  $html[] = '</div>';
140  $html[] = '</div>';
141 
142  $resultArray['html'] = $this->‪wrapWithFieldsetAndLegend(implode(LF, $html));
143  return $resultArray;
144  }
145 
157  protected function ‪renderSingleCheckboxElement($label, $itemCounter, $formElementValue, $numberOfItems, $additionalInformation, $disabled): string
158  {
159  $config = $additionalInformation['fieldConf']['config'];
160  $invert = isset($config['items'][0]['invertStateDisplay']) && $config['items'][0]['invertStateDisplay'] === true;
161  $checkboxParameters = $this->‪checkBoxParams(
162  $additionalInformation['itemFormElName'],
163  $formElementValue,
164  $itemCounter,
165  $numberOfItems,
166  $additionalInformation['fieldChangeFunc'] ?? [],
167  $invert
168  );
169  $checkboxId = htmlspecialchars(‪StringUtility::getUniqueId('formengine-check-toggle-') . '-' . $itemCounter);
170  return '
171  <div class="form-check form-switch' . (!$disabled ? '' : ' disabled') . '">
172  <input type="checkbox"
173  class="form-check-input"
174  value="1"
175  data-formengine-input-name="' . htmlspecialchars($additionalInformation['itemFormElName']) . '"
176  ' . $checkboxParameters . '
177  ' . (!$disabled ? '' : ' disabled="disabled"') . '
178  id="' . $checkboxId . '" />
179  <label class="form-check-label" for="' . $checkboxId . '">
180  ' . $this->‪appendValueToLabelInDebugMode(($label ? htmlspecialchars($label) : ''), $formElementValue) . '
181  </label>
182  </div>';
183  }
184 }
‪TYPO3\CMS\Backend\Form\Element\AbstractFormElement\renderFieldInformation
‪array renderFieldInformation()
Definition: AbstractFormElement.php:73
‪TYPO3\CMS\Backend\Form\Element\CheckboxToggleElement\render
‪array render()
Definition: CheckboxToggleElement.php:64
‪TYPO3\CMS\Backend\Form\AbstractNode\mergeChildReturnIntoExistingResult
‪array mergeChildReturnIntoExistingResult(array $existing, array $childReturn, bool $mergeHtml=true)
Definition: AbstractNode.php:104
‪TYPO3\CMS\Backend\Form\Element\CheckboxToggleElement
Definition: CheckboxToggleElement.php:26
‪TYPO3\CMS\Backend\Form\Element\AbstractFormElement
Definition: AbstractFormElement.php:37
‪TYPO3\CMS\Backend\Form\Element\CheckboxToggleElement\$defaultFieldWizard
‪array $defaultFieldWizard
Definition: CheckboxToggleElement.php:41
‪TYPO3\CMS\Backend\Form\Element
Definition: AbstractFormElement.php:16
‪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\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\CheckboxToggleElement\renderSingleCheckboxElement
‪string renderSingleCheckboxElement($label, $itemCounter, $formElementValue, $numberOfItems, $additionalInformation, $disabled)
Definition: CheckboxToggleElement.php:155
‪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\Element\CheckboxToggleElement\$defaultFieldInformation
‪array $defaultFieldInformation
Definition: CheckboxToggleElement.php:31
‪TYPO3\CMS\Backend\Form\AbstractNode\initializeResultArray
‪initializeResultArray()
Definition: AbstractNode.php:77