‪TYPO3CMS  9.5
CheckboxToggleElement.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 
22 
27 {
31  private ‪$iconRegistry;
32 
38  protected ‪$defaultFieldInformation = [
39  'tcaDescription' => [
40  'renderType' => 'tcaDescription',
41  ],
42  ];
43 
49  protected ‪$defaultFieldWizard = [
50  'localizationStateSelector' => [
51  'renderType' => 'localizationStateSelector',
52  ],
53  'otherLanguageContent' => [
54  'renderType' => 'otherLanguageContent',
55  'after' => [
56  'localizationStateSelector'
57  ],
58  ],
59  'defaultLanguageDifferences' => [
60  'renderType' => 'defaultLanguageDifferences',
61  'after' => [
62  'otherLanguageContent',
63  ],
64  ],
65  ];
66 
72  {
73  parent::__construct(‪$nodeFactory, ‪$data);
74  $this->iconRegistry = GeneralUtility::makeInstance(IconRegistry::class);
75  }
76 
82  public function ‪render(): array
83  {
84  $resultArray = $this->‪initializeResultArray();
85 
86  $elementHtml = '';
87  $disabled = false;
88  if ($this->data['parameterArray']['fieldConf']['config']['readOnly']) {
89  $disabled = true;
90  }
91  // Traversing the array of items
92  $items = $this->data['parameterArray']['fieldConf']['config']['items'];
93 
94  $numberOfItems = count($items);
95  if ($numberOfItems === 0) {
96  $items[] = ['', ''];
97  $numberOfItems = 1;
98  }
99  $formElementValue = (int)$this->data['parameterArray']['itemFormElValue'];
100  $cols = (int)$this->data['parameterArray']['fieldConf']['config']['cols'];
101  if ($cols > 1) {
102  [$colClass, $colClear] = $this->‪calculateColumnMarkup($cols);
103  $elementHtml .= '<div class="checkbox-row row">';
104  $counter = 0;
105  // $itemKey is important here, because items could have been removed via TSConfig
106  foreach ($items as $itemKey => $itemDefinition) {
107  $label = $itemDefinition[0];
108  $elementHtml .=
109  '<div class="checkbox-column ' . $colClass . '">'
110  . $this->‪renderSingleCheckboxElement($label, $itemKey, $formElementValue, $numberOfItems, $this->data['parameterArray'], $disabled) .
111  '</div>';
112  ++$counter;
113  if ($counter < $numberOfItems && !empty($colClear)) {
114  foreach ($colClear as $rowBreakAfter => $clearClass) {
115  if ($counter % $rowBreakAfter === 0) {
116  $elementHtml .= '<div class="clearfix ' . $clearClass . '"></div>';
117  }
118  }
119  }
120  }
121  $elementHtml .= '</div>';
122  } else {
123  $counter = 0;
124  foreach ($items as $itemKey => $itemDefinition) {
125  $label = $itemDefinition[0];
126  $elementHtml .= $this->‪renderSingleCheckboxElement($label, $counter, $formElementValue, $numberOfItems, $this->data['parameterArray'], $disabled);
127  ++$counter;
128  }
129  }
130  if (!$disabled) {
131  $elementHtml .= '<input type="hidden" name="' . htmlspecialchars($this->data['parameterArray']['itemFormElName']) . '" value="' . htmlspecialchars((string)$formElementValue) . '" />';
132  }
133 
134  $fieldInformationResult = $this->‪renderFieldInformation();
135  $fieldInformationHtml = $fieldInformationResult['html'];
136  $resultArray = $this->‪mergeChildReturnIntoExistingResult($resultArray, $fieldInformationResult, false);
137 
138  $fieldWizardResult = $this->‪renderFieldWizard();
139  $fieldWizardHtml = $fieldWizardResult['html'];
140  $resultArray = $this->‪mergeChildReturnIntoExistingResult($resultArray, $fieldWizardResult, false);
141 
142  $html = [];
143  $html[] = '<div class="formengine-field-item t3js-formengine-field-item">';
144  $html[] = $fieldInformationHtml;
145  $html[] = '<div class="form-wizards-wrap">';
146  $html[] = '<div class="form-wizards-element">';
147  $html[] = $elementHtml;
148  $html[] = '</div>';
149  if (!$disabled && !empty($fieldWizardHtml)) {
150  $html[] = '<div class="form-wizards-items-bottom">';
151  $html[] = $fieldWizardHtml;
152  $html[] = '</div>';
153  }
154  $html[] = '</div>';
155  $html[] = '</div>';
156 
157  $resultArray['html'] = implode(LF, $html);
158  return $resultArray;
159  }
160 
172  protected function ‪renderSingleCheckboxElement($label, $itemCounter, $formElementValue, $numberOfItems, $additionalInformation, $disabled): string
173  {
174  $config = $additionalInformation['fieldConf']['config'];
175  $inline = !empty($config['cols']) && $config['cols'] === 'inline';
176  $invert = isset($config['items'][0]['invertStateDisplay']) && $config['items'][0]['invertStateDisplay'] === true;
177  $checkboxParameters = $this->‪checkBoxParams(
178  $additionalInformation['itemFormElName'],
179  $formElementValue,
180  $itemCounter,
181  $numberOfItems,
182  implode('', $additionalInformation['fieldChangeFunc'])
183  );
184  $uniqueId = ‪StringUtility::getUniqueId('_');
185  $checkboxId = $additionalInformation['itemFormElID'] . '_' . $itemCounter . $uniqueId;
186  return '
187  <div class="checkbox checkbox-type-toggle' . ($invert ? ' checkbox-invert' : '') . ($inline ? ' checkbox-inline' : '') . (!$disabled ? '' : ' disabled') . '">
188  <input type="checkbox"
189  class="checkbox-input"
190  value="1"
191  data-formengine-input-name="' . htmlspecialchars($additionalInformation['itemFormElName']) . '"
192  ' . $checkboxParameters . '
193  ' . (!$disabled ?: ' disabled="disabled"') . '
194  id="' . $checkboxId . '" />
195  <label class="checkbox-label" for="' . $checkboxId . '">
196  <span class="checkbox-label-text">' . $this->‪appendValueToLabelInDebugMode(($label ? htmlspecialchars($label) : '&nbsp;'), $formElementValue) . '</span>
197  </label>
198  </div>';
199  }
200 }
‪TYPO3\CMS\Backend\Form\Element\AbstractFormElement\renderFieldInformation
‪array renderFieldInformation()
Definition: AbstractFormElement.php:71
‪TYPO3\CMS\Backend\Form\Element\CheckboxToggleElement\__construct
‪__construct(NodeFactory $nodeFactory, array $data)
Definition: CheckboxToggleElement.php:68
‪TYPO3\CMS\Backend\Form\Element\CheckboxToggleElement\render
‪array render()
Definition: CheckboxToggleElement.php:79
‪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\Backend\Form\Element\AbstractFormElement\appendValueToLabelInDebugMode
‪string int appendValueToLabelInDebugMode($label, $value)
Definition: AbstractFormElement.php:377
‪TYPO3\CMS\Backend\Form\Element\CheckboxToggleElement
Definition: CheckboxToggleElement.php:27
‪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\CheckboxToggleElement\$defaultFieldWizard
‪array $defaultFieldWizard
Definition: CheckboxToggleElement.php:46
‪TYPO3\CMS\Backend\Form\Element
Definition: AbstractFormElement.php:2
‪TYPO3\CMS\Backend\Form\AbstractNode\$data
‪array $data
Definition: AbstractNode.php:40
‪TYPO3\CMS\Backend\Form\Element\CheckboxToggleElement\$iconRegistry
‪IconRegistry $iconRegistry
Definition: CheckboxToggleElement.php:30
‪TYPO3\CMS\Core\Imaging\IconRegistry
Definition: IconRegistry.php:34
‪TYPO3\CMS\Backend\Form\NodeFactory
Definition: NodeFactory.php:36
‪TYPO3\CMS\Core\Utility\StringUtility\getUniqueId
‪static string getUniqueId($prefix='')
Definition: StringUtility.php:91
‪TYPO3\CMS\Backend\Form\Element\AbstractFormElement\calculateColumnMarkup
‪array calculateColumnMarkup(int $cols)
Definition: AbstractFormElement.php:338
‪TYPO3\CMS\Backend\Form\Element\CheckboxToggleElement\renderSingleCheckboxElement
‪string renderSingleCheckboxElement($label, $itemCounter, $formElementValue, $numberOfItems, $additionalInformation, $disabled)
Definition: CheckboxToggleElement.php:169
‪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
‪TYPO3\CMS\Backend\Form\Element\CheckboxToggleElement\$defaultFieldInformation
‪array $defaultFieldInformation
Definition: CheckboxToggleElement.php:36