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

FluidTYPO3 / vhs / 12968903100

25 Jan 2025 10:19PM UTC coverage: 72.341%. Remained the same
12968903100

push

github

NamelessCoder
[BUGFIX] Guard against missing record in v:page.resources.fal

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

5550 of 7672 relevant lines covered (72.34%)

13.48 hits per line

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

29.41
/Classes/ViewHelpers/Page/Resources/FalViewHelper.php
1
<?php
2
namespace FluidTYPO3\Vhs\ViewHelpers\Page\Resources;
3

4
/*
5
 * This file is part of the FluidTYPO3/Vhs project under GPLv2 or later.
6
 *
7
 * For the full copyright and license information, please read the
8
 * LICENSE.md file that was distributed with this source code.
9
 */
10

11
use FluidTYPO3\Vhs\Service\PageService;
12
use FluidTYPO3\Vhs\Traits\SlideViewHelperTrait;
13
use FluidTYPO3\Vhs\ViewHelpers\Resource\Record\FalViewHelper as ResourcesFalViewHelper;
14
use TYPO3\CMS\Core\Context\Context;
15
use TYPO3\CMS\Core\Context\LanguageAspect;
16
use TYPO3\CMS\Core\Utility\GeneralUtility;
17

18
/**
19
 * Page FAL resource ViewHelper.
20
 *
21
 * Do not use the "uid" argument in the "Preview" section.
22
 * Instead, use the "record" argument and pass the entire record.
23
 * This bypasses visibility restrictions that normally apply when you attempt
24
 * to load a record by UID through TYPO3's PageRepository, which is what the
25
 * resource ViewHelpers do if you only pass uid.
26
 */
27
class FalViewHelper extends ResourcesFalViewHelper
28
{
29
    use SlideViewHelperTrait;
30

31
    const DEFAULT_TABLE = 'pages';
32
    const DEFAULT_FIELD = 'media';
33

34
    protected string $table = self::DEFAULT_TABLE;
35
    protected string $field = self::DEFAULT_FIELD;
36

37
    public function initializeArguments(): void
38
    {
39
        parent::initializeArguments();
6✔
40

41
        $this->overrideArgument('table', 'string', 'The table to lookup records.', false, static::DEFAULT_TABLE);
6✔
42
        $this->overrideArgument(
6✔
43
            'field',
6✔
44
            'string',
6✔
45
            'The field of the table associated to resources.',
6✔
46
            false,
6✔
47
            static::DEFAULT_FIELD
6✔
48
        );
6✔
49
        $this->registerSlideArguments();
6✔
50
    }
51

52
    public function getRecord(int $id): ?array
53
    {
54
        $record = parent::getRecord($id);
×
NEW
55
        if (!$this->isDefaultLanguage() && $record !== null) {
×
56
            /** @var PageService $pageService */
57
            $pageService = GeneralUtility::makeInstance(PageService::class);
×
58
            $pageRepository = $pageService->getPageRepository();
×
59
            $localisation = $pageRepository->getPageOverlay($record, $this->getCurrentLanguageUid());
×
60
            if (is_array($localisation)) {
×
61
                $record = $localisation;
×
62
            }
63
        }
64
        return $record;
×
65
    }
66

67
    public function getResources(array $record): array
68
    {
69
        return $this->getSlideRecords($record['uid']);
×
70
    }
71

72
    protected function getSlideRecordsFromPage(int $pageUid, ?int $limit): array
73
    {
74
        $pageRecord = $this->getRecord($pageUid);
×
75
        if ($pageRecord === null) {
×
76
            return [];
×
77
        }
78
        // NB: we call parent::getResources intentionally, as to not call the overridden
79
        // method on this class. Calling $this->getResources() would yield wrong result
80
        // for the purpose of this method.
81
        $resources = parent::getResources($pageRecord);
×
82
        if (null !== $limit && count($resources) > $limit) {
×
83
            $resources = array_slice($resources, 0, $limit);
×
84
        }
85
        return $resources;
×
86
    }
87

88
    protected function isDefaultLanguage(): bool
89
    {
90
        return $this->getCurrentLanguageUid() === 0;
×
91
    }
92

93
    protected function getCurrentLanguageUid(): int
94
    {
95
        if (class_exists(LanguageAspect::class)) {
×
96
            /** @var Context $context */
97
            $context = GeneralUtility::makeInstance(Context::class);
×
98
            /** @var LanguageAspect $languageAspect */
99
            $languageAspect = $context->getAspect('language');
×
100
            $languageUid = $languageAspect->getId();
×
101
        } else {
102
            $languageUid = $GLOBALS['TSFE']->sys_language_uid;
×
103
        }
104

105
        return (integer) $languageUid;
×
106
    }
107

108
    /**
109
     * AbstractRecordResource usually uses the current cObj as reference,
110
     * but the page is needed here
111
     */
112
    public function getActiveRecord(): array
113
    {
114
        return $GLOBALS['TSFE']->page;
×
115
    }
116
}
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