[TASK] Make belog ViewHelpers compilable 02/40002/4
authorWouter Wolters <typo3@wouterwolters.nl>
Fri, 5 Jun 2015 17:40:01 +0000 (19:40 +0200)
committerMarkus Klein <markus.klein@typo3.org>
Sat, 6 Jun 2015 13:20:41 +0000 (15:20 +0200)
Resolves: #66749
Releases: master
Change-Id: I2a6e96eed3b754fe3887bfe33fe81ee5af0d7ee8
Reviewed-on: http://review.typo3.org/40002
Reviewed-by: Susanne Moog <typo3@susannemoog.de>
Tested-by: Susanne Moog <typo3@susannemoog.de>
Reviewed-by: Markus Klein <markus.klein@typo3.org>
Tested-by: Markus Klein <markus.klein@typo3.org>
typo3/sysext/belog/Classes/ViewHelpers/UsernameViewHelper.php
typo3/sysext/belog/Classes/ViewHelpers/WorkspaceTitleViewHelper.php

index 95761b1..c3de5f0 100644 (file)
@@ -14,19 +14,17 @@ namespace TYPO3\CMS\Belog\ViewHelpers;
  * The TYPO3 project - inspiring people to share!
  */
 
+use TYPO3\CMS\Fluid\Core\Rendering\RenderingContextInterface;
+use TYPO3\CMS\Fluid\Core\ViewHelper\AbstractViewHelper;
+use TYPO3\CMS\Fluid\Core\ViewHelper\Facets\CompilableInterface;
+
 /**
  * Get username from backend user id
  *
  * @author Christian Kuhn <lolli@schwarzbu.ch>
  * @internal
  */
-class UsernameViewHelper extends \TYPO3\CMS\Fluid\Core\ViewHelper\AbstractViewHelper {
-
-       /**
-        * @var \TYPO3\CMS\Extbase\Domain\Repository\BackendUserRepository
-        * @inject
-        */
-       protected $backendUserRepository;
+class UsernameViewHelper extends AbstractViewHelper implements CompilableInterface {
 
        /**
         * First level cache of user names
@@ -42,12 +40,33 @@ class UsernameViewHelper extends \TYPO3\CMS\Fluid\Core\ViewHelper\AbstractViewHe
         * @return string Username or an empty string if there is no user with that UID
         */
        public function render($uid) {
+               return self::renderStatic(
+                       array(
+                               'uid' => $uid
+                       ),
+                       $this->buildRenderChildrenClosure(),
+                       $this->renderingContext
+               );
+       }
+
+       /**
+        * @param array $arguments
+        * @param callable $renderChildrenClosure
+        * @param RenderingContextInterface $renderingContext
+        *
+        * @return string
+        */
+       static public function renderStatic(array $arguments, \Closure $renderChildrenClosure, RenderingContextInterface $renderingContext) {
+               $uid = $arguments['uid'];
+
                if (isset(static::$usernameRuntimeCache[$uid])) {
                        return static::$usernameRuntimeCache[$uid];
                }
 
+               $objectManager = \TYPO3\CMS\Core\Utility\GeneralUtility::makeInstance(\TYPO3\CMS\Extbase\Object\ObjectManager::class);
+               $backendUserRepository = $objectManager->get(\TYPO3\CMS\Extbase\Domain\Repository\BackendUserRepository::class);
                /** @var $user \TYPO3\CMS\Extbase\Domain\Model\BackendUser */
-               $user = $this->backendUserRepository->findByUid($uid);
+               $user = $backendUserRepository->findByUid($uid);
                // $user may be NULL if user was deleted from DB, set it to empty string to always return a string
                static::$usernameRuntimeCache[$uid] = ($user === NULL) ? '' : $user->getUserName();
                return static::$usernameRuntimeCache[$uid];
index c2927a7..0d771e2 100644 (file)
@@ -14,19 +14,17 @@ namespace TYPO3\CMS\Belog\ViewHelpers;
  * The TYPO3 project - inspiring people to share!
  */
 
+use TYPO3\CMS\Fluid\Core\Rendering\RenderingContextInterface;
+use TYPO3\CMS\Fluid\Core\ViewHelper\AbstractViewHelper;
+use TYPO3\CMS\Fluid\Core\ViewHelper\Facets\CompilableInterface;
+
 /**
  * Get workspace title from workspace id
  *
  * @author Christian Kuhn <lolli@schwarzbu.ch>
  * @internal
  */
-class WorkspaceTitleViewHelper extends \TYPO3\CMS\Fluid\Core\ViewHelper\AbstractViewHelper {
-
-       /**
-        * @var \TYPO3\CMS\Belog\Domain\Repository\WorkspaceRepository
-        * @inject
-        */
-       protected $workspaceRepository = NULL;
+class WorkspaceTitleViewHelper extends AbstractViewHelper implements CompilableInterface {
 
        /**
         * First level cache of workspace titles
@@ -39,20 +37,41 @@ class WorkspaceTitleViewHelper extends \TYPO3\CMS\Fluid\Core\ViewHelper\Abstract
         * Resolve workspace title from UID.
         *
         * @param int $uid UID of the workspace
-        * @return string username or UID
+        * @return string workspace title or UID
         */
        public function render($uid) {
+               return self::renderStatic(
+                       array(
+                               'uid' => $uid
+                       ),
+                       $this->buildRenderChildrenClosure(),
+                       $this->renderingContext
+               );
+       }
+
+       /**
+        * @param array $arguments
+        * @param callable $renderChildrenClosure
+        * @param RenderingContextInterface $renderingContext
+        *
+        * @return string
+        */
+       static public function renderStatic(array $arguments, \Closure $renderChildrenClosure, RenderingContextInterface $renderingContext) {
+               $uid = $arguments['uid'];
+
                if (isset(static::$workspaceTitleRuntimeCache[$uid])) {
                        return static::$workspaceTitleRuntimeCache[$uid];
                }
 
                if ($uid === 0) {
-                       static::$workspaceTitleRuntimeCache[$uid] = \TYPO3\CMS\Extbase\Utility\LocalizationUtility::translate('live', $this->controllerContext->getRequest()->getControllerExtensionName());
+                       static::$workspaceTitleRuntimeCache[$uid] = \TYPO3\CMS\Extbase\Utility\LocalizationUtility::translate('live', $renderingContext->getControllerContext()->getRequest()->getControllerExtensionName());
                } elseif (!\TYPO3\CMS\Core\Utility\ExtensionManagementUtility::isLoaded('workspaces')) {
                        static::$workspaceTitleRuntimeCache[$uid] = '';
                } else {
+                       $objectManager = \TYPO3\CMS\Core\Utility\GeneralUtility::makeInstance(\TYPO3\CMS\Extbase\Object\ObjectManager::class);
+                       $workspaceRepository = $objectManager->get(\TYPO3\CMS\Belog\Domain\Repository\WorkspaceRepository::class);
                        /** @var $workspace \TYPO3\CMS\Belog\Domain\Model\Workspace */
-                       $workspace = $this->workspaceRepository->findByUid($uid);
+                       $workspace = $workspaceRepository->findByUid($uid);
                        // $workspace may be null, force empty string in this case
                        static::$workspaceTitleRuntimeCache[$uid] = ($workspace === NULL) ? '' : $workspace->getTitle();
                }