[TASK] Make belog ViewHelpers compilable
[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\Fluid\Core\Rendering\RenderingContextInterface;
18 use TYPO3\CMS\Fluid\Core\ViewHelper\AbstractViewHelper;
19 use TYPO3\CMS\Fluid\Core\ViewHelper\Facets\CompilableInterface;
20
21 /**
22 * Get workspace title from workspace id
23 *
24 * @author Christian Kuhn <lolli@schwarzbu.ch>
25 * @internal
26 */
27 class WorkspaceTitleViewHelper extends AbstractViewHelper implements CompilableInterface {
28
29 /**
30 * First level cache of workspace titles
31 *
32 * @var array
33 */
34 static protected $workspaceTitleRuntimeCache = array();
35
36 /**
37 * Resolve workspace title from UID.
38 *
39 * @param int $uid UID of the workspace
40 * @return string workspace title or UID
41 */
42 public function render($uid) {
43 return self::renderStatic(
44 array(
45 'uid' => $uid
46 ),
47 $this->buildRenderChildrenClosure(),
48 $this->renderingContext
49 );
50 }
51
52 /**
53 * @param array $arguments
54 * @param callable $renderChildrenClosure
55 * @param RenderingContextInterface $renderingContext
56 *
57 * @return string
58 */
59 static public function renderStatic(array $arguments, \Closure $renderChildrenClosure, RenderingContextInterface $renderingContext) {
60 $uid = $arguments['uid'];
61
62 if (isset(static::$workspaceTitleRuntimeCache[$uid])) {
63 return static::$workspaceTitleRuntimeCache[$uid];
64 }
65
66 if ($uid === 0) {
67 static::$workspaceTitleRuntimeCache[$uid] = \TYPO3\CMS\Extbase\Utility\LocalizationUtility::translate('live', $renderingContext->getControllerContext()->getRequest()->getControllerExtensionName());
68 } elseif (!\TYPO3\CMS\Core\Utility\ExtensionManagementUtility::isLoaded('workspaces')) {
69 static::$workspaceTitleRuntimeCache[$uid] = '';
70 } else {
71 $objectManager = \TYPO3\CMS\Core\Utility\GeneralUtility::makeInstance(\TYPO3\CMS\Extbase\Object\ObjectManager::class);
72 $workspaceRepository = $objectManager->get(\TYPO3\CMS\Belog\Domain\Repository\WorkspaceRepository::class);
73 /** @var $workspace \TYPO3\CMS\Belog\Domain\Model\Workspace */
74 $workspace = $workspaceRepository->findByUid($uid);
75 // $workspace may be null, force empty string in this case
76 static::$workspaceTitleRuntimeCache[$uid] = ($workspace === NULL) ? '' : $workspace->getTitle();
77 }
78
79 return static::$workspaceTitleRuntimeCache[$uid];
80 }
81
82 }