There have been a lot of question on stackoverflow on how to get some fields of the current logged in user and display in the FrondEnd. Almost all of the answers were TypoScript based. But TypoScript is very limited on what can bring back as a result. What if there was a way that you can get the whole object in the FrondEnd including all its relations, just as all the other variables? Well, you are in for a treat, it is possible and quite easy. The solution in a word: ViewHelpers! So let's get to it.
First you need an extension where you can create the Viewhelper. A sitepackage would do just fine.
The first thing we need to do, is to declare the ViewHelper. So under your_extension/ext_localconf.php
$GLOBALS['TYPO3_CONF_VARS']['SYS']['fluid']['namespaces']['ak'] = ['Karavas\\AkSitePackage\\ViewHelpers'];
With this we just add the namespace ak and we can use it in fluid like this <ak:someViewHelper/>
Now that we defined the namespace it is time to create the class. Under your_extension/Classes/ViewHelpers/Data/ create the class UserViewHelper.php
<?php
namespace Karavas\AkSitePackage\ViewHelpers\Data;
use Closure;
use TYPO3\CMS\Core\Context\Context;
use TYPO3\CMS\Core\Utility\GeneralUtility;
use TYPO3\CMS\Extbase\Domain\Repository\FrontendUserRepository;
use TYPO3Fluid\Fluid\Core\Rendering\RenderingContextInterface;
use TYPO3Fluid\Fluid\Core\ViewHelper\Traits\CompileWithRenderStatic;
use TYPO3Fluid\Fluid\Core\ViewHelper\AbstractViewHelper;
class UserViewHelper extends AbstractViewHelper
{
use CompileWithRenderStatic;
public static function renderStatic(
array $arguments,
Closure $renderChildrenClosure,
RenderingContextInterface $renderingContext)
{
}
}
This is the basic declaration of the class. You can just copy and paste it. You will have to change the namespace to your namespace.
Now that we know how the class structure looks like, it is time to get the user. With TYPO3 10 the context API has been introduced so we will use it to fetch the user.
<?php
namespace Karavas\AkSitePackage\ViewHelpers\Data;
use Closure;
use TYPO3\CMS\Core\Context\Context;
use TYPO3\CMS\Core\Utility\GeneralUtility;
use TYPO3\CMS\Extbase\Domain\Repository\FrontendUserRepository;
use TYPO3Fluid\Fluid\Core\Rendering\RenderingContextInterface;
use TYPO3Fluid\Fluid\Core\ViewHelper\Traits\CompileWithRenderStatic;
use TYPO3Fluid\Fluid\Core\ViewHelper\AbstractViewHelper;
class UserViewHelper extends AbstractViewHelper
{
use CompileWithRenderStatic;
public static function renderStatic(
array $arguments,
Closure $renderChildrenClosure,
RenderingContextInterface $renderingContext
) {
$output = $renderChildrenClosure();
$userRepository = GeneralUtility::makeInstance(FrontendUserRepository::class);
$context = GeneralUtility::makeInstance(Context::class);
$userId = $context->getPropertyFromAspect('frontend.user', 'id');
$user = $userRepository->findByUid($userId);
}
}
So what we did here is to first instantiate the classes FrontendUserRepository and Context. Then we got the current logged in user id and we used to get the FrontEndUser object.
Now that we got our user, we have to assign the object to an ExtBase variable. In order to do that we have to use the $renderingContext->getVariableProvider() API
<?php
namespace Karavas\AkSitePackage\ViewHelpers\Data;
use Closure;
use TYPO3\CMS\Core\Context\Context;
use TYPO3\CMS\Core\Utility\GeneralUtility;
use TYPO3\CMS\Extbase\Domain\Repository\FrontendUserRepository;
use TYPO3Fluid\Fluid\Core\Rendering\RenderingContextInterface;
use TYPO3Fluid\Fluid\Core\ViewHelper\Traits\CompileWithRenderStatic;
use TYPO3Fluid\Fluid\Core\ViewHelper\AbstractViewHelper;
class UserViewHelper extends AbstractViewHelper
{
use CompileWithRenderStatic;
public static function renderStatic(
array $arguments,
Closure $renderChildrenClosure,
RenderingContextInterface $renderingContext
) {
$userRepository = GeneralUtility::makeInstance(FrontendUserRepository::class);
$context = GeneralUtility::makeInstance(Context::class);
$userId = $context->getPropertyFromAspect('frontend.user', 'id');
$user = $userRepository->findByUid($userId);
$renderingContext->getVariableProvider()->add(
'loggedInUser',
$user
);
return $output;
}
}
Now we added the variable loggedInUser in the FrontEnd.
Now that we added the variable on the FrontEnd, it is time to use the ViewHelper. Idealy you can use the ViewHelper on your Layouts. This way the user object will be available to all the templates that use this Layout. So under your_extension/Resources/Private/Layouts/SomeLayout.html you will have to add the following.
<html xmlns:f="http://typo3.org/ns/TYPO3/CMS/Fluid/ViewHelpers" data-namespace-typo3-fluid="true">
<ak:data.user/>
<f:render section="Main"/>
</html>
As you can see my ViewHelper is defined like this <ak:data.user/>. The data part is the subfolder where the UserViewHelper is located. You can change it to your preference.
Now if you do the following <f:debug>{_all}</f:debug> you will see that the loggedInUser variable is there and you can use it as you please.
That's it. Quick and easy! If you have question, don't hesitate to contact me.