[TASK] Move ViewHelper arguments to initializeArguments() in ext:belog
[Packages/TYPO3.CMS.git] / typo3 / sysext / belog / Classes / ViewHelpers / WorkspaceTitleViewHelper.php
1 <?php
2 namespace TYPO3\CMS\Belog\ViewHelpers;
3
4 /*
5 * This file is part of the TYPO3 CMS project.
6 *
7 * It is free software; you can redistribute it and/or modify it under
8 * the terms of the GNU General Public License, either version 2
9 * of the License, or any later version.
10 *
11 * For the full copyright and license information, please read the
12 * LICENSE.txt file that was distributed with this source code.
13 *
14 * The TYPO3 project - inspiring people to share!
15 */
16
17 use TYPO3\CMS\Belog\Domain\Repository\WorkspaceRepository;
18 use TYPO3\CMS\Core\Utility\ExtensionManagementUtility;
19 use TYPO3\CMS\Core\Utility\GeneralUtility;
20 use TYPO3\CMS\Extbase\Object\ObjectManager;
21 use TYPO3\CMS\Extbase\Utility\LocalizationUtility;
22 use TYPO3\CMS\Fluid\Core\Rendering\RenderingContext;
23 use TYPO3\CMS\Fluid\Core\ViewHelper\AbstractViewHelper;
24 use TYPO3Fluid\Fluid\Core\Rendering\RenderingContextInterface;
25
26 /**
27 * Get workspace title from workspace id
28 * @internal
29 */
30 class WorkspaceTitleViewHelper extends AbstractViewHelper
31 {
32 /**
33 * First level cache of workspace titles
34 *
35 * @var array
36 */
37 protected static $workspaceTitleRuntimeCache = array();
38
39 /**
40 * Initializes the arguments
41 */
42 public function initializeArguments()
43 {
44 parent::initializeArguments();
45 $this->registerArgument('uid', 'int', 'UID of the workspace', true);
46 }
47
48 /**
49 * Resolve workspace title from UID.
50 *
51 * @return string workspace title or UID
52 */
53 public function render()
54 {
55 return static::renderStatic(
56 $this->arguments,
57 $this->buildRenderChildrenClosure(),
58 $this->renderingContext
59 );
60 }
61
62 /**
63 * @param array $arguments
64 * @param \Closure $renderChildrenClosure
65 * @param RenderingContextInterface $renderingContext
66 *
67 * @return string
68 * @throws \InvalidArgumentException
69 */
70 public static function renderStatic(array $arguments, \Closure $renderChildrenClosure, RenderingContextInterface $renderingContext)
71 {
72 if (!$renderingContext instanceof RenderingContext) {
73 throw new \InvalidArgumentException('The given rendering context is not of type "TYPO3\CMS\Fluid\Core\Rendering\RenderingContext"', 1468363946);
74 }
75
76 $uid = $arguments['uid'];
77 if (isset(static::$workspaceTitleRuntimeCache[$uid])) {
78 return static::$workspaceTitleRuntimeCache[$uid];
79 }
80
81 if ($uid === 0) {
82 static::$workspaceTitleRuntimeCache[$uid] = LocalizationUtility::translate(
83 'live',
84 $renderingContext->getControllerContext()->getRequest()->getControllerExtensionName()
85 );
86 } elseif (!ExtensionManagementUtility::isLoaded('workspaces')) {
87 static::$workspaceTitleRuntimeCache[$uid] = '';
88 } else {
89 $objectManager = GeneralUtility::makeInstance(ObjectManager::class);
90 $workspaceRepository = $objectManager->get(WorkspaceRepository::class);
91 /** @var \TYPO3\CMS\Belog\Domain\Model\Workspace $workspace */
92 $workspace = $workspaceRepository->findByUid($uid);
93 // $workspace may be null, force empty string in this case
94 static::$workspaceTitleRuntimeCache[$uid] = $workspace === null ? '' : $workspace->getTitle();
95 }
96
97 return static::$workspaceTitleRuntimeCache[$uid];
98 }
99 }