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

CPS-IT / handlebars-forms / 25665832270

11 May 2026 10:54AM UTC coverage: 0.55% (-0.006%) from 0.556%
25665832270

Pull #40

github

web-flow
Merge 047d4ff38 into e5bf32a28
Pull Request #40: [FEATURE] Allow passthrough to content object within `HBS_RENDERABLES`

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

4 existing lines in 1 file now uncovered.

7 of 1272 relevant lines covered (0.55%)

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 Context\ContextStack $contextStack,
44
        private readonly Context\ValueCollector $valueCollector,
UNCOV
45
    ) {}
×
46

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

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

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

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

106
        foreach ($renderables as $index => $child) {
×
107
            if (!$this->isEnabled($child)) {
×
108
                continue;
×
109
            }
110

111
            // Add current renderable index to TSFE register
UNCOV
112
            $tsfe->register[self::IDENTIFIER_CURRENT] = $index;
×
113

114
            try {
NEW
115
                if (array_key_exists($child->getType() . '.', $configuration)) {
×
116
                    // Use configured type-specific configuration (e.g. "Fieldset." for fieldsets)
NEW
117
                    $childConfiguration = $configuration[$child->getType() . '.'];
×
NEW
118
                } elseif (is_string($configuration[$child->getType()] ?? null)) {
×
119
                    // Render single content object without further configuration (e.g. HBS_PASSTHROUGH)
NEW
120
                    $processedRenderables[] = $this->renderRenderable(
×
NEW
121
                        $context,
×
NEW
122
                        $child,
×
NEW
123
                        $configuration[$child->getType()],
×
NEW
124
                    );
×
125

NEW
126
                    continue;
×
NEW
127
                } elseif (!array_key_exists('default.', $configuration)) {
×
128
                    // Skip rendering on missing fallback config
NEW
129
                    continue;
×
130
                } else {
131
                    // Use configured fallback configuration ("default.")
NEW
132
                    $childConfiguration = $configuration['default.'];
×
133
                }
134

NEW
135
                if (is_array($childConfiguration)) {
×
NEW
136
                    $childViewModel = $this->buildViewModel($child, $context->renderingContext);
×
137
                } else {
NEW
138
                    $childConfiguration = [];
×
NEW
139
                    $childViewModel = new Domain\ViewModel\SimpleViewModel($child);
×
140
                }
141

UNCOV
142
                $processedChild = $context->process($childConfiguration, $child, $childViewModel);
×
143

NEW
144
                if ($processedChild !== null) {
×
NEW
145
                    $processedRenderables[] = $processedChild;
×
146
                }
147
            } finally {
148
                unset($tsfe->register[self::IDENTIFIER_CURRENT]);
×
149
            }
150
        }
151

152
        unset($tsfe->register[self::IDENTIFIER_COUNT]);
×
153

154
        return $processedRenderables;
×
155
    }
156

NEW
157
    private function renderRenderable(
×
158
        Context\ValueResolutionContext $context,
159
        Form\Domain\Model\Renderable\RenderableInterface $renderable,
160
        string $contentObject,
161
    ): mixed {
NEW
162
        $this->contextStack->push($context->withRenderable($renderable));
×
163

164
        try {
NEW
165
            $result = $this->cObj?->cObjGetSingle($contentObject, []);
×
166
        } finally {
NEW
167
            $this->contextStack->pop();
×
168
        }
169

NEW
170
        if (is_string($result) && $this->valueCollector->has($result)) {
×
NEW
171
            return $this->valueCollector->load($result);
×
172
        }
173

NEW
174
        return $result;
×
175
    }
176

UNCOV
177
    private function buildViewModel(
×
178
        Form\Domain\Model\Renderable\RootRenderableInterface $renderable,
179
        Fluid\Core\Rendering\RenderingContext $renderingContext,
180
    ): Domain\ViewModel\ViewModel {
181
        foreach ($this->viewModelBuilders as $viewModelBuilder) {
×
182
            if ($viewModelBuilder->supports($renderable)) {
×
183
                return $viewModelBuilder->build($renderable, $renderingContext);
×
184
            }
185
        }
186

187
        return new Domain\ViewModel\SimpleViewModel($renderable);
×
188
    }
189

190
    private function isElement(Form\Domain\Model\Renderable\RenderableInterface $renderable): bool
×
191
    {
192
        return $renderable instanceof Form\Domain\Model\FormElements\FormElementInterface
×
193
            && $this->isEnabled($renderable)
×
194
        ;
×
195
    }
196

197
    private function isEnabled(Form\Domain\Model\Renderable\RenderableInterface $renderable): bool
×
198
    {
199
        if (!$renderable->isEnabled()) {
×
200
            return false;
×
201
        }
202

203
        while (($renderable = $renderable->getParentRenderable()) !== null) {
×
204
            if (!$renderable->isEnabled()) {
×
205
                return false;
×
206
            }
207
        }
208

209
        return true;
×
210
    }
211
}
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