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

CPS-IT / handlebars-forms / 24175366870

09 Apr 2026 06:09AM UTC coverage: 0.582% (-0.007%) from 0.589%
24175366870

Pull #28

github

web-flow
Merge 6d820a2bc into 51332cec7
Pull Request #28: [FEATURE] Support configuration of rendering order for `SummaryPage`

0 of 15 new or added lines in 2 files covered. (0.0%)

1 existing line in 1 file now uncovered.

7 of 1203 relevant lines covered (0.58%)

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
    ) {}
×
44

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

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

58
        // Resolve rendering order
NEW
59
        if (is_string($configuration['order'] ?? null)) {
×
NEW
60
            $order = RenderingOrder::from($configuration['order']);
×
61
        } else {
NEW
62
            $order = RenderingOrder::determineFromRenderable($renderable);
×
63
        }
64

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

107
        // Add renderables count to TSFE register
108
        // @todo Use $this->request->getAttribute('frontend.register.stack') in TYPO3 v14
109
        $tsfe = $this->getTypoScriptFrontendController();
×
110
        $tsfe->register[self::IDENTIFIER_COUNT] = count($renderables);
×
111

112
        foreach ($renderables as $index => $child) {
×
113
            if (!$this->isEnabled($child)) {
×
114
                continue;
×
115
            }
116

117
            if (array_key_exists($child->getType() . '.', $configuration)) {
×
118
                // Use configured type-specific configuration (e.g. "Fieldset." for fieldsets)
119
                $childConfiguration = $configuration[$child->getType() . '.'];
×
120
            } elseif (!array_key_exists('default.', $configuration)) {
×
121
                // Skip rendering on missing fallback config
122
                continue;
×
123
            } else {
124
                // Use configured fallback configuration ("default.")
125
                $childConfiguration = $configuration['default.'];
×
126
            }
127

128
            if (is_array($childConfiguration)) {
×
129
                $childViewModel = $this->buildViewModel($child, $context->renderingContext);
×
130
            } else {
131
                $childConfiguration = [];
×
132
                $childViewModel = new Domain\ViewModel\SimpleViewModel($child);
×
133
            }
134

135
            // Add current renderable index to TSFE register
136
            $tsfe->register[self::IDENTIFIER_CURRENT] = $index;
×
137

138
            try {
139
                $processedChild = $context->process($childConfiguration, $child, $childViewModel);
×
140
            } finally {
141
                unset($tsfe->register[self::IDENTIFIER_CURRENT]);
×
142
            }
143

144
            if ($processedChild !== null) {
×
145
                $processedRenderables[] = $processedChild;
×
146
            }
147
        }
148

149
        unset($tsfe->register[self::IDENTIFIER_COUNT]);
×
150

151
        return $processedRenderables;
×
152
    }
153

154
    private function buildViewModel(
×
155
        Form\Domain\Model\Renderable\RootRenderableInterface $renderable,
156
        Fluid\Core\Rendering\RenderingContext $renderingContext,
157
    ): Domain\ViewModel\ViewModel {
158
        foreach ($this->viewModelBuilders as $viewModelBuilder) {
×
159
            if ($viewModelBuilder->supports($renderable)) {
×
160
                return $viewModelBuilder->build($renderable, $renderingContext);
×
161
            }
162
        }
163

164
        return new Domain\ViewModel\SimpleViewModel($renderable);
×
165
    }
166

167
    private function isElement(Form\Domain\Model\Renderable\RenderableInterface $renderable): bool
×
168
    {
169
        return $renderable instanceof Form\Domain\Model\FormElements\FormElementInterface
×
170
            && $this->isEnabled($renderable)
×
171
        ;
×
172
    }
173

NEW
174
    private function isTopLevelElement(Form\Domain\Model\Renderable\RenderableInterface $renderable): bool
×
175
    {
NEW
176
        return $this->isElement($renderable)
×
NEW
177
            && $renderable->getParentRenderable() instanceof Form\Domain\Model\FormElements\Page
×
NEW
178
        ;
×
179
    }
180

UNCOV
181
    private function isEnabled(Form\Domain\Model\Renderable\RenderableInterface $renderable): bool
×
182
    {
183
        if (!$renderable->isEnabled()) {
×
184
            return false;
×
185
        }
186

187
        while (($renderable = $renderable->getParentRenderable()) !== null) {
×
188
            if (!$renderable->isEnabled()) {
×
189
                return false;
×
190
            }
191
        }
192

193
        return true;
×
194
    }
195
}
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