[TASK] Disable slug in pseudo sites and disable alias in sites
[Packages/TYPO3.CMS.git] / typo3 / sysext / core / Classes / Compatibility / PseudoSiteTcaDisplayCondition.php
1 <?php
2 declare(strict_types = 1);
3 namespace TYPO3\CMS\Core\Compatibility;
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\Core\Routing\SiteMatcher;
19 use TYPO3\CMS\Core\Site\Entity\Site;
20 use TYPO3\CMS\Core\Utility\GeneralUtility;
21
22 /**
23 * A display condition that returns true if the page we are dealing
24 * with is in a page tree that is represented by a PseudoSite object.
25 *
26 * This is used to suppress the 'slug' field in pseudo site page trees
27 * when editing page records and to show the alias field.
28 *
29 * Both "Pseudo sites" and "alias" db field will bite the dust in v10,
30 * so this is a temporary display condition for v9 only and thus marked internal.
31 *
32 * @internal Implementation and class will probably vanish in v10 without further notice
33 */
34 class PseudoSiteTcaDisplayCondition
35 {
36 /**
37 * Takes the given page id of the record and verifies if the page has
38 * a pseudo site object or a site object attached.
39 *
40 * @param array $parameters
41 * @return bool
42 * @throws \InvalidArgumentException
43 */
44 public function isInPseudoSite(array $parameters): bool
45 {
46 if (!isset($parameters['conditionParameters'][0])
47 || $parameters['conditionParameters'][0] !== 'pages'
48 || !isset($parameters['conditionParameters'][1])
49 || (!in_array($parameters['conditionParameters'][1], ['true', 'false'], true))
50 || empty($parameters['record']['uid'])
51 ) {
52 // Validate arguments
53 throw new \InvalidArgumentException(
54 'Invalid arguments using isInPseudoSite display condition',
55 1535055415
56 );
57 }
58
59 $defaultLanguagePageId = (int)$parameters['record']['uid'];
60 if (!empty($parameters['record']['l10n_parent'][0])) {
61 $defaultLanguagePageId = (int)$parameters['record']['l10n_parent'][0];
62 }
63
64 // Catch all: If not a "Site" object, it must be a PseudoSite or NullSite or whatever
65 // we may have invented. To be as robust as possible we just say "yes pseudo" here.
66 $isInPseudoSite = true;
67 $site = GeneralUtility::makeInstance(SiteMatcher::class)->matchByPageId($defaultLanguagePageId);
68 if ($site instanceof Site) {
69 $isInPseudoSite = false;
70 }
71
72 if ($parameters['conditionParameters'][1] === 'false') {
73 // Negate if requested
74 return !$isInPseudoSite;
75 }
76
77 return $isInPseudoSite;
78 }
79 }