‪TYPO3CMS  ‪main
CheckboxLabeledToggleElement.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  $formElementValue = (int)$this->data['parameterArray']['itemFormElValue'];
84  $cols = (int)($this->data['parameterArray']['fieldConf']['config']['cols'] ?? 0);
85  if ($cols > 1) {
86  [$colClass, $colClear] = $this->‪calculateColumnMarkup($cols);
87  $elementHtml .= '<div class="row">';
88  $counter = 0;
89  // $itemKey is important here, because items could have been removed via TSConfig
90  foreach ($items as $itemKey => $itemDefinition) {
91  $label = $itemDefinition['label'];
92  $elementHtml .=
93  '<div class="' . $colClass . '">'
94  . $this->‪renderSingleCheckboxElement($label, $itemKey, $formElementValue, $numberOfItems, $this->data['parameterArray'], $disabled) .
95  '</div>';
96  ++$counter;
97  if ($counter < $numberOfItems && !empty($colClear)) {
98  foreach ($colClear as $rowBreakAfter => $clearClass) {
99  if ($counter % $rowBreakAfter === 0) {
100  $elementHtml .= '<div class="clearfix ' . $clearClass . '"></div>';
101  }
102  }
103  }
104  }
105  $elementHtml .= '</div>';
106  } else {
107  $counter = 0;
108  foreach ($items as $itemDefinition) {
109  $label = $itemDefinition['label'];
110  $elementHtml .= $this->‪renderSingleCheckboxElement($label, $counter, $formElementValue, $numberOfItems, $this->data['parameterArray'], $disabled);
111  ++$counter;
112  }
113  }
114  if (!$disabled) {
115  $elementHtml .= '<input type="hidden" name="' . htmlspecialchars($this->data['parameterArray']['itemFormElName']) . '" value="' . htmlspecialchars((string)$formElementValue) . '" />';
116  }
117 
118  $fieldInformationResult = $this->‪renderFieldInformation();
119  $fieldInformationHtml = $fieldInformationResult['html'];
120  $resultArray = $this->‪mergeChildReturnIntoExistingResult($resultArray, $fieldInformationResult, false);
121 
122  $fieldWizardResult = $this->‪renderFieldWizard();
123  $fieldWizardHtml = $fieldWizardResult['html'];
124  $resultArray = $this->‪mergeChildReturnIntoExistingResult($resultArray, $fieldWizardResult, false);
125 
126  $html = [];
127  $html[] = '<div class="formengine-field-item t3js-formengine-field-item">';
128  $html[] = $fieldInformationHtml;
129  $html[] = '<div class="form-wizards-wrap">';
130  $html[] = '<div class="form-wizards-element">';
131  $html[] = $elementHtml;
132  $html[] = '</div>';
133  if (!$disabled && !empty($fieldWizardHtml)) {
134  $html[] = '<div class="form-wizards-items-bottom">';
135  $html[] = $fieldWizardHtml;
136  $html[] = '</div>';
137  }
138  $html[] = '</div>';
139  $html[] = '</div>';
140 
141  $resultArray['html'] = $this->‪wrapWithFieldsetAndLegend(implode(LF, $html));
142  return $resultArray;
143  }
144 
156  protected function ‪renderSingleCheckboxElement($label, $itemCounter, $formElementValue, $numberOfItems, $additionalInformation, $disabled): string
157  {
158  $config = $additionalInformation['fieldConf']['config'];
159  $inline = !empty($config['cols']) && $config['cols'] === 'inline';
160  $invert = isset($config['items'][$itemCounter]['invertStateDisplay']) && $config['items'][$itemCounter]['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-labeled-') . '-' . $itemCounter);
170  return '
171  <div class="form-check form-check-type-labeled-toggle' . ($inline ? ' form-check-inline' : '') . (!$disabled ? '' : ' disabled') . '">
172  <input type="checkbox"
173  class="form-check-input"
174  value="1"
175  data-form-check-label-checked="' . $config['items'][$itemCounter]['labelChecked'] . '"
176  data-form-check-label-unchecked="' . $config['items'][$itemCounter]['labelUnchecked'] . '"
177  data-formengine-input-name="' . htmlspecialchars($additionalInformation['itemFormElName']) . '"
178  ' . $checkboxParameters . '
179  ' . (!$disabled ? '' : ' disabled="disabled"') . '
180  id="' . $checkboxId . '" />
181  <label class="form-check-label" for="' . $checkboxId . '">
182  ' . $this->‪appendValueToLabelInDebugMode(($label ? htmlspecialchars($label) : ''), $formElementValue) . '
183  </label>
184  </div>';
185  }
186 }
‪TYPO3\CMS\Backend\Form\Element\CheckboxLabeledToggleElement\renderSingleCheckboxElement
‪string renderSingleCheckboxElement($label, $itemCounter, $formElementValue, $numberOfItems, $additionalInformation, $disabled)
Definition: CheckboxLabeledToggleElement.php:154
‪TYPO3\CMS\Backend\Form\Element\AbstractFormElement\renderFieldInformation
‪array renderFieldInformation()
Definition: AbstractFormElement.php:73
‪TYPO3\CMS\Backend\Form\Element\CheckboxLabeledToggleElement\$defaultFieldInformation
‪array $defaultFieldInformation
Definition: CheckboxLabeledToggleElement.php:31
‪TYPO3\CMS\Backend\Form\AbstractNode\mergeChildReturnIntoExistingResult
‪array mergeChildReturnIntoExistingResult(array $existing, array $childReturn, bool $mergeHtml=true)
Definition: AbstractNode.php:104
‪TYPO3\CMS\Backend\Form\Element\CheckboxLabeledToggleElement\$defaultFieldWizard
‪array $defaultFieldWizard
Definition: CheckboxLabeledToggleElement.php:41
‪TYPO3\CMS\Backend\Form\Element\CheckboxLabeledToggleElement\render
‪array render()
Definition: CheckboxLabeledToggleElement.php:64
‪TYPO3\CMS\Backend\Form\Element\AbstractFormElement
Definition: AbstractFormElement.php:37
‪TYPO3\CMS\Backend\Form\Element
Definition: AbstractFormElement.php:16
‪TYPO3\CMS\Backend\Form\Element\CheckboxLabeledToggleElement
Definition: CheckboxLabeledToggleElement.php:26
‪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\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