XF 2.0 Get a random thread from the featured threads

abdfahim

Well-known member
I am trying to develop a widget for XF2 Demo Featured Threads Portal to select a random featured thread. I know how to do it using a raw SQL query, but I am wondering if I could use the existing repo to do the same (I am still a bit hazy about XF repos).

Currently, we can fetch all featured threads using the following code

PHP:
/** @var \Demo\Portal\Repository\FeaturedThread $repo */
        $repo = $this->repository('Demo\Portal:FeaturedThread');

        $finder = $repo->findFeaturedThreadsForPortalView();

        $featuredThreads = $finder->fetch()
            ->filter(function(\Demo\Portal\Entity\FeaturedThread $featuredThread)
            {
                return ($featuredThread->Thread->canView());
            });

        $viewParams = [
            'featuredThreads' => $featuredThreads
        ];

I am in short of an idea if I can somehow modify this to return one random thread instead.

Appreciate if anyone can help.
 
The addon is a part of the official development tutorial. I have compiled the code from there with a few modifications to serve my purpose.

If you want a feature-rich featured thread addon for XF2, I believe @ThemeHouse released one such paid addon.
 
Back
Top Bottom