• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

FluidTYPO3 / flux / 14774311267

01 May 2025 11:00AM UTC coverage: 93.246% (+0.3%) from 92.9%
14774311267

push

github

NamelessCoder
[TER] 11.0.0

7083 of 7596 relevant lines covered (93.25%)

66.31 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

95.65
/Classes/ViewHelpers/AbstractFormViewHelper.php
1
<?php
2
declare(strict_types=1);
3
namespace FluidTYPO3\Flux\ViewHelpers;
4

5
/*
6
 * This file is part of the FluidTYPO3/Flux project under GPLv2 or later.
7
 *
8
 * For the full copyright and license information, please read the
9
 * LICENSE.md file that was distributed with this source code.
10
 */
11

12
use FluidTYPO3\Flux\Form;
13
use FluidTYPO3\Flux\Form\Container\Grid;
14
use FluidTYPO3\Flux\Form\FormInterface;
15
use TYPO3\CMS\Core\Utility\VersionNumberUtility;
16
use TYPO3Fluid\Fluid\Component\Argument\ArgumentCollection;
17
use TYPO3Fluid\Fluid\Core\Rendering\RenderingContextInterface;
18
use TYPO3Fluid\Fluid\Core\ViewHelper\AbstractViewHelper;
19

20
/**
21
 * Base class for all FlexForm related ViewHelpers
22
 */
