‪TYPO3CMS  10.4
UsernameViewHelper.php
Go to the documentation of this file.
1 <?php
2 
3 /*
4  * This file is part of the TYPO3 CMS project.
5  *
6  * It is free software; you can redistribute it and/or modify it under
7  * the terms of the GNU General Public License, either version 2
8  * of the License, or any later version.
9  *
10  * For the full copyright and license information, please read the
11  * LICENSE.txt file that was distributed with this source code.
12  *
13  * The TYPO3 project - inspiring people to share!
14  */
15 
17 
21 use TYPO3Fluid\Fluid\Core\Rendering\RenderingContextInterface;
22 use TYPO3Fluid\Fluid\Core\ViewHelper\AbstractViewHelper;
23 use TYPO3Fluid\Fluid\Core\ViewHelper\Traits\CompileWithRenderStatic;
24 
29 class ‪UsernameViewHelper extends AbstractViewHelper
30 {
31  use CompileWithRenderStatic;
32 
38  protected static ‪$usernameRuntimeCache = [];
39 
43  public function ‪initializeArguments()
44  {
45  $this->registerArgument('uid', 'int', 'Uid of the user', true);
46  }
47 
57  public static function ‪renderStatic(array $arguments, \Closure $renderChildrenClosure, RenderingContextInterface $renderingContext)
58  {
59  $uid = $arguments['uid'];
60  if (isset(static::$usernameRuntimeCache[$uid])) {
61  return static::$usernameRuntimeCache[$uid];
62  }
63 
64  $objectManager = GeneralUtility::makeInstance(ObjectManager::class);
65  $backendUserRepository = $objectManager->get(BackendUserRepository::class);
67  $user = $backendUserRepository->findByUid($uid);
68  // $user may be NULL if user was deleted from DB, set it to empty string to always return a string
69  static::$usernameRuntimeCache[$uid] = $user === null ? '' : $user->getUserName();
70  return static::$usernameRuntimeCache[$uid];
71  }
72 }
‪TYPO3\CMS\Belog\ViewHelpers\UsernameViewHelper\renderStatic
‪static string renderStatic(array $arguments, \Closure $renderChildrenClosure, RenderingContextInterface $renderingContext)
Definition: UsernameViewHelper.php:55
‪TYPO3\CMS\Belog\ViewHelpers
‪TYPO3\CMS\Belog\ViewHelpers\UsernameViewHelper\$usernameRuntimeCache
‪static array $usernameRuntimeCache
Definition: UsernameViewHelper.php:36
‪TYPO3\CMS\Extbase\Domain\Repository\BackendUserRepository
Definition: BackendUserRepository.php:27
‪TYPO3\CMS\Core\Utility\GeneralUtility
Definition: GeneralUtility.php:46
‪TYPO3\CMS\Extbase\Object\ObjectManager
Definition: ObjectManager.php:28
‪TYPO3\CMS\Belog\ViewHelpers\UsernameViewHelper\initializeArguments
‪initializeArguments()
Definition: UsernameViewHelper.php:41
‪TYPO3\CMS\Belog\ViewHelpers\UsernameViewHelper
Definition: UsernameViewHelper.php:30