Completed
Push — master ( 76a0e0...7a82fc )
by
unknown
17:28
created

TcaSlug::addData()   B

Complexity

Conditions 7
Paths 15

Size

Total Lines 32
Code Lines 19

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 7
eloc 19
c 1
b 0
f 0
nc 15
nop 1
dl 0
loc 32
rs 8.8333
1
<?php
2
declare(strict_types=1);
3
namespace TYPO3\CMS\Backend\Form\FormDataProvider;
4
5
/*
6
 * This file is part of the TYPO3 CMS project.
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
use TYPO3\CMS\Backend\Form\FormDataProviderInterface;
19
use TYPO3\CMS\Core\Site\Entity\SiteInterface;
20
use TYPO3\CMS\Core\Utility\GeneralUtility;
21
22
/**
23
 * Handles custom data for TCA Type=Slug
24
 */
25
class TcaSlug implements FormDataProviderInterface
26
{
27
    /**
28
     * Resolve slug prefix items
29
     *
30
     * @param array $result
31
     *
32
     * @return array
33
     * @throws \UnexpectedValueException
34
     */
35
    public function addData(array $result): array
36
    {
37
        $table = $result['tableName'];
38
        $site = $result['site'];
39
        $row = $result['databaseRow'];
40
        $languageId = 0;
41
42
        if (($result['processedTca']['ctrl']['languageField'] ?? '') !== '') {
43
            $languageField = $result['processedTca']['ctrl']['languageField'];
44
            $languageId = (int)((is_array($row[$languageField]) ? $row[$languageField][0] : $row[$languageField]) ?? 0);
45
        }
46
47
        foreach ($result['processedTca']['columns'] as $fieldName => $fieldConfig) {
48
            if (($fieldConfig['config']['type'] ?? '') !== 'slug') {
49
                continue;
50
            }
51
52
            $prefix = $fieldConfig['config']['appearance']['prefix'] ?? '';
53
54
            if ($prefix !== '') {
55
                $parameters = ['site' => $site, 'languageId' => $languageId, 'table' => $table, 'row' => $row];
56
                $prefix = GeneralUtility::callUserFunction($prefix, $parameters, $this);
57
            } elseif ($site instanceof SiteInterface) {
58
                // default behaviour used for pages
59
                $prefix = $this->getPrefixForSite($site, $languageId);
60
            }
61
62
            $result['customData'][$fieldName]['slugPrefix'] = $prefix;
63
            $result['processedTca']['columns'][$fieldName]['config']['appearance']['prefix'] = $prefix;
64
        }
65
66
        return $result;
67
    }
68
69
    /**
70
     * Render the prefix for the input field.
71
     *
72
     * @param SiteInterface $site
73
     * @param int $languageId
74
     * @return string
75
     */
76
    protected function getPrefixForSite(SiteInterface $site, int $languageId): string
77
    {
78
        try {
79
            $language = ($languageId < 0) ? $site->getDefaultLanguage() : $site->getLanguageById($languageId);
80
            $base = $language->getBase();
81
            $prefix = rtrim((string)$base, '/');
82
            if ($prefix !== '' && empty($base->getScheme()) && $base->getHost() !== '') {
83
                $prefix = 'http:' . $prefix;
84
            }
85
        } catch (\InvalidArgumentException $e) {
86
            // No site found
87
            $prefix = '';
88
        }
89
90
        return $prefix;
91
    }
92
}
93