23
abstract class AbstractFormViewHelper extends AbstractViewHelper
24
{
25
    const SCOPE = FormViewHelper::class;
26
    const SCOPE_VARIABLE_EXTENSIONNAME = 'extensionName';
27
    const SCOPE_VARIABLE_FORM = 'form';
28
    const SCOPE_VARIABLE_CONTAINER = 'container';
29
    const SCOPE_VARIABLE_GRIDS = 'grids';
30

31
    protected function overrideArgument(
32
        $name,
33
        $type,
34
        $description,
35
        $required = false,
36
        $defaultValue = null,
37
        $escape = null
38
    ) {
39
        if (version_compare(VersionNumberUtility::getCurrentTypo3Version(), '13.4', '>=')) {
42✔
40
            return parent::registerArgument($name, $type, $description, $required, $defaultValue, $escape);
6✔
41
        }
42
        return parent::overrideArgument($name, $type, $description, $required, $defaultValue, $escape);
36✔
43
    }
44

45
    /**
46
     * @return string
47
     */
48
    public function render()
49
    {
50
        return static::renderStatic($this->arguments, $this->buildRenderChildrenClosure(), $this->renderingContext);
40✔
51
    }
52

53
    protected function callRenderMethod(): string
54
    {
55
        return static::renderStatic(
276✔
56
            $this->arguments instanceof ArgumentCollection ? $this->arguments->getArrayCopy() : $this->arguments,
276✔
57
            $this->buildRenderChildrenClosure(),
276✔
58
            $this->renderingContext
276✔
59
        );
276✔
60
    }
61

62
    public static function renderStatic(
63
        array $arguments,
64
        \Closure $renderChildrenClosure,
65
        RenderingContextInterface $renderingContext
66
    ): string {
67
        $container = static::getContainerFromRenderingContext($renderingContext);
267✔
68
        if (method_exists(static::class, 'getComponent')) {
267✔
69
            $component = static::getComponent($renderingContext, $arguments, $renderChildrenClosure);
267✔
70
            // rendering child nodes with Form's last sheet as active container
71
            static::setContainerInRenderingContext($renderingContext, $component);
260✔
72
        }
73
        $renderChildrenClosure();
260✔
74
        static::setContainerInRenderingContext($renderingContext, $container);
260✔
75

76
        return '';
260✔
77
    }
78

79
    public static function getComponent(
80
        RenderingContextInterface $renderingContext,
81
        iterable $arguments
82
    ): FormInterface {
83
        return Form::create();
7✔
84
    }
85

86
    /**
87
     * @return mixed
88
     */
89
    public function renderChildren()
90
    {
91
        // Make sure the current extension name always propagates to child nodes
92
        static::setExtensionNameInRenderingContext(
138✔
93
            $this->renderingContext,
138✔
94
            static::getExtensionNameFromRenderingContextOrArguments($this->renderingContext, $this->arguments)
138✔
95
        );
138✔
96

97
        return parent::renderChildren();
138✔
98
    }
99

100
    protected static function setExtensionNameInRenderingContext(
101
        RenderingContextInterface $renderingContext,
102
        string $name
103
    ): void {
104
        $renderingContext->getViewHelperVariableContainer()
138✔
105
            ->addOrUpdate(static::SCOPE, static::SCOPE_VARIABLE_EXTENSIONNAME, $name);
138✔
106
    }
107

108
    protected static function getExtensionNameFromRenderingContextOrArguments(
109
        RenderingContextInterface $renderingContext,
110
        array $arguments
111
    ): string {
112
        if ($extensionName = $arguments[static::SCOPE_VARIABLE_EXTENSIONNAME] ?? false) {
443✔
113
            return (string) $extensionName;
7✔
114
        }
115
        $viewHelperVariableContainer = $renderingContext->getViewHelperVariableContainer();
436✔
116
        if ($extensionName = $viewHelperVariableContainer->get(static::SCOPE, static::SCOPE_VARIABLE_EXTENSIONNAME)) {
436✔
117
            return is_scalar($extensionName) ? (string) $extensionName : 'FluidTYPO3.Flux';
88✔
118
        }
119
        $request = null;
436✔
120
        $controllerContext = null;
436✔
121
        if (method_exists($renderingContext, 'getControllerContext')) {
436✔
122
            $controllerContext = $renderingContext->getControllerContext();
298✔
123
            if ($controllerContext && $controllerContext->getRequest()) {
298✔
124
                $request = $controllerContext->getRequest();
256✔
125
            }
126
        } elseif (method_exists($renderingContext, 'getRequest')) {
138✔
127
            $request = $renderingContext->getRequest();
138✔
128
        }
129
        if (!$request && $controllerContext) {
436✔
130
            $request = $controllerContext->getRequest();
×
131
            /** @var string|null $controllerExtensionName */
132
            $controllerExtensionName = $request->getControllerExtensionName();
×
133
            return $controllerExtensionName ?? 'FluidTYPO3.Flux';
×
134
        }
135
        return 'FluidTYPO3.Flux';
436✔
136
    }
137

138
    public static function getFormFromRenderingContext(RenderingContextInterface $renderingContext): Form
139
    {
140
        /** @var Form|null $form */
141
        $form = $renderingContext->getViewHelperVariableContainer()->get(static::SCOPE, static::SCOPE_VARIABLE_FORM);
462✔
142
        if (!$form) {
462✔
143
            $form = Form::create([
399✔
144
                'extensionName' => $renderingContext->getViewHelperVariableContainer()->get(
399✔
145
                    FormViewHelper::SCOPE,
399✔
146
                    FormViewHelper::SCOPE_VARIABLE_EXTENSIONNAME
399✔
147
                )
399✔
148
            ]);
399✔
149
            $renderingContext->getViewHelperVariableContainer()->add(static::SCOPE, static::SCOPE_VARIABLE_FORM, $form);
399✔
150
        }
151
        return $form;
462✔
152
    }
153

154
    protected static function getGridFromRenderingContext(
155
        RenderingContextInterface $renderingContext,
156
        string $gridName = 'grid'
157
    ): Grid {
158
        $viewHelperVariableContainer = $renderingContext->getViewHelperVariableContainer();
91✔
159
        /** @var Grid[] $grids */
160
        $grids = (array) $viewHelperVariableContainer->get(static::SCOPE, static::SCOPE_VARIABLE_GRIDS);
91✔
161

162
        if (!isset($grids[$gridName])) {
91✔
163
            $grids[$gridName] = Grid::create(['name' => $gridName]);
84✔
164
            $viewHelperVariableContainer->addOrUpdate(static::SCOPE, static::SCOPE_VARIABLE_GRIDS, $grids);
84✔
165
        }
166
        /** @var Grid $grid */
167
        $grid = $grids[$gridName];
91✔
168
        return $grid;
91✔
169
    }
170

171
    protected static function getContainerFromRenderingContext(
172
        RenderingContextInterface $renderingContext
173
    ): FormInterface {
174
        /** @var Form\FormInterface|null $container */
175
        $container = $renderingContext->getViewHelperVariableContainer()->get(
504✔
176
            static::SCOPE,
504✔
177
            static::SCOPE_VARIABLE_CONTAINER
504✔
178
        );
504✔
179
        return $container ?? static::getFormFromRenderingContext($renderingContext);
504✔
180
    }
181

182
    protected static function setContainerInRenderingContext(
183
        RenderingContextInterface $renderingContext,
184
        FormInterface $container
185
    ): void {
186
        $renderingContext->getViewHelperVariableContainer()->addOrUpdate(
280✔
187
            static::SCOPE,
280✔
188
            static::SCOPE_VARIABLE_CONTAINER,
280✔
189
            $container
280✔
190
        );
280✔
191
    }
192
}
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2026 Coveralls, Inc