(no commit message)
[paste/936.git] / Page / InheritedPropertyViewHelper.php
1 <?php\r
2 \r
3 declare(strict_types=1);\r
4 \r
5 namespace Vendor\MyExtension\ViewHelpers\Page;\r
6 \r
7 use TYPO3\CMS\Frontend\Controller\TypoScriptFrontendController;\r
8 use TYPO3Fluid\Fluid\Core\ViewHelper\AbstractViewHelper;\r
9 \r
10 /**\r
11  * Get a page row property, inherited up through the rootline.\r
12  *\r
13  * When a value is NULL or an empty string "", then the parent page's\r
14  * value is used.\r
15  *\r
16  * The column must be in the rootline already.\r
17  * This can be configured via TYPO3_CONF_VARS[FE][addRootLineFields].\r
18  */\r
19 class InheritedPropertyViewHelper extends AbstractViewHelper\r
20 {\r
21     protected $escapeOutput = false;\r
22 \r
23     public function initializeArguments(): void\r
24     {\r
25         $this->registerArgument(\r
26             'column',\r
27             'string',\r
28             'Page row column name',\r
29             true,\r
30         );\r
31         $this->registerArgument(\r
32             'default',\r
33             'string',\r
34             'Fallback value when all pages have an empty value',\r
35             true,\r
36         );\r
37     }\r
38 \r
39     public function render(): mixed\r
40     {\r
41         $column = $this->arguments['column'];\r
42         $rootLine = $this->getRootLine();\r
43 \r
44         foreach ($rootLine as $pageRow) {\r
45             if (!array_key_exists($column, $pageRow)) {\r
46                 throw new \OutOfRangeException(\r
47                     'Column is not part of the rootline: ' . $column,\r
48                     1758882723\r
49                 );\r
50             }\r
51 \r
52             if ($pageRow[$column] !== null && $pageRow[$column] !== '') {\r
53                 return $pageRow[$column];\r
54             }\r
55         }\r
56 \r
57         return $this->arguments['default'];\r
58     }\r
59 \r
60     protected function getRootLine(): array\r
61     {\r
62         return $this->getFrontendController()->rootLine;\r
63     }\r
64 \r
65     protected function getFrontendController(): TypoScriptFrontendController\r
66     {\r
67         return $GLOBALS['TSFE'];\r
68     }\r
69 }\r