*/ class BeforeTemplateRenderedListener implements IEventListener { /** @var ProfileManager */ private $profileManager; /** * BeforeTemplateRenderedListener constructor. * * @param ProfileManager $profileManager * @param IUserSession $userSession * @param IInitialStateService $initialState * @param JSDataService $jsDataService */ public function __construct( ProfileManager $profileManager, private IUserSession $userSession, private IInitialStateService $initialState, private JSDataService $jsDataService, ) { $this->profileManager = $profileManager; } /** * @inheritDoc */ public function handle(Event $event): void { $user = $this->userSession->getUser(); if ($user === null) { return; } if (!($event instanceof BeforeTemplateRenderedEvent)) { // Unrelated return; } if (!$event->isLoggedIn() || $event->getResponse()->getRenderAs() !== TemplateResponse::RENDER_AS_USER) { return; } $this->initialState->provideLazyInitialState(Application::APP_ID, 'status', function () { return $this->jsDataService; }); $this->initialState->provideLazyInitialState(Application::APP_ID, 'profileEnabled', function () use ($user) { return ['profileEnabled' => $this->profileManager->isProfileEnabled($user)]; }); Util::addScript('user_status', 'menu'); Util::addStyle('user_status', 'menu'); } }