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

CPS-IT / handlebars-forms / 23844134334

01 Apr 2026 10:28AM UTC coverage: 0.592%. Remained the same
23844134334

push

github

web-flow
Merge pull request #21 from CPS-IT/task/register

[TASK] Use register from TSFE to populate renderable information

0 of 5 new or added lines in 1 file covered. (0.0%)

1 existing line in 1 file now uncovered.

7 of 1183 relevant lines covered (0.59%)

0.02 hits per line

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

0.0
/Classes/ContentObject/RenderablesContentObject.php
1
<?php
2

3
declare(strict_types=1);
4

5
/*
6
 * This file is part of the TYPO3 CMS extension "handlebars_forms".
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

18
namespace CPSIT\Typo3HandlebarsForms\ContentObject;
19

20
use CPSIT\Typo3HandlebarsForms\Domain;
21
use Symfony\Component\DependencyInjection;
22
use TYPO3\CMS\Fluid;
23
use TYPO3\CMS\Form;
24

25
/**
26
 * RenderablesContentObject
27
 *
28
 * @author Elias Häußler <e.haeussler@familie-redlich.de>
29
 * @license GPL-2.0-or-later
30
 */
31
#[DependencyInjection\Attribute\AutoconfigureTag('frontend.contentobject', ['identifier' => 'HBS_RENDERABLES'])]
32
final class RenderablesContentObject extends AbstractHandlebarsFormsContentObject
33
{
34
    private const IDENTIFIER_COUNT = 'HBS_RENDERABLES_COUNT';
35
    private const IDENTIFIER_CURRENT = 'HBS_RENDERABLES_CURRENT';
36

37
    /**
38
     * @param iterable<Domain\ViewModel\Builder\ViewModelBuilder<Form\Domain\Model\Renderable\RootRenderableInterface>> $viewModelBuilders
39
     */
40
    public function __construct(
×
41
        #[DependencyInjection\Attribute\AutowireIterator('handlebars_forms.view_model_builder')]
42
        private readonly iterable $viewModelBuilders,
43
        private readonly PassthroughContentObject $passthroughContentObject,
44
    ) {}
×
45

46
    /**
47
     * @return list<mixed>
48
     */
49
    protected function resolve(array $configuration, Context\ValueResolutionContext $context): array
×
50
    {
51
        $baseRenderable = $renderable = $context->renderable;
×
52
        $processedRenderables = [];
×
53

54
        // Use current page as base renderable if we're on root form context
55
        if ($baseRenderable instanceof Form\Domain\Runtime\FormRuntime) {
×
56
            $renderable = $baseRenderable->getCurrentPage() ?? $baseRenderable;
×
57
        }
58

59
        // Fetch renderables from base renderable:
60
        // - On summary pages, the base renderable defines the selection of renderables:
61
        //   + If the incoming renderable is the summary page, we use ALL elements (no sections) of the configured form.
62
        //   + If the incoming renderable is the root form, we explicitly render the summary page renderable
63
        //     to allow further configuration of this specific page type. In TypoScript, the form renderables may
64
        //     still be rendered for summary pages by using a combination of HBS_RENDERABLES objects for form & page:
65
        //       formData {
66
        //         items = HBS_RENDERABLES
67
        //         items {
68
        //           # ...
69
        //           SummaryPage {
70
        //             elements = HBS_RENDERABLES
71
        //             elements {
72
        //               Text { ... }
73
        //               # ...
74
        //             }
75
        //           }
76
        //         }
77
        //       }
78
        // - On default sections (e.g. non-summary pages), this reflects all direct children.
79
        // - On all other composite renderables, this reflects all renderables recursively (including deeply nested
80
        //   renderables).
81
        // - If we have a non-composite base renderable in place, we do nothing since this value resolver only handles
82
        //   composite renderables.
83
        if ($baseRenderable instanceof Form\Domain\Model\FormElements\Page && $baseRenderable->getType() === 'SummaryPage') {
×
84
            $renderables = array_filter(
×
85
                $baseRenderable->getRootForm()->getRenderablesRecursively(),
×
86
                $this->isElement(...),
×
87
            );
×
88
        } elseif ($renderable instanceof Form\Domain\Model\FormElements\Page && $renderable->getType() === 'SummaryPage') {
×
89
            $renderables = [$renderable];
×
90
        } elseif ($renderable instanceof Form\Domain\Model\FormElements\AbstractSection) {
×
91
            $renderables = $renderable->getElements();
×
92
        } elseif ($renderable instanceof Form\Domain\Model\Renderable\CompositeRenderableInterface) {
×
93
            $renderables = $renderable->getRenderablesRecursively();
×
94
        } else {
95
            $renderables = [];
×
96
        }
97

98
        // Add renderables count to TSFE register
99
        // @todo Use $this->request->getAttribute('frontend.register.stack') in TYPO3 v14
NEW
100
        $tsfe = $this->getTypoScriptFrontendController();
×
NEW
101
        $tsfe->register[self::IDENTIFIER_COUNT] = count($renderables);
×
102

103
        foreach ($renderables as $index => $child) {
×
104
            if (!$this->isEnabled($child)) {
×
105
                continue;
×
106
            }
107

108
            if (!array_key_exists($child->getType() . '.', $configuration)) {
×
109
                $processedRenderables[] = $this->passthroughContentObject->render();
×
110

111
                continue;
×
112
            }
113

114
            $childConfiguration = $configuration[$child->getType() . '.'];
×
115

116
            if (is_array($childConfiguration)) {
×
117
                $childViewModel = $this->buildViewModel($child, $context->renderingContext);
×
118
            } else {
119
                $childConfiguration = [];
×
120
                $childViewModel = new Domain\ViewModel\SimpleViewModel($child);
×
121
            }
122

123
            // Add current renderable index to TSFE register
NEW
124
            $tsfe->register[self::IDENTIFIER_CURRENT] = $index;
×
125

126
            try {
127
                $processedChild = $context->process($childConfiguration, $child, $childViewModel);
×
128
            } finally {
NEW
129
                unset($tsfe->register[self::IDENTIFIER_CURRENT]);
×
130
            }
131

132
            if ($processedChild !== null) {
×
133
                $processedRenderables[] = $processedChild;
×
134
            }
135
        }
136

NEW
137
        unset($tsfe->register[self::IDENTIFIER_COUNT]);
×
138

UNCOV
139
        return $processedRenderables;
×
140
    }
141

142
    private function buildViewModel(
×
143
        Form\Domain\Model\Renderable\RootRenderableInterface $renderable,
144
        Fluid\Core\Rendering\RenderingContext $renderingContext,
145
    ): Domain\ViewModel\ViewModel {
146
        foreach ($this->viewModelBuilders as $viewModelBuilder) {
×
147
            if ($viewModelBuilder->supports($renderable)) {
×
148
                return $viewModelBuilder->build($renderable, $renderingContext);
×
149
            }
150
        }
151

152
        return new Domain\ViewModel\SimpleViewModel($renderable);
×
153
    }
154

155
    private function isElement(Form\Domain\Model\Renderable\RenderableInterface $renderable): bool
×
156
    {
157
        return $renderable instanceof Form\Domain\Model\FormElements\FormElementInterface
×
158
            && $this->isEnabled($renderable)
×
159
        ;
×
160
    }
161

162
    private function isEnabled(Form\Domain\Model\Renderable\RenderableInterface $renderable): bool
×
163
    {
164
        if (!$renderable->isEnabled()) {
×
165
            return false;
×
166
        }
167

168
        while (($renderable = $renderable->getParentRenderable()) !== null) {
×
169
            if (!$renderable->isEnabled()) {
×
170
                return false;
×
171
            }
172
        }
173

174
        return true;
×
175
    }
176
}
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