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

Yoast / Yoast-SEO-for-TYPO3 / 13327579701

14 Feb 2025 10:43AM UTC coverage: 1.276%. First build
13327579701

push

github

web-flow
Merge pull request #597 from Yoast/feature/v11

[FEATURE] Release 11.0.0

21 of 894 new or added lines in 76 files covered. (2.35%)

35 of 2744 relevant lines covered (1.28%)

0.04 hits per line

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

0.0
/Classes/Service/SnippetPreview/SnippetPreviewUrlGenerator.php
1
<?php
2

3
declare(strict_types=1);
4

5
namespace YoastSeoForTypo3\YoastSeo\Service\SnippetPreview;
6

7
use TYPO3\CMS\Backend\Utility\BackendUtility;
8
use TYPO3\CMS\Core\Domain\Repository\PageRepository;
9
use TYPO3\CMS\Core\Utility\GeneralUtility;
10
use TYPO3\CMS\Frontend\Page\CacheHashCalculator;
11
use YoastSeoForTypo3\YoastSeo\Service\UrlService;
12

13
class SnippetPreviewUrlGenerator
14
{
15
    public function __construct(
16
        protected CacheHashCalculator $cacheHashCalculator,
17
        protected UrlService $urlService
NEW
18
    ) {}
×
19

20
    /**
21
     * @param array<string, mixed> $data
22
     */
23
    public function getPreviewUrl(array $data): string
24
    {
NEW
25
        $currentPageId = (int)$data['effectivePid'];
×
NEW
26
        $recordId = (int)$data['vanillaUid'];
×
NEW
27
        $tableName = $data['tableName'];
×
28

NEW
29
        $recordArray = $this->getRecordArray($tableName, $recordId);
×
NEW
30
        $previewConfiguration = $this->getPreviewConfiguration($currentPageId, $tableName);
×
31

NEW
32
        $previewPageId = $this->getPreviewPageId($currentPageId, $previewConfiguration);
×
33

NEW
34
        $linkParameters = [];
×
NEW
35
        $languageId = 0;
×
36
        // language handling
NEW
37
        $languageField = $GLOBALS['TCA'][$tableName]['ctrl']['languageField'] ?? '';
×
38

NEW
39
        if ($languageField && !empty($recordArray[$languageField])) {
×
NEW
40
            $l18nPointer = $GLOBALS['TCA'][$tableName]['ctrl']['transOrigPointerField'] ?? '';
×
NEW
41
            if ($l18nPointer && !empty($recordArray[$l18nPointer])) {
×
NEW
42
                if (isset($previewConfiguration['useDefaultLanguageRecord'])
×
NEW
43
                    && !$previewConfiguration['useDefaultLanguageRecord']) {
×
44
                    // use parent record
NEW
45
                    $recordId = $recordArray[$l18nPointer];
×
46
                }
47

NEW
48
                if ($tableName === 'pages') {
×
NEW
49
                    $previewPageId = $recordArray[$l18nPointer];
×
50
                }
51
            }
NEW
52
            $languageId = $recordArray[$languageField] > -1 ? $recordArray[$languageField] : 0;
×
53
        }
54

55
        // map record data to GET parameters
NEW
56
        if (isset($previewConfiguration['fieldToParameterMap.'])) {
×
NEW
57
            foreach ($previewConfiguration['fieldToParameterMap.'] as $field => $parameterName) {
×
NEW
58
                $value = $recordArray[$field] ?? '';
×
NEW
59
                if ($field === 'uid') {
×
NEW
60
                    $value = $recordId;
×
61
                }
NEW
62
                $linkParameters[$parameterName] = $value;
×
63
            }
64
        }
65

66
        // add/override parameters by configuration
NEW
67
        if (isset($previewConfiguration['additionalGetParameters.'])) {
×
NEW
68
            $additionalGetParameters = [];
×
NEW
69
            $this->parseAdditionalGetParameters(
×
NEW
70
                $additionalGetParameters,
×
NEW
71
                $previewConfiguration['additionalGetParameters.']
×
NEW
72
            );
×
NEW
73
            $linkParameters = array_replace($linkParameters, $additionalGetParameters);
×
74
        }
75

NEW
76
        if (!empty($previewConfiguration['useCacheHash'])) {
×
NEW
77
            $fullLinkParameters = GeneralUtility::implodeArrayForUrl(
×
NEW
78
                '',
×
NEW
79
                array_merge($linkParameters, ['id' => $previewPageId])
×
NEW
80
            );
×
NEW
81
            $cacheHashParameters = $this->cacheHashCalculator->getRelevantParameters($fullLinkParameters);
×
NEW
82
            $linkParameters['cHash'] = $this->cacheHashCalculator->calculateCacheHash($cacheHashParameters);
×
83
        }
84

NEW
85
        $additionalParamsForUrl = GeneralUtility::implodeArrayForUrl('', $linkParameters, '', false, true);
×
86

NEW
87
        return $this->urlService->getPreviewUrl($previewPageId, $languageId, $additionalParamsForUrl);
×
88
    }
89

90
    /**
91
     * @param array<string, mixed> $previewConfiguration
92
     */
93
    protected function getPreviewPageId(int $currentPageId, array $previewConfiguration): int
94
    {
95
        // find the right preview page id
NEW
96
        $previewPageId = (int)($previewConfiguration['previewPageId'] ?? 0);
×
97

98
        // if no preview page was configured
NEW
99
        if (!$previewPageId) {
×
NEW
100
            $rootPageData = null;
×
NEW
101
            $rootLine = BackendUtility::BEgetRootLine($currentPageId);
×
NEW
102
            $currentPage = reset($rootLine);
×
103
            // Allow all doktypes below 200
104
            // This makes custom doktype work as well with opening a frontend page.
NEW
105
            if ((int)$currentPage['doktype'] <= PageRepository::DOKTYPE_SPACER) {
×
106
                // try the current page
NEW
107
                $previewPageId = $currentPageId;
×
108
            } else {
109
                // or search for the root page
NEW
110
                foreach ($rootLine as $page) {
×
NEW
111
                    if ($page['is_siteroot']) {
×
NEW
112
                        $rootPageData = $page;
×
NEW
113
                        break;
×
114
                    }
115
                }
NEW
116
                $previewPageId = isset($rootPageData)
×
NEW
117
                    ? (int)$rootPageData['uid']
×
NEW
118
                    : $currentPageId;
×
119
            }
120
        }
NEW
121
        return $previewPageId;
×
122
    }
123

124
    /**
125
     * @return array<string, mixed>
126
     */
127
    protected function getRecordArray(string $tableName, int $recordId): array
128
    {
NEW
129
        return BackendUtility::getRecord($tableName, $recordId) ?? [];
×
130
    }
131

132
    /**
133
     * @return array<string, mixed>
134
     */
135
    protected function getPreviewConfiguration(int $currentPageId, string $tableName): array
136
    {
NEW
137
        $pageTsConfig = BackendUtility::getPagesTSconfig($currentPageId);
×
NEW
138
        return $pageTsConfig['TCEMAIN.']['preview.'][$tableName . '.'] ?? [];
×
139
    }
140

141
    /**
142
     * Migrates a set of (possibly nested) GET parameters in TypoScript syntax to a
143
     * plain array
144
     *
145
     * This basically removes the trailing dots of sub-array keys in TypoScript.
146
     * The result can be used to create a query string with
147
     * GeneralUtility::implodeArrayForUrl().
148
     *
149
     * @param array<string, mixed> $parameters Should be an empty array by default
150
     * @param array<string, mixed> $typoScript The TypoScript configuration
151
     */
152
    protected function parseAdditionalGetParameters(
153
        array &$parameters,
154
        array $typoScript
155
    ): void {
NEW
156
        foreach ($typoScript as $key => $value) {
×
NEW
157
            if (is_array($value)) {
×
NEW
158
                $key = rtrim($key, '.');
×
NEW
159
                $parameters[$key] = [];
×
NEW
160
                $this->parseAdditionalGetParameters($parameters[$key], $value);
×
161
            } else {
NEW
162
                $parameters[$key] = $value;
×
163
            }
164
        }
165
    }
166
}
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