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

CPS-IT / handlebars-forms / 24332321847

13 Apr 2026 07:58AM UTC coverage: 0.58% (-0.003%) from 0.583%
24332321847

push

github

eliashaeussler
[BUGFIX] Allow content object rendering in `HBS_*` content objects

0 of 9 new or added lines in 3 files covered. (0.0%)

7 of 1207 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/AbstractHandlebarsFormsContentObject.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\Utility;
21
use Psr\Log;
22
use TYPO3\CMS\Frontend;
23

24
/**
25
 * AbstractHandlebarsFormsContentObject
26
 *
27
 * @author Elias Häußler <e.haeussler@familie-redlich.de>
28
 * @license GPL-2.0-or-later
29
 */
30
abstract class AbstractHandlebarsFormsContentObject extends Frontend\ContentObject\AbstractContentObject implements Log\LoggerAwareInterface
31
{
32
    use Log\LoggerAwareTrait;
33

34
    private Context\ContextStack $contextStack;
35
    private Context\ValueCollector $valueCollector;
36

37
    public function injectContextStack(Context\ContextStack $contextStack): void
×
38
    {
39
        $this->contextStack = $contextStack;
×
40
    }
41

42
    public function injectValueCollector(Context\ValueCollector $valueCollector): void
×
43
    {
44
        $this->valueCollector = $valueCollector;
×
45
    }
46

47
    /**
48
     * @param array<string|int, mixed> $conf
49
     */
50
    final public function render($conf = []): string
×
51
    {
52
        $context = $this->contextStack->current();
×
53

54
        if ($context === null) {
×
55
            $this->logger?->warning(
×
56
                'Using a HBS_* content object in other contexts than "process-form" data processor is not supported.',
×
57
            );
×
58

59
            return '';
×
60
        }
61

62
        // Resolve value and apply stdWrap (either on stringable value or by using value as currentValue in COR)
63
        $resolvedValue = $this->resolve($conf, $context);
×
64
        $value = $this->applyStdWrap($resolvedValue, $conf);
×
65

66
        // Strings can be returned without additional caching using ValueCollector,
67
        // since content objects are already designed to return string values
68
        if (is_string($value)) {
×
69
            return $value;
×
70
        }
71

72
        return $this->valueCollector->save($this, $value);
×
73
    }
74

75
    /**
76
     * @param array<string|int, mixed> $configuration
77
     */
78
    abstract protected function resolve(array $configuration, Context\ValueResolutionContext $context): mixed;
×
79

80
    /**
81
     * @param array<string|int, mixed> $configuration
82
     */
NEW
83
    protected function processGenericValue(string $value, array $configuration): string
×
84
    {
85
        try {
NEW
86
            $contentObject = $this->getContentObjectRenderer()->getContentObject($value);
×
NEW
87
        } catch (Frontend\ContentObject\Exception\ContentRenderingException) {
×
NEW
88
            return $value;
×
89
        }
90

NEW
91
        if ($contentObject === null) {
×
NEW
92
            return $value;
×
93
        }
94

NEW
95
        return $this->getContentObjectRenderer()->cObjGetSingle($value, $configuration);
×
96
    }
97

98
    /**
99
     * @param array<string|int, mixed> $configuration
100
     */
101
    private function applyStdWrap(mixed $value, array $configuration): mixed
×
102
    {
103
        if ($this->cObj === null || !is_array($configuration['stdWrap.'] ?? null)) {
×
104
            return $value;
×
105
        }
106

107
        $apply = fn(string $string) => $this->cObj->stdWrap($string, $configuration['stdWrap.']) ?? $value;
×
108

109
        // Backup and override current value
110
        $currentValue = $this->cObj->getCurrentVal();
×
111
        $this->cObj->setCurrentVal($this->convertValueToStringableValue($value));
×
112

113
        try {
114
            // Apply stdWrap directly on stringable value
115
            if (Utility\StringUtility::isStringable($value)) {
×
116
                return Utility\StringUtility::processStringable($value, $apply(...));
×
117
            }
118

119
            // Apply stdWrap on empty string, but give consumers the chance to perform actions
120
            // based on the current value (which reflects the non-stringable resolved value)
121
            return $apply('');
×
122
        } finally {
123
            // Restore previous current value
124
            $this->cObj?->setCurrentVal($currentValue);
×
125
        }
126
    }
127

128
    /**
129
     * Build stringable value for usage as current value in COR.
130
     * Passes through stringables and tries to convert scalar-arrays to a string list.
131
     */
132
    private function convertValueToStringableValue(mixed $value): ?string
×
133
    {
134
        if ($value === null) {
×
135
            return null;
×
136
        }
137

138
        if (Utility\StringUtility::isStringable($value)) {
×
139
            return (string)$value;
×
140
        }
141

142
        if (is_array($value)) {
×
143
            return implode(',', array_filter($value, is_scalar(...)));
×
144
        }
145

146
        return null;
×
147
    }
148
}
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