Ad

How To Loop ObjectStorage Items On Fluid From Typo3

- 1 answer

I have a like feature on my plugin where a user can like an idea, for that I have a mm table where I relate the idea and the user. I want to disable or enable the "like" button depending on if the user has liked the idea or not already. When I call the $this->votedUsers from my model my fluid template gets this. What is this? Is not an array? How can I loop this to check if the user has liked the idea already.

This is what my $this->votedUsers property from my Idea model contains and I want to loop to check if that FrontendUser has liked the idea already and disable or enable de button.

enter image description here

This is how <f:debug>{_all}</f:debug> looks like

enter image description here

enter image description here

Ad

Answer

I would do this inside your idea model:


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;
}

After that you can check it in Fluid via <f:if condition="{idea.likedByCurrentUser}"> ... </f:if>.

Ad
source: stackoverflow.com
Ad