I need a way to get the current logged in user object from Fluid. I need it in Partials and Layouts, not only Templates, so I can't do this by getting it as an argument from a Controller class.
I already know how to do this from Typoscript, e.g. to get his/her name:
TSFE:fe_user|user|name
To get it from Php I use my own static function from my own extended FrontendUser class: User, called User::getLogged()
. Could I call this function from a Partial? Or is there any other method?
Thanks in advance!
My Answer:
In Fluid, you can access PHP functions and objects by using the {namespace} syntax. You can create a custom namespace in your Fluid template to access your User class and its methods.
Here's an example of how you can access the User::getLogged() method in a Fluid template:
1. Define a custom namespace in your Fluid template:
html
{namespace user=Vendor\ExtensionName\ViewHelpers}
2. Create a ViewHelper class that calls the User::getLogged() method:
php
namespace Vendor\ExtensionName\ViewHelpers;
use TYPO3\CMS\Fluid\Core\ViewHelper\AbstractViewHelper;
class GetLoggedUserViewHelper extends AbstractViewHelper
{
public function render()
{
return \Vendor\ExtensionName\Domain\Model\User::getLogged();
}
}
3. Use the custom ViewHelper in your Fluid template to get the current logged-in user:
html
{user:getLogged()}
This way, you can access the current logged-in user object in your Partials and Layouts without passing it as an argument from a Controller class.
Rate this post
3 of 5 based on 4691 votesComments