如何从 typo3 循环 ObjectStorage 项目
How to loop ObjectStorage items on fluid from typo3
我的插件有一个点赞功能,用户可以点赞一个想法,因为我有一个 mm table 可以将想法与用户联系起来。我想根据用户是否喜欢这个想法来禁用或启用“喜欢”按钮。
当我从我的模型中调用 $this->votedUsers 时,我的流体模板得到了这个。这是什么?是不是数组?我怎样才能循环这个以检查用户是否已经喜欢这个想法。
这是我的想法模型中的 $this->votedUsers 属性 包含的内容,我想循环检查 FrontendUser 是否已经喜欢这个想法并禁用或启用 de 按钮。
这就是 {_all} 的样子
我会在你的想法模型中这样做:
public function isLikedByCurrentUser(): bool
{
$currentUser = GeneralUtility::makeInstance(ObjectManager::class)
->get(FrontendUserRepository::class)
->findByUid($GLOBALS['TSFE']->fe_user->user['uid']);
if ($currentUser) {
return $this->votedUsers->contains($currentUser);
}
return false;
}
之后你可以通过<f:if condition="{idea.likedByCurrentUser}"> ... </f:if>
在Fluid中查看它。
我的插件有一个点赞功能,用户可以点赞一个想法,因为我有一个 mm table 可以将想法与用户联系起来。我想根据用户是否喜欢这个想法来禁用或启用“喜欢”按钮。 当我从我的模型中调用 $this->votedUsers 时,我的流体模板得到了这个。这是什么?是不是数组?我怎样才能循环这个以检查用户是否已经喜欢这个想法。
这是我的想法模型中的 $this->votedUsers 属性 包含的内容,我想循环检查 FrontendUser 是否已经喜欢这个想法并禁用或启用 de 按钮。
这就是
我会在你的想法模型中这样做:
public function isLikedByCurrentUser(): bool
{
$currentUser = GeneralUtility::makeInstance(ObjectManager::class)
->get(FrontendUserRepository::class)
->findByUid($GLOBALS['TSFE']->fe_user->user['uid']);
if ($currentUser) {
return $this->votedUsers->contains($currentUser);
}
return false;
}
之后你可以通过<f:if condition="{idea.likedByCurrentUser}"> ... </f:if>
在Fluid中查看它。