Search code examples
typo3typo3-8.xtypo3-extensions

Get data from FE user into own EXT in TYPO3 8.7.x


Heyho, I am trying to get data, (i.e. the uid) from the current logged in user who is using the site.

In TYPO3 7.6.X it was very easy. You just had to use $GLOBALS['TSFE']->fe_user->user to get the data. In TYPO3 8.7.x it is a little bit more complicated. It should work with $frontendUserAspect = GeneralUtility::makeInstance(Context::class)->getAspect('frontend.user'); $frontendUserAspect->get('id'). But in my case it does not.

My code looks like this:

<?php

namespace Reevo\ReevoElearning\Output;

use TYPO3\CMS\Core\Database\ConnectionPool;
use TYPO3\CMS\Core\Database\Query\QueryBuilder;
use TYPO3\CMS\Core\Utility\GeneralUtility;
use TYPO3\CMS\Extbase\Persistence\Generic\Mapper\DataMapper;
use \TYPO3\CMS\Frontend\ContentObject\ContentObjectRenderer;
use TYPO3\CMS\Extbase\Object\ObjectManager;
use TYPO3\CMS\Frontend\Controller\TypoScriptFrontendController;
use TYPO3\CMS\Core\Context\Context;
use TYPO3\CMS\Core\Context\UserAspect;

class FlexformValue {
    function field($content, $conf) {
// TSFE USER ID
$frontendUserAspect = GeneralUtility::makeInstance(Context::class)->getAspect('frontend.user');
$frontendUserAspect->get('id'); 
$test = $frontendUserAspect;
echo $test;
return "$test";
    }
} 

I get the following error: 'TYPO3\CMS\Core\Context\Context' is not found or something like this. But if I am deleting this line use TYPO3\CMS\Core\Context\Context; it is looking for the same file in my namespace folder. Does anyone know how to get it work?


Solution

  • $GLOBALS['TSFE']->fe_user->user is still available in TYPO3 8.7. I think it has been deprecated in 9, but won't be removed until 10. The FrontendUserAspect was introduced in 9.4 I think and probably TYPO3\CMS\Core\Context\Context was as well, so the error is correct. You should still use $GLOBALS['TSFE']->fe_user->user in TYPO3 8.7.

    Official documentation:

    As for TYPO3 v9, the old properties can be used the same way as before, but will trigger a PHP E_USER_DEPRECATED error.