‪TYPO3CMS  10.4
TcaSlug.php
Go to the documentation of this file.
1 <?php
2 
3 declare(strict_types=1);
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 
19 
23 
28 {
37  public function ‪addData(array $result): array
38  {
39  $table = $result['tableName'];
40  $site = $result['site'];
41  $row = $result['databaseRow'];
42  $languageId = 0;
43 
44  if (($result['processedTca']['ctrl']['languageField'] ?? '') !== '') {
45  $languageField = $result['processedTca']['ctrl']['languageField'];
46  $languageId = (int)((is_array($row[$languageField]) ? $row[$languageField][0] : $row[$languageField]) ?? 0);
47  }
48 
49  foreach ($result['processedTca']['columns'] as $fieldName => $fieldConfig) {
50  if (($fieldConfig['config']['type'] ?? '') !== 'slug') {
51  continue;
52  }
53 
54  $prefix = $fieldConfig['config']['appearance']['prefix'] ?? '';
55 
56  if ($prefix !== '') {
57  $parameters = ['site' => $site, 'languageId' => $languageId, 'table' => $table, 'row' => $row];
58  $prefix = GeneralUtility::callUserFunction($prefix, $parameters, $this);
59  } elseif ($site instanceof ‪SiteInterface) {
60  // default behaviour used for pages
61  $prefix = $this->‪getPrefixForSite($site, $languageId);
62  }
63 
64  $result['customData'][$fieldName]['slugPrefix'] = $prefix;
65  $result['processedTca']['columns'][$fieldName]['config']['appearance']['prefix'] = $prefix;
66  }
67 
68  return $result;
69  }
70 
78  protected function ‪getPrefixForSite(‪SiteInterface $site, int $languageId): string
79  {
80  try {
81  $language = ($languageId < 0) ? $site->‪getDefaultLanguage() : $site->‪getLanguageById($languageId);
82  $base = $language->‪getBase();
83  $prefix = rtrim((string)$base, '/');
84  if ($prefix !== '' && empty($base->getScheme()) && $base->getHost() !== '') {
85  $prefix = 'http:' . $prefix;
86  }
87  } catch (\InvalidArgumentException $e) {
88  // No site found
89  $prefix = '';
90  }
91 
92  return $prefix;
93  }
94 }
‪TYPO3\CMS\Core\Site\Entity\SiteInterface
Definition: SiteInterface.php:26
‪TYPO3\CMS\Backend\Form\FormDataProvider\TcaSlug\getPrefixForSite
‪string getPrefixForSite(SiteInterface $site, int $languageId)
Definition: TcaSlug.php:78
‪TYPO3\CMS\Backend\Form\FormDataProvider\TcaSlug\addData
‪array addData(array $result)
Definition: TcaSlug.php:37
‪TYPO3\CMS\Backend\Form\FormDataProvider\TcaSlug
Definition: TcaSlug.php:28
‪TYPO3\CMS\Core\Site\Entity\SiteInterface\getDefaultLanguage
‪SiteLanguage getDefaultLanguage()
‪TYPO3\CMS\Core\Site\Entity\SiteInterface\getLanguageById
‪SiteLanguage getLanguageById(int $languageId)
‪TYPO3\CMS\Backend\Form\FormDataProvider
Definition: AbstractDatabaseRecordProvider.php:16
‪TYPO3\CMS\Backend\Form\FormDataProviderInterface
Definition: FormDataProviderInterface.php:23
‪TYPO3\CMS\Core\Site\Entity\SiteLanguage\getBase
‪UriInterface getBase()
Definition: SiteLanguage.php:214
‪TYPO3\CMS\Core\Utility\GeneralUtility
Definition: GeneralUtility.php:46