如何从typo3循环流体上的ObjectStorage项目

问题描述

我的插件上有一个喜欢的功能用户可以喜欢一个想法,为此我有一个 mm 表,我可以将想法和用户联系起来。我想根据用户是否喜欢这个想法来禁用或启用“喜欢”按钮。 当我从模型中调用 $this->VotedUsers 时,我的流体模板会得到这个。这是什么?不是数组吗?我如何循环这个以检查用户是否已经喜欢这个想法。

这是我的想法模型中的 $this->VotedUsers 属性所包含的内容,我想循环检查 FrontendUser 是否已经喜欢该想法并禁用或启用按钮。

enter image description here

这就是 {_all} 的样子

enter image description here

enter image description here

解决方法

我会在您的想法模型中执行此操作:


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 中检查它。