‪TYPO3CMS  9.5
GridColumnClassAutoConfigurationViewHelper.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 
20 use TYPO3Fluid\Fluid\Core\Rendering\RenderingContextInterface;
21 use TYPO3Fluid\Fluid\Core\ViewHelper\AbstractViewHelper;
22 use TYPO3Fluid\Fluid\Core\ViewHelper\Traits\CompileWithRenderStatic;
23 
27 class ‪GridColumnClassAutoConfigurationViewHelper extends AbstractViewHelper
28 {
29  use CompileWithRenderStatic;
30 
34  protected ‪$escapeOutput = false;
35 
41  public function ‪initializeArguments()
42  {
43  $this->registerArgument('element', RootRenderableInterface::class, 'A RootRenderableInterface instance', true);
44  }
45 
52  public static function ‪renderStatic(
53  array $arguments,
54  \Closure $renderChildrenClosure,
55  RenderingContextInterface $renderingContext
56  ) {
57  $formElement = $arguments['element'];
58 
59  $gridRowElement = $formElement->getParentRenderable();
60  $gridContainerElement = $gridRowElement->getParentRenderable();
61  $gridRowEChildElements = $gridRowElement->getElements();
62 
63  if ($gridContainerElement instanceof ‪GridContainerInterface) {
64  $gridViewPortConfiguration = $gridContainerElement->getProperties()['gridColumnClassAutoConfiguration'];
65  } else {
66  $gridViewPortConfiguration = $gridRowElement->getProperties()['gridColumnClassAutoConfiguration'];
67  }
68 
69  if (empty($gridViewPortConfiguration)) {
70  return '';
71  }
72  $gridSize = (int)$gridViewPortConfiguration['gridSize'];
73 
74  $columnsToCalculate = [];
75  $usedColumns = [];
76  foreach ($gridRowEChildElements as $childElement) {
77  if (empty($childElement->getProperties()['gridColumnClassAutoConfiguration'])) {
78  foreach ($gridViewPortConfiguration['viewPorts'] as $viewPortName => $configuration) {
79  $columnsToCalculate[$viewPortName]['elements']++;
80  }
81  } else {
82  $gridColumnViewPortConfiguration = $childElement->getProperties()['gridColumnClassAutoConfiguration'];
83  foreach ($gridViewPortConfiguration['viewPorts'] as $viewPortName => $configuration) {
84  $configuration = $gridColumnViewPortConfiguration['viewPorts'][$viewPortName];
85  if (
86  isset($configuration['numbersOfColumnsToUse'])
87  && (int)$configuration['numbersOfColumnsToUse'] > 0
88  ) {
89  $usedColumns[$viewPortName]['sum'] += (int)$configuration['numbersOfColumnsToUse'];
90  if ($childElement->getIdentifier() === $formElement->getIdentifier()) {
91  $usedColumns[$viewPortName]['concreteNumbersOfColumnsToUse'] = (int)$configuration['numbersOfColumnsToUse'];
92  if ($usedColumns[$viewPortName]['concreteNumbersOfColumnsToUse'] > $gridSize) {
93  $usedColumns[$viewPortName]['concreteNumbersOfColumnsToUse'] = $gridSize;
94  }
95  }
96  } else {
97  $columnsToCalculate[$viewPortName]['elements']++;
98  }
99  }
100  }
101  }
102 
103  $classes = [];
104  foreach ($gridViewPortConfiguration['viewPorts'] as $viewPortName => $configuration) {
105  if (isset($usedColumns[$viewPortName]['concreteNumbersOfColumnsToUse'])) {
106  $numbersOfColumnsToUse = $usedColumns[$viewPortName]['concreteNumbersOfColumnsToUse'];
107  } else {
108  $restColumnsToDivide = $gridSize - $usedColumns[$viewPortName]['sum'];
109  $restElements = (int)$columnsToCalculate[$viewPortName]['elements'];
110 
111  if ($restColumnsToDivide < 1) {
112  $restColumnsToDivide = $gridSize;
113  }
114  if ($restElements < 1) {
115  $restElements = 1;
116  }
117  $numbersOfColumnsToUse = floor($restColumnsToDivide / $restElements);
118  }
119 
120  $classes[] = str_replace(
121  '{@numbersOfColumnsToUse}',
122  $numbersOfColumnsToUse,
123  $configuration['classPattern']
124  );
125  }
126 
127  return implode(' ', $classes);
128  }
129 }
‪TYPO3\CMS\Form\ViewHelpers\GridColumnClassAutoConfigurationViewHelper
Definition: GridColumnClassAutoConfigurationViewHelper.php:28
‪TYPO3\CMS\Form\ViewHelpers\GridColumnClassAutoConfigurationViewHelper\renderStatic
‪static string renderStatic(array $arguments, \Closure $renderChildrenClosure, RenderingContextInterface $renderingContext)
Definition: GridColumnClassAutoConfigurationViewHelper.php:50
‪TYPO3\CMS\Form\ViewHelpers\GridColumnClassAutoConfigurationViewHelper\initializeArguments
‪initializeArguments()
Definition: GridColumnClassAutoConfigurationViewHelper.php:39
‪TYPO3\CMS\Form\ViewHelpers\GridColumnClassAutoConfigurationViewHelper\$escapeOutput
‪bool $escapeOutput
Definition: GridColumnClassAutoConfigurationViewHelper.php:32
‪TYPO3\CMS\Form\Domain\Model\FormElements\GridContainerInterface
Definition: GridContainerInterface.php:22
‪TYPO3\CMS\Form\Domain\Model\Renderable\RootRenderableInterface
Definition: RootRenderableInterface.php:27
‪TYPO3\CMS\Form\ViewHelpers