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

FluidTYPO3 / vhs / 13566190336

27 Feb 2025 12:18PM UTC coverage: 72.127% (-0.6%) from 72.746%
13566190336

push

github

NamelessCoder
[TER] 7.1.0

5649 of 7832 relevant lines covered (72.13%)

20.01 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\ArgumentOverride;
13
use FluidTYPO3\Vhs\Traits\SlideViewHelperTrait;
14
use FluidTYPO3\Vhs\ViewHelpers\Resource\Record\FalViewHelper as ResourcesFalViewHelper;
15
use TYPO3\CMS\Core\Context\Context;
16
use TYPO3\CMS\Core\Context\LanguageAspect;
17
use TYPO3\CMS\Core\Utility\GeneralUtility;
18

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

33
    const DEFAULT_TABLE = 'pages';
34
    const DEFAULT_FIELD = 'media';
35

36
    protected string $table = self::DEFAULT_TABLE;
37
    protected string $field = self::DEFAULT_FIELD;
38

39
    public function initializeArguments(): void
40
    {
41
        parent::initializeArguments();
7✔
42

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

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

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

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

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

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

107
        return (integer) $languageUid;
×
108
    }
109

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