‪TYPO3CMS  ‪main
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 {
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 (($languageField = $result['processedTca']['ctrl']['languageField'] ?? '') !== '' && isset($row[$languageField])) {
43  $languageId = (int)(is_array($row[$languageField]) ? ($row[$languageField][0] ?? 0) : $row[$languageField]);
44  }
45 
46  foreach ($result['processedTca']['columns'] as $fieldName => $fieldConfig) {
47  if (($fieldConfig['config']['type'] ?? '') !== 'slug') {
48  continue;
49  }
50 
51  $prefix = $fieldConfig['config']['appearance']['prefix'] ?? '';
52 
53  if ($prefix !== '') {
54  $parameters = ['site' => $site, 'languageId' => $languageId, 'table' => $table, 'row' => $row];
55  $prefix = GeneralUtility::callUserFunction($prefix, $parameters, $this);
56  } elseif ($site instanceof ‪SiteInterface) {
57  // default behaviour used for pages
58  $prefix = $this->‪getPrefixForSite($site, $languageId);
59  }
60 
61  $result['customData'][$fieldName]['slugPrefix'] = $prefix;
62  $result['processedTca']['columns'][$fieldName]['config']['appearance']['prefix'] = $prefix;
63  }
64 
65  return $result;
66  }
67 
71  protected function ‪getPrefixForSite(‪SiteInterface $site, int $languageId): string
72  {
73  try {
74  $language = ($languageId < 0) ? $site->‪getDefaultLanguage() : $site->‪getLanguageById($languageId);
75  $base = $language->getBase();
76  $prefix = rtrim((string)$base, '/');
77  if ($prefix !== '' && empty($base->getScheme()) && $base->getHost() !== '') {
78  $prefix = 'http:' . $prefix;
79  }
80  } catch (\InvalidArgumentException $e) {
81  // No site found
82  $prefix = '';
83  }
84 
85  return $prefix;
86  }
87 }
‪TYPO3\CMS\Core\Site\Entity\SiteInterface
Definition: SiteInterface.php:26
‪TYPO3\CMS\Backend\Form\FormDataProvider\TcaSlug\getPrefixForSite
‪getPrefixForSite(SiteInterface $site, int $languageId)
Definition: TcaSlug.php:71
‪TYPO3\CMS\Backend\Form\FormDataProvider\TcaSlug
Definition: TcaSlug.php:28
‪TYPO3\CMS\Backend\Form\FormDataProvider\TcaSlug\addData
‪addData(array $result)
Definition: TcaSlug.php:35
‪TYPO3\CMS\Core\Site\Entity\SiteInterface\getDefaultLanguage
‪getDefaultLanguage()
‪TYPO3\CMS\Backend\Form\FormDataProvider
Definition: AbstractDatabaseRecordProvider.php:16
‪TYPO3\CMS\Backend\Form\FormDataProviderInterface
Definition: FormDataProviderInterface.php:23
‪TYPO3\CMS\Core\Utility\GeneralUtility
Definition: GeneralUtility.php:52
‪TYPO3\CMS\Core\Site\Entity\SiteInterface\getLanguageById
‪getLanguageById(int $languageId)