2016-09-16 9 views
0

Viewhelperを使用してFEユーザーのuidを取得してレンダリングするにはどうすればよいですか?以下はコントローラ経由で動作していますが、Viewhelperでは動作しません。違いはどこですか?私は7.6.11を使用しています。最後に、FEユーザのユーザIDとユーザグループのユーザIDを持っていて、さらに拡張のHTMLや一般的な部分で使用したいと思います...TYPO3 - Viewhelper経由でFE UIDを取得する方法

/typo3conf/extに/拡張子/クラス/ ViewHelpers/UserViewHelper.php

<?php 

namespace Vendor\Extension\ViewHelpers; 

class UserViewHelper extends \TYPO3\CMS\Fluid\Core\ViewHelper\AbstractViewHelper { 

/** 
* User Repository 
* 
* @var \TYPO3\CMS\Extbase\Domain\Repository\FrontendUserRepository 
* @inject 
*/ 
protected $userRepository; 

/** 
* @var \TYPO3\CMS\Extbase\Domain\Repository\FrontendUserGroupRepository 
* @inject 
*/ 
protected $frontendUserGroupRepository; 

public function render() { 

    $userIDTest = $this->userRepository->findByUid($GLOBALS['TSFE']->fe_user->user['uid']); 
    $this->view->assign('userIDTest', $userIDTest); 

} 

} 

するlist.html

<f:layout name="Default" /> 
<f:section name="main"> 

    {userIDTest.uid} 

</f:section> 

のDimitryによって示唆されるように私は

$this->view->assign('userIDTest', $userIDTest); 
を置き換えます

return $userIDTest; 

とするlist.htmlのある

私はこれがあります。

{namespace custom=Vendor\Extension\ViewHelpers} 

<f:layout name="Default" /> 
<f:section name="main"> 

<f:alias map="{user: '{custom:user()}'}"> 
    {user.uid} {user.username} 
</f:alias> 

</f:section> 

...と、すべてのキャッシュをクリア(FE/BE /インストール)とtypo3tempを削除した後...今その作業を!

+0

どのTYPO3バージョンですか? 7.x以降では、ViewHelpersがコンパイルされ、結果として「レンダリング」メソッドが1回だけ呼び出されます(コンパイル用に)。その後、 "renderStatic"(静的メソッド)だけが呼び出されます。あなたは毎回呼び出される "renderStatic"を上書きすることができます。もちろん、リポジトリはそれほど簡単には利用できません。 – Jost

+0

こんにちはJost ...私は上記を更新しました。私は7.6.11を使用しています。サンプルを表示できますか?あまりにも多くありますか?私の側に ... –

+0

あなたは、単にユーザーID、または完全なユーザーオブジェクトをしたいですか?リポジトリが必要ないため、IDを使用すると簡単になります。 – Jost

答えて

1

ユーザーまたはユーザーのuidをビューヘルパーに戻したい場合は、それを戻します。

代わりの

$this->view->assign('userIDTest', $userIDTest); 

を使用すると、さまざまな方法でユーザー変数を使用することができ、あなたの流体でこの

return $userIDTest; 

を行います。最も簡単なのは、 "エイリアス"ビューヘルパーを使用することです:https://fluidtypo3.org/viewhelpers/fluid/master/AliasViewHelper.html

<f:alias map="{user: '{namespace:user()}'}"> 
    {user.uid} {user.username} 
</f:alias> 
2

7.x以降では、ViewHelpersがコンパイルされ、renderメソッドがコンパイルのために1回しか呼び出されません。その後、静的メソッドrenderStatic()のみが呼び出されます。あなたはそれが毎回呼び出されます、renderStaticを上書きすることができます:

<?php 

namespace Vendor\Extension\ViewHelpers; 

use TYPO3\CMS\Fluid\Core\ViewHelper\AbstractViewHelper; 

class UserIdViewHelper extends AbstractViewHelper 
{ 
    public function render() 
    { 
     return static::renderStatic(
      [], 
      $this->renderChildrenClosure, 
      $this->renderingContext 
     ); 
    } 

    public static function renderStatic(
     array $arguments, 
     \Closure $renderChildrenClosure, 
     RenderingContextInterface $renderingContext 
    ) { 
     $userData = $GLOBALS['TSFE']->fe_user->user; 

     return null !== $userData ? (int)$userData['uid'] : null; 
    } 
} 

あなたなViewHelperでいくつかのサービスを使用する必要がある場合は、依存性注入をコンパイルViewHelpersでは動作しないので、物事は、より複雑になります。オブジェクトマネージャを取得し、オブジェクトマネージャを使用してサービスのインスタンスを取得する必要があります。

これは、あなたが全体のユーザーオブジェクトだけでなく、ユーザーのUIDを返すようにしたいので、あなたがサービスとしてFrontendUserRepositoryを使用したいと仮定すると、次のようになります。

<?php 

namespace Vendor\Extension\ViewHelpers; 

use TYPO3\CMS\Core\Utility\GeneralUtility; 
use TYPO3\CMS\Extbase\Domain\Repository\FrontendUserRepository; 
use TYPO3\CMS\Extbase\Object\ObjectManager; 
use TYPO3\CMS\Fluid\Core\ViewHelper\AbstractViewHelper; 

class UserViewHelper extends AbstractViewHelper 
{ 
    /** 
    * @var FrontendUserRepository 
    */ 
    private static $frontendUserRepository = null; 

    public function render() 
    { 
     return static::renderStatic(
      [], 
      $this->renderChildrenClosure, 
      $this->renderingContext 
     ); 
    } 

    public static function renderStatic(
     array $arguments, 
     \Closure $renderChildrenClosure, 
     RenderingContextInterface $renderingContext 
    ) { 
     $userData = $GLOBALS['TSFE']->fe_user->user; 

     if (null === $userData) { 
      return null; 
     } 

     return static::getFrontendUserRepository()->findByUid((int)$userData['uid']); 
    } 

    private static function getFrontendUserRepository() 
    { 
     if (null === static::$frontendUserRepository) { 
      $objectManager = GeneralUtility::makeInstance(ObjectManager::class); 
      static::$frontendUserRepository = $objectManager->get(FrontendUserRepository::class); 
     } 

     return static::$frontendUserRepository; 
    } 
} 

免責事項:すべてのコードをされます実際に実行せずに書かれているので、バグがあります。

関連する問題