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

FluidTYPO3 / flux / 12937879944

23 Jan 2025 08:54PM UTC coverage: 93.294% (-0.01%) from 93.304%
12937879944

push

github

NamelessCoder
[FEATURE] Add replicated overrideArgument method

Necessary due to breaking change in Fluid.

2 of 3 new or added lines in 1 file covered. (66.67%)

7039 of 7545 relevant lines covered (93.29%)

56.3 hits per line

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

92.75
/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', '>=')) {
36✔
NEW
40
            return parent::registerArgument($name, $type, $description, $required, $defaultValue, $escape);
×
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);
×
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);
228✔
68
        if (method_exists(static::class, 'getComponent')) {
228✔
69
            $component = static::getComponent($renderingContext, $arguments, $renderChildrenClosure);
228✔
70
            // rendering child nodes with Form's last sheet as active container
71
            static::setContainerInRenderingContext($renderingContext, $component);
222✔
72
        }
73
        $renderChildrenClosure();
222✔
74
        static::setContainerInRenderingContext($renderingContext, $container);
222✔
75

76
        return '';
222✔
77
    }
78

79
    public static function getComponent(
80
        RenderingContextInterface $renderingContext,
81
        iterable $arguments
82
    ): FormInterface {
83
        return Form::create();
6✔
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) {
382✔
113
            return (string) $extensionName;
6✔
114
        }
115
        $viewHelperVariableContainer = $renderingContext->getViewHelperVariableContainer();
376✔
116
        if ($extensionName = $viewHelperVariableContainer->get(static::SCOPE, static::SCOPE_VARIABLE_EXTENSIONNAME)) {
376✔
117
            return is_scalar($extensionName) ? (string) $extensionName : 'FluidTYPO3.Flux';
76✔
118
        }
119
        $request = null;
376✔
120
        $controllerContext = null;
376✔
121
        if (method_exists($renderingContext, 'getControllerContext')) {
376✔
122
            $controllerContext = $renderingContext->getControllerContext();
284✔
123
            if ($controllerContext && $controllerContext->getRequest()) {
284✔
124
                $request = $controllerContext->getRequest();
256✔
125
            }
126
        } elseif (method_exists($renderingContext, 'getRequest')) {
92✔
127
            $request = $renderingContext->getRequest();
92✔
128
        }
129
        if (!$request && $controllerContext) {
376✔
130
            $request = $controllerContext->getRequest();
×
131
            /** @var string|null $controllerExtensionName */
132
            $controllerExtensionName = $request->getControllerExtensionName();
×
133
            return $controllerExtensionName ?? 'FluidTYPO3.Flux';
×
134
        }
135
        return 'FluidTYPO3.Flux';
376✔
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);
396✔
142
        if (!$form) {
396✔
143
            $form = Form::create([
342✔
144
                'extensionName' => $renderingContext->getViewHelperVariableContainer()->get(
342✔
145
                    FormViewHelper::SCOPE,
342✔
146
                    FormViewHelper::SCOPE_VARIABLE_EXTENSIONNAME
342✔
147
                )
342✔
148
            ]);
342✔
149
            $renderingContext->getViewHelperVariableContainer()->add(static::SCOPE, static::SCOPE_VARIABLE_FORM, $form);
342✔
150
        }
151
        return $form;
396✔
152
    }
153

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

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

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

182
    protected static function setContainerInRenderingContext(
183
        RenderingContextInterface $renderingContext,
184
        FormInterface $container
185
    ): void {
186
        $renderingContext->getViewHelperVariableContainer()->addOrUpdate(
240✔
187
            static::SCOPE,
240✔
188
            static::SCOPE_VARIABLE_CONTAINER,
240✔
189
            $container
240✔
190
        );
240✔
